开发者

Using Instruments for Memory Leak Checking in XCode?

Good Day,

I'm completely inexperienced in checking for memory leaks and so any help with this would be great.

I've just finished the bulk of the work for my iPhone app and I'm now trying to figure out why it stops working after a couple of runs. Using Instruments in Leaks and Al开发者_开发知识库locations mode I can see there are two objects that are piling up memory quite quickly and not releasing:

Using Instruments for Memory Leak Checking in XCode?

I'm not a hundred percent sure where or why this is happening, but when I clicked on the arrow to the right of UIDeviceRGBColor the Responsible Caller is stated as

[UIColor allocWithZone];

I did a search through my project for UIColor and came up this (take note of _colorThreshold):

Using Instruments for Memory Leak Checking in XCode?

I believe my problem has to do with _colorThreshold which doesn't seem to be getting released:

Using Instruments for Memory Leak Checking in XCode?

I've tried adding autorelease to their initialisation arguments, but that made the app crash. Any advice here?


EDIT 1

Here is the screen shot from LevelMeter.h

Using Instruments for Memory Leak Checking in XCode?


There are several issues with the above:

  • Is LevelMeterColorThreshold an Objective-C class?
  • If so, why are you using malloc instead of the usual alloc/init?
  • As you pasted screenshots of your search results, we cannot see the surrounding code, as only lines with search hits are displayed.

Does the Leaks instrument report leaks, or are you just allocating unnecessary memory?
There is a difference between those two cases:

  1. A leak happens if you loose reference to an object so that you cannot send it a release message later.
  2. Instantiating objects, that aren't needed anymore without releasing/freeing them

Leaks can only detect the first case.
Sample for a leak:

NSMutableString* test = [[NSMutableString alloc] initWithString:@"1"];
NSLog(@"%@", test);
NSMutableString* anotherTest = [[NSMutableString alloc] initWithString:@"2"];
test = anotherTest; //here we loose reference to the original object
NSLog(@"%@", test);

By assigning anotherTest to test, we have lost the reference that points to the memory address that contains @"1".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