celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pnol...@apache.org
Subject [2/2] celix git commit: CELIX-367: Fixes mem leak in properties
Date Tue, 12 Jul 2016 07:01:17 GMT
CELIX-367: Fixes mem leak in properties


Project: http://git-wip-us.apache.org/repos/asf/celix/repo
Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/e7fc3a64
Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/e7fc3a64
Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/e7fc3a64

Branch: refs/heads/develop
Commit: e7fc3a643e4f84218ac31271f06e5c6bff997ed6
Parents: 9828c68
Author: Pepijn Noltes <pepijnnoltes@gmail.com>
Authored: Tue Jul 12 08:43:20 2016 +0200
Committer: Pepijn Noltes <pepijnnoltes@gmail.com>
Committed: Tue Jul 12 08:43:20 2016 +0200

----------------------------------------------------------------------
 framework/private/mock/properties_mock.c |  3 +-
 framework/private/src/framework.c        | 40 +++++++++++----------------
 framework/private/src/manifest.c         |  6 +---
 framework/private/src/properties.c       | 15 ++++++++--
 framework/public/include/properties.h    |  2 +-
 utils/private/include/hash_map_private.h |  2 +-
 utils/private/src/hash_map.c             |  2 +-
 7 files changed, 33 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/celix/blob/e7fc3a64/framework/private/mock/properties_mock.c
----------------------------------------------------------------------
diff --git a/framework/private/mock/properties_mock.c b/framework/private/mock/properties_mock.c
index 8f05569..cf873a9 100644
--- a/framework/private/mock/properties_mock.c
+++ b/framework/private/mock/properties_mock.c
@@ -58,12 +58,11 @@ const char * properties_getWithDefault(properties_pt properties, const
char * ke
 	return mock_c()->returnValue().value.pointerValue;
 }
 
