Search code examples
iphoneobjective-ciosmemory-managementeventkit

why is there a memory leak in the SimpleEKDemo code?


When profiling the SimpleEKDemo application from Apple I note there are some memory leaks.

One of the leaks is __NSArrayM which has 3 lines in the Leaked Blocks history, a Malloc/Assign/Release.

Question - can someone point out the root cause issue here? (I'm trying learn how to take Instruments output of where a leaky object was created, and then from there work out root cause, so this would be really useful)


Solution

  • You will notice that when you run the demo with leaks that there is a leak in viewDidLoad (responsible frame). If you switch to Call Tree detail and if you have enabled Invert Call Tree, you will see a leak associated with the call +[NSArray new]. If you open that a bit, you will see initWithArray which is called in the RootViewController's viewDidLoad. The problem bit is,

    self.eventsList = [[NSMutableArray alloc] initWithArray:0];
    

    eventsList is a retained property so the object created has a retain count of 2. However it is only released once either through the release in dealloc or through reassignment of eventsList. YOu will have to autorelease this object.

    self.eventsList = [[[NSMutableArray alloc] initWithArray:0] autorelease];
    

    Once fixed, you shouldn't get any errors.