开发者

NSMutableArray - removeallobjects produces a leak

开发者 https://www.devze.com 2023-01-09 08:40 出处:网络
I have a mutableArray that I fill up with objects. When I try to refill the array, I first use removeAllObjects - which produces a memory leak...

I have a mutableArray that I fill up with objects. When I try to refill the array, I first use removeAllObjects - which produces a memory leak...

The properties of the object are synthesized, retained and released on dealloc.

The Array is initialized on viewDidLoad like this:

theArray = [[NSMutableArray alloc] initWithCapacity:10];

... and it's retained and synthesized. (@property (nonatomic, retain) NSMutableArray *theArray)

I'm adding the objects in a while-loop like this:

myObject *theObject = [[myObject alloc] init];

theObject.someProperty = @"theprop";

[theArray addObject: theObject];

[theObject re开发者_运维知识库lease];

then on the next call of the method, I remove all objects like this:

[theArray removeAllObjects];

That's where the leak occurs. If I comment this line out, the leak doesn't appear. So I guess I'm doing something wrong in my object?


Seems like the problem is solved...

a) I didn't realize, that when I use instruments, the app isn't compiled before launch - thus, some of the changes I made were not taking into effect, when using instruments. So now I first build and run after a change and then run it in instruments.

b) thus, I don't really know what solved the problem. But it might be that I had the dealloc-method in my object wrong. I was using:

[super dealloc];

[myProperty release];

instead of the other way around:

[myProperty release];
[super dealloc];

Thanks for the help, though!


Does myObject have any retained properties? If so, are you setting them to nil in the dealloc message? If not, when it is dealloced it won't release the objects that its properties are set to.

0

精彩评论

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