-const char * properties_set(properties_pt properties, const char * key, const char * value)
{
+void properties_set(properties_pt properties, const char * key, const char * value) {
 	mock_c()->actualCall("properties_set")
 		->withPointerParameters("properties", properties)
 		->withStringParameters("key", key)
 		->withStringParameters("value", value);
-	return mock_c()->returnValue().value.pointerValue;
 }
 
 

http://git-wip-us.apache.org/repos/asf/celix/blob/e7fc3a64/framework/private/src/framework.c
----------------------------------------------------------------------
diff --git a/framework/private/src/framework.c b/framework/private/src/framework.c
index 09b36a3..013eb88 100644
--- a/framework/private/src/framework.c
+++ b/framework/private/src/framework.c
@@ -272,12 +272,12 @@ celix_status_t framework_destroy(framework_pt framework) {
                     array_list_pt handles = NULL;
                     status = CELIX_DO_IF(status, bundleArchive_getCurrentRevision(archive,
&revision));
                     status = CELIX_DO_IF(status, bundleRevision_getHandles(revision, &handles));
-                    if(handles != NULL){
-			for (int i = arrayList_size(handles) - 1; i >= 0; i--) {
-			    void *handle = arrayList_get(handles, i);
-			    fw_closeLibrary(handle);
-			}
-						  }
+                    if (handles != NULL) {
+                        for (int i = arrayList_size(handles) - 1; i >= 0; i--) {
+                            void *handle = arrayList_get(handles, i);
+                            fw_closeLibrary(handle);
+                        }
+                    }
                 }
 
                 bundleArchive_destroy(archive);
@@ -791,17 +791,17 @@ celix_status_t framework_updateBundle(framework_pt framework, bundle_pt
bundle,
 	status = CELIX_DO_IF(status, bundleArchive_setLastModified(archive, time(NULL)));
 	status = CELIX_DO_IF(status, framework_setBundleStateAndNotify(framework, bundle, OSGI_FRAMEWORK_BUNDLE_INSTALLED));
 
-	// TODO Unload all libraries for transition to unresolved
 	bundle_revision_pt revision = NULL;
 	array_list_pt handles = NULL;
 	status = CELIX_DO_IF(status, bundleArchive_getCurrentRevision(archive, &revision));
     status = CELIX_DO_IF(status, bundleRevision_getHandles(revision, &handles));
-    if(handles != NULL){
-	for (int i = arrayList_size(handles) - 1; i >= 0; i--) {
-	    void *handle = arrayList_get(handles, i);
-	    fw_closeLibrary(handle);
-	}
-	 }
+    if (handles != NULL) {
+        int i;
+	    for (i = arrayList_size(handles) - 1; i >= 0; i--) {
+	        void* handle = arrayList_get(handles, i);
+	        fw_closeLibrary(handle);
+	    }
+    }
 
 
 	status = CELIX_DO_IF(status, fw_fireBundleEvent(framework, OSGI_FRAMEWORK_BUNDLE_EVENT_UNRESOLVED,
bundle));
@@ -2496,7 +2496,6 @@ static celix_status_t framework_loadBundleLibraries(framework_pt framework,
bund
     return status;
 }
 
-// TODO Store all handles for unloading!
 static celix_status_t framework_loadLibraries(framework_pt framework, const char *librariesIn,
const char *activator, bundle_archive_pt archive, void **activatorHandle) {
     celix_status_t status = CELIX_SUCCESS;
 
@@ -2531,14 +2530,8 @@ static celix_status_t framework_loadLibraries(framework_pt framework,
const char
         char *trimmedLib = utils_stringTrim(lib);
         status = framework_loadLibrary(framework, trimmedLib, archive, &handle);
 
-        if( (status == CELIX_SUCCESS) && (activator != NULL) && (strcmp(trimmedLib,
activator) == 0) ) {
-		*activatorHandle = handle;
-        }
-        else{
-		*activatorHandle = NULL;
-		if(handle != NULL){
-			fw_closeLibrary(handle);
-		}
+        if ( (status == CELIX_SUCCESS) && (activator != NULL) && (strcmp(trimmedLib,
activator) == 0) ) {
+		    *activatorHandle = handle;
         }
 
         token = strtok_r(NULL, ",", &last);
@@ -2587,9 +2580,8 @@ static celix_status_t framework_loadLibrary(framework_pt framework,
const char *
     	status = CELIX_FRAMEWORK_EXCEPTION;
     } else {
 		*handle = fw_openLibrary(libraryPath);
-		if (*handle == NULL) {
+        if (*handle == NULL) {
 			error = fw_getLastError();
-			// #TODO this is wrong
 			status =  CELIX_BUNDLE_EXCEPTION;
 		} else {
 			bundle_revision_pt revision = NULL;

http://git-wip-us.apache.org/repos/asf/celix/blob/e7fc3a64/framework/private/src/manifest.c
----------------------------------------------------------------------
diff --git a/framework/private/src/manifest.c b/framework/private/src/manifest.c
index 7e24aec..29b155a 100644
--- a/framework/private/src/manifest.c
+++ b/framework/private/src/manifest.c
@@ -207,7 +207,6 @@ celix_status_t manifest_readAttributes(manifest_pt manifest, properties_pt
prope
 
 
 	while (fgets(lbuf, sizeof(lbuf), file ) != NULL ) {
-		bool lineContinued = false;
 		int len = strlen(lbuf);
 
 		if (lbuf[--len] != '\n') {
@@ -226,7 +225,6 @@ celix_status_t manifest_readAttributes(manifest_pt manifest, properties_pt
prope
 			buf[0] = '\0';
 
 			// Line continued
-			lineContinued = true;
 			strcat(buf, lastLine);
 			strncat(buf, lbuf+1, len - 1);
 
@@ -265,9 +263,7 @@ celix_status_t manifest_readAttributes(manifest_pt manifest, properties_pt
prope
 			value[len - i] = '\0';
 		}
 
-		if ((properties_set(properties, name, value) != NULL) && (!lineContinued)) {
-			printf("Duplicate entry: %s", name);
-		}
+		properties_set(properties, name, value);
 	}
 
 	return CELIX_SUCCESS;

http://git-wip-us.apache.org/repos/asf/celix/blob/e7fc3a64/framework/private/src/properties.c
----------------------------------------------------------------------
diff --git a/framework/private/src/properties.c b/framework/private/src/properties.c
index 7b3fd50..68ed7f7 100644
--- a/framework/private/src/properties.c
+++ b/framework/private/src/properties.c
@@ -135,7 +135,7 @@ FRAMEWORK_EXPORT properties_pt properties_loadWithStream(FILE *file) {
 
 			if (!isComment) {
 				//printf("putting 'key'/'value' '%s'/'%s' in properties\n", utils_stringTrim(key), utils_stringTrim(value));
-				hashMap_put(props, strdup(utils_stringTrim(key)), strdup(utils_stringTrim(value)));
+				properties_set(props, utils_stringTrim(key), utils_stringTrim(value));
 			}
 		}
 	}
@@ -218,6 +218,15 @@ const char* properties_getWithDefault(properties_pt properties, const
char* key,
 	return value == NULL ? defaultValue : value;
 }
 
-const char* properties_set(properties_pt properties, const char* key, const char* value)
{
-	return hashMap_put(properties, strndup(key, 1024*10), strndup(value, 1024*10));
+void properties_set(properties_pt properties, const char* key, const char* value) {
+    hash_map_entry_pt entry = hashMap_getEntry(properties, key);
+    char* oldValue = NULL;
+	if (entry != NULL) {
+        char* oldKey = hashMapEntry_getKey(entry);
+        oldValue = hashMapEntry_getValue(entry);
+        hashMap_put(properties, oldKey, strndup(value, 1024*10));
+	} else {
+        hashMap_put(properties, strndup(key, 1024*10), strndup(value, 1024*10));
+    }
+    free(oldValue);
 }

http://git-wip-us.apache.org/repos/asf/celix/blob/e7fc3a64/framework/public/include/properties.h
----------------------------------------------------------------------
diff --git a/framework/public/include/properties.h b/framework/public/include/properties.h
index 0f0b0b1..13daebb 100644
--- a/framework/public/include/properties.h
+++ b/framework/public/include/properties.h
@@ -43,7 +43,7 @@ FRAMEWORK_EXPORT void properties_store(properties_pt properties, const char*
fil
 
 FRAMEWORK_EXPORT const char* properties_get(properties_pt properties, const char* key);
 FRAMEWORK_EXPORT const char* properties_getWithDefault(properties_pt properties, const char*
key, const char* defaultValue);
-FRAMEWORK_EXPORT const char* properties_set(properties_pt properties, const char* key, const
char* value);
+FRAMEWORK_EXPORT void properties_set(properties_pt properties, const char* key, const char*
value);
 
 FRAMEWORK_EXPORT celix_status_t properties_copy(properties_pt properties, properties_pt *copy);
 

http://git-wip-us.apache.org/repos/asf/celix/blob/e7fc3a64/utils/private/include/hash_map_private.h
----------------------------------------------------------------------
diff --git a/utils/private/include/hash_map_private.h b/utils/private/include/hash_map_private.h
index 4950b63..6efa2a1 100644
--- a/utils/private/include/hash_map_private.h
+++ b/utils/private/include/hash_map_private.h
@@ -34,7 +34,7 @@ UTILS_EXPORT unsigned int hashMap_hashCode(const void* toHash);
 UTILS_EXPORT int hashMap_equals(const void* toCompare, const void* compare);
 
 void hashMap_resize(hash_map_pt map, int newCapacity);
-void * hashMap_removeEntryForKey(hash_map_pt map, const void* key);
+hash_map_entry_pt hashMap_removeEntryForKey(hash_map_pt map, const void* key);
 UTILS_EXPORT hash_map_entry_pt hashMap_removeMapping(hash_map_pt map, hash_map_entry_pt entry);
 void hashMap_addEntry(hash_map_pt map, int hash, void* key, void* value, int bucketIndex);
 

http://git-wip-us.apache.org/repos/asf/celix/blob/e7fc3a64/utils/private/src/hash_map.c
----------------------------------------------------------------------
diff --git a/utils/private/src/hash_map.c b/utils/private/src/hash_map.c
index aa320d4..4c1ca7c 100644
--- a/utils/private/src/hash_map.c
+++ b/utils/private/src/hash_map.c
@@ -218,7 +218,7 @@ void * hashMap_remove(hash_map_pt map, const void* key) {
 	return value;
 }
 
-void * hashMap_removeEntryForKey(hash_map_pt map, const void* key) {
+hash_map_entry_pt hashMap_removeEntryForKey(hash_map_pt map, const void* key) {
 	unsigned int hash = (key == NULL) ? 0 : hashMap_hash(map->hashKey(key));
 	int i = hashMap_indexFor(hash, map->tablelength);
 	hash_map_entry_pt prev = map->table[i];


Mime
View raw message