Aleksa Topić
Aleksa Topić

Reputation: 549

Is JSONKit creating memory leaks?

I have been using JSONKit in my app, but now that I have upgraded to Xcode 4.5.1 and run the analyze, Xcode is reporting possible memory leaks in JSONKit code.

/Users/aleksa.topic/SVN/Apple/iTTChart/trunk/iTTChart/Other Sources/JSONKit.m:682:23: Memory is never released; potential leak of memory pointed to by 'array' (and the it gives the same potential leak for dictionary).

Does anyone have some experience with this? Is it really creating memory leaks or is it just that Xcode is't analyzing good enough?

Upvotes: 4

Views: 1125

Answers (3)

Baby Groot
Baby Groot

Reputation: 4279

See this link. Just replace the lines marked as - with the one marked as +.

-    if((array = [array init]) == NULL) { return(NULL); }
+    if([array init] == NULL) { free(array); return(NULL); }

-    if(JK_EXPECT_F((array->objects = (id *)malloc(sizeof(id) * array->capacity)) == NULL)) { [array autorelease]; return(NULL); }
+    if(JK_EXPECT_F((array->objects = (id *)malloc(sizeof(id) * array->capacity)) == NULL)) { free(array); return(NULL); }

-    if((dictionary = [dictionary init]) == NULL) { return(NULL); }
+    if([dictionary init] == NULL) { free(dictionary);return(NULL); }

-    if(JK_EXPECT_F((dictionary->entry = (JKHashTableEntry *)calloc(1UL, sizeof(JKHashTableEntry) * dictionary->capacity)) == NULL)) { [dictionary autorelease]; return(NULL); }
+    if(JK_EXPECT_F((dictionary->entry = (JKHashTableEntry *)calloc(1UL, sizeof(JKHashTableEntry) * dictionary->capacity)) == NULL)) { free(dictionary); return(NULL); }

Upvotes: 2

Aina
Aina

Reputation: 11

Replace ((array = [array init]) == NULL) by (dictionary == NULL) and use free(array) function instead of [array autorelease] to fix it. Because it's allecated manually so it shoult be released manualy too.

Upvotes: 1

lqs
lqs

Reputation: 1454

This is a false positive in the static analyzer. There's a bug report trying to resolve it.

Upvotes: 4

Related Questions