문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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); }

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top