Leaks (device) report leaks in auto-realized objects

Update: This leak has been resolved. If you get similar leaks and your application is multithreaded, you are most likely making UIKit calls from the background thread; use, for example, [NSThread performSelectorOnMainThread:] to route UIKit calls to the main thread, which is the only place they are allowed .

I have been running leaks in my current project to find leaks recently, and I continue to run into these โ€œleaks,โ€ which from what I can say are not leaks. The following code, taken directly from the project, has two leaks according to the leaks:

 - (NSArray *)areaForIndex:(int)index { NSMutableArray *a = [NSMutableArray arrayWithArray: [world retrieveNeighborsForIndex:index]]; // leak 1 [a insertObject:[references objectAtIndex:index] atIndex:0]; return [NSArray arrayWithArray:a]; // leak 2 } 

Leak 1 goes away if I change the first line to: (see Update 2-3)

Leak 2 goes away if I change the last line to:

  return a; 

I cannot, unfortunately, do this with leak 1, because I am imposing an immutable array into a mutable one. However, arrayWithArray should be auto-advertising, one way or another, so from what I can say, this should not leak anything. Any ideas why this is happening?

Update: I tested this on both the device and the Simulator. A leak there on both. However, on the Simulator, I get additional information about this leak:

The leak history is as follows:

 # | Category | Event Type | Timestamp | RefCt | Address | Size | Responsible Library | Responsible Caller --+----------+-------------+ 0 | CFArray | Malloc | 00:09.598 | 1 | 0x474f6d0 | 48 | asynchro | -[muddyGrid areaForIndex:] 1 | CFArray | Autorelease | 00:09.598 | | 0x474f6d0 | 0 | Foundation | NSRecordAllocationEvent 2 | CFArray | CFRetain | 00:09.598 | 2 | 0x474f7d0 | 0 | Foundation | -[NSCFArray retain] 3 | CFArray | CFRelease | 00:09.611 | 1 | 0x474f7d0 | 0 | Foundation | NSPopAutoreleasePool 

What I can distinguish from the above is that the auto-implemented array is somehow saved twice, and then auto-implemented, leaving the count value at 1. I donโ€™t know where and why, though ...

Update 2 and 3: I tried changing the line for leak 1 to:

  NSMutableArray *a = [[[NSMutableArray alloc] initWithArray: [world retrieveNeighborsForIndex:index]] autorelease]; 

I thought this fixed the leak, but in the end this did not happen. Therefore, I am still at a loss.

+6
memory-leaks iphone xcode ipad instruments
source share
3 answers

This was solved analytically, as I solved a bunch of other problems with my code.

Basically, the code had several places where user interface updates were made outside the main thread, which is a big no-no. One of these other unrelated issues was to cause a memory leak in the above code, as it no longer reports any leaks (I have 0 leaks in its current form), although I did not modify the code.

+2
source share

What returns retrieveNeighborsForIndex ?

Is there a chance that the result of this method will be saved (not auto-implemented)?

0
source share

arrayWithArray stores the objects stored in your mutable array. http://www.iphonedevsdk.com/forum/iphone-sdk-development/14285-nsmutablearray-arraywitharray-does-add-retain.html

I would recommend releasing it, or even safer creating objects with a set of autorelease attributes. When calling extraction source neighbors for an index, these objects are automatically freed, and they will be freed when the nsmutable array dealloc'ed

EDIT: One piece of advice I could add when tracking memory errors is to enable zombies and check the number of links to your objects. You can then determine which ones are not being released, and this should facilitate your tracking. This link will show you how to set up an xcode project to enable zombies: cocoadev.com/index.pl?NSZombieEnabled

-one
source share

All Articles