views:

111

answers:

2

Hi Forum

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 release];

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?

+1  A: 

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.

Lou Franco
yes - the properties in the object are retained.And I'm using release in the dealloc.h@property (nonatomic, retain) NSString *someProperty;.m@synthesize someProperty;-(void)dealloc{[someProperty release];[super dealloc];}
Urs
A: 

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!

Urs