Index: wrapper/opensync-plugin.i
===================================================================
--- wrapper/opensync-plugin.i	(revision 4327)
+++ wrapper/opensync-plugin.i	(working copy)
@@ -106,7 +106,7 @@
 	}
 
 	~PluginEnv() {
-		osync_plugin_env_free(self);
+		osync_plugin_env_unref(self);
 	}
 
 	void load(const char *path = NULL) {
Index: opensync/plugin/opensync_plugin_env_private.h
===================================================================
--- opensync/plugin/opensync_plugin_env_private.h	(revision 4327)
+++ opensync/plugin/opensync_plugin_env_private.h	(working copy)
@@ -27,6 +27,8 @@
 	GList *modules;
 	
 	GModule *current_module;
+
+	int ref_count;
 };
 
 #endif /* _OPENSYNC_PLUGIN_ENV_PRIVATE_H_ */
Index: opensync/plugin/opensync_plugin_env.c
===================================================================
--- opensync/plugin/opensync_plugin_env.c	(revision 4327)
+++ opensync/plugin/opensync_plugin_env.c	(working copy)
@@ -37,30 +37,45 @@
 		return NULL;
 	}
 	
+	env->ref_count = 1;
 	osync_trace(TRACE_EXIT, "%s: %p", __func__, env);
 	return env;
 }
 
-void osync_plugin_env_free(OSyncPluginEnv *env)
+
+OSyncPluginEnv *osync_plugin_env_ref(OSyncPluginEnv *env)
 {
 	osync_trace(TRACE_ENTRY, "%s(%p)", __func__, env);
 	osync_assert(env);
+
+	g_atomic_int_inc(&(env->ref_count));
+
+	osync_trace(TRACE_EXIT, "%s", __func__);
+	return env;
+
+}
+
+void osync_plugin_env_unref(OSyncPluginEnv *env)
+{
+	osync_trace(TRACE_ENTRY, "%s(%p)", __func__, env);
+	osync_assert(env);
 	
-	/* Free the plugins */
-	while (env->plugins) {
-		osync_plugin_unref(env->plugins->data);
-		env->plugins = g_list_remove(env->plugins, env->plugins->data);
-	}
+	if (g_atomic_int_dec_and_test(&(env->ref_count))) {
+		/* Free the plugins */
+		while (env->plugins) {
+			osync_plugin_unref(env->plugins->data);
+			env->plugins = g_list_remove(env->plugins, env->plugins->data);
+		}
 	
-	/* Unload all modules */
-	while (env->modules) {
-		osync_module_unload(env->modules->data);
-		osync_module_free(env->modules->data);
-		env->modules = g_list_remove(env->modules, env->modules->data);
+		/* Unload all modules */
+		while (env->modules) {
+			osync_module_unload(env->modules->data);
+			osync_module_free(env->modules->data);
+			env->modules = g_list_remove(env->modules, env->modules->data);
+		}
+		osync_free(env);
 	}
 	
-	g_free(env);
-	
 	osync_trace(TRACE_EXIT, "%s", __func__);
 }
 
Index: opensync/plugin/opensync_plugin_env.h
===================================================================
--- opensync/plugin/opensync_plugin_env.h	(revision 4327)
+++ opensync/plugin/opensync_plugin_env.h	(working copy)
@@ -47,15 +47,28 @@
  */
 OSYNC_EXPORT OSyncPluginEnv *osync_plugin_env_new(OSyncError **error);
 
-/*! @brief Frees a osync environment
+/*! @brief Increases the reference counton an opensync plugin environment
  * 
- * Frees a osync environment and all resources.
+ * The reference count on the OSyncPluginEnv is incremented. When the
+ * reference is no longer needed it should be removed with 
+ * osync_plugin_env_unref
  * 
- * @param env Pointer to the environment to free
+ * @returns The environment passed in
  * 
  */
-OSYNC_EXPORT void osync_plugin_env_free(OSyncPluginEnv *env);
+OSYNC_EXPORT OSyncPluginEnv *osync_plugin_env_ref(OSyncPluginEnv *env);
 
+/*! @brief Removes a reference to an OSyncPluginEnv
+ * 
+ * Decrements the reference count on an osync plugin environment.  If
+ * the reference count reaches zero the environment is freed and all
+ * resources are unreferenced
+ * 
+ * @param env Pointer to the environment to unreference
+ * 
+ */
+OSYNC_EXPORT void osync_plugin_env_unref(OSyncPluginEnv *env);
+
 /*! @brief Loads the sync modules from a given directory
  * 
  * Loads all sync modules from a directory into a osync environment
Index: opensync/engine/opensync_engine.c
===================================================================
--- opensync/engine/opensync_engine.c	(revision 4327)
+++ opensync/engine/opensync_engine.c	(working copy)
@@ -1301,7 +1301,7 @@
   }
 	
   if (engine->pluginenv) {
-    osync_plugin_env_free(engine->pluginenv);
+    osync_plugin_env_unref(engine->pluginenv);
     engine->pluginenv = NULL;
   }
 	
Index: opensync/client/opensync_client.c
===================================================================
--- opensync/client/opensync_client.c	(revision 4327)
+++ opensync/client/opensync_client.c	(working copy)
@@ -698,7 +698,7 @@
   }
 	
   if (client->plugin_env) {
-    osync_plugin_env_free(client->plugin_env);
+    osync_plugin_env_unref(client->plugin_env);
     client->plugin_env = NULL;
   }
 	
Index: tools/osyncplugin.c
===================================================================
--- tools/osyncplugin.c	(revision 4327)
+++ tools/osyncplugin.c	(working copy)
@@ -429,7 +429,7 @@
   osync_format_env_free(format_env);
   format_env = NULL;
  error_free_pluginenv:
-  osync_plugin_env_free(plugin_env);
+  osync_plugin_env_unref(plugin_env);
   plugin_env = NULL;
  error:	
   return FALSE;
@@ -1218,7 +1218,7 @@
   osync_format_env_free(format_env);
   format_env = NULL;
  error_free_pluginenv:
-  osync_plugin_env_free(plugin_env);
+  osync_plugin_env_unref(plugin_env);
   plugin_env = NULL;
  error:	
   return FALSE;
@@ -1255,7 +1255,7 @@
 
  success:
   if (plugin_env)
-    osync_plugin_env_free(plugin_env);
+    osync_plugin_env_unref(plugin_env);
 
   for (o=cmdlist; o; o = o->next) {
     Command *cmd = o->data;
@@ -1272,7 +1272,7 @@
   finalize_plugin(&plugin_data);
   //error_free_plugin_env:
   if (plugin_env)
-    osync_plugin_env_free(plugin_env);
+    osync_plugin_env_unref(plugin_env);
 
   for (o=cmdlist; o; o = o->next) {
     Command *cmd = o->data;
Index: opensync.sym
===================================================================
--- opensync.sym	(revision 4327)
+++ opensync.sym	(working copy)
@@ -561,13 +561,14 @@
 osync_plugin_connection_usb_set_vendorid
 osync_plugin_discover
 osync_plugin_env_find_plugin
-osync_plugin_env_free
 osync_plugin_env_load
 osync_plugin_env_new
 osync_plugin_env_nth_plugin
 osync_plugin_env_num_plugins
 osync_plugin_env_plugin_is_usable
+osync_plugin_env_ref
 osync_plugin_env_register_plugin
+osync_plugin_env_unref
 osync_plugin_finalize
 osync_plugin_get_config_type
 osync_plugin_get_data

