开发者

iPhone memory leak help

开发者 https://www.devze.com 2022-12-16 23:46 出处:网络
This code is leaking, the performance tool blames two leaks on this block of code. If i comment it out the leak doesn\'t happen. Any help pinning it down would be greatly appreciated.

This code is leaking, the performance tool blames two leaks on this block of code. If i comment it out the leak doesn't happen. Any help pinning it down would be greatly appreciated.

Leaks:

Malloc 48 bytes

NSCFarray 32 bytes

Code Block:

    NSArray *myArray = [[NSArray alloc] initWithObjects: @"Add", @"Edit", nil];
    segmentControl = [[UISegmentedControl alloc] initWithItems:myArray];
    [myArray release];
    [segmentControl setSegmentedControlStyle:UISegmentedControlStyleBar];
    [segmentControl setMomentary:YES];
    [segmentControl addTarget:self action:@selector(addOrEditPressed) forControlEvents:UIControlEventValueChanged];
    UIBarButtonItem *myBarButto开发者_Python百科nItem = [[UIBarButtonItem alloc] initWithCustomView:segmentControl];
    self.navigationItem.rightBarButtonItem = myBarButtonItem;
    [myBarButtonItem release];


As long as segmentControl is nil when you enter the code block and is being released somewhere else in your code (like dealloc or viewDidUnload) then you are doing nothing wrong.

Have you tried running your code under the static analyzer (Xcode menu: Build | Build & Analyze)?

Instruments can sometimes generate false positives when searching for leaks. If the leaked memory doesn't accumulate over time, your worst case scenario is that your program is leaking a total of 80 bytes. Leaks that grow over time are what you should be concerned about.


Is segmentControl meant to be released?


myArray's retain count is still one after this section of code. When you add it to the initWithItems to create the segmentControl, it now has a reference to the object.

Is that possibly the leak?


Is segmentControl a property? Do you nil it in viewDidUnload?


No alloc is required when you create the array.

NSArray *myArray = [[NSArray alloc] initWithObjects: @"Add", @"Edit", nil];

Use :

+ (id)arrayWithObjects:(id)firstObj, ...

Try it this way, this also doesn't needs release.

0

精彩评论

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

关注公众号