开发者

why is there a memory leak in the SimpleEKDemo code?

开发者 https://www.devze.com 2023-03-09 08:19 出处:网络
When profiling the SimpleEKDemo application from Apple I note there are some memory leaks. One of the leaks is __NSArrayM wh开发者_开发问答ich has 3 lines in the Leaked Blocks history, a Malloc/Assig

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

One of the leaks is __NSArrayM wh开发者_开发问答ich 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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消