views:

95

answers:

4

Hi all,

I'm having a problem with my memory footage, it keeps on increasing even though I have properly released the objects in tight loop. The app will crash with "out of memory error" after some time... I've drilled down the problem to this:

/******************** Begin SimpleObject ***********/
//@interface SimpleObject : NSObject { 
//@public 
//    int iVarA, iVarB; 
//    int iVarC; 
//} 
//-(id) init; 
//-(void) dealloc; 
//@end 

//@implementation SimpleObject 
//- (id) init { return [super init]; } 
//- (void) dealloc { 
//    // NSLog ( @"SimpleObject dealloc" ); 
//    [super dealloc]; 
//} 
//@end 
/******************** End SimpleObject ***************/

NSAutoreleasePool *looppool = [[NSAutoreleasePool alloc] init];
for ( int i = 0; i < 1000; i++ ) {
    // This lead to the same problem
    // void *pVoid = malloc( 10000 );
    // free( pVoid );
    // End This lead to the same problem

 SimpleObject *obj = [[SimpleObject alloc] init];
 [obj release];
}
[looppool drain]; // whether or not I am using NSAutoreleasePool did not matter at all...

The memory might be fragmented, but shouldn't the OS deal with that problem? besides, there is nothing in between the allocations...

Any thoughts will be highly appreciated. Thanks!

A: 

You are correctly dealing the release of SimpleObj, no question about that.

That means the problem must be inside SimpleObj, look at the dealloc method and make sure you release everything in there.

willcodejavaforfood
Thanks! but there's nothing to release though, it just contains primitive data types. I have added a call to [super dealloc]; but the problem persist...
Zennichimaro
what if you put the pool inside the loop?
willcodejavaforfood
I'd still like to see inside SimpleObj. Is it a NSObject?
CiscoIPPhone
Yes, it is a NSObject. The full implementation is here:
Zennichimaro
/******************** Begin SimpleObject ***********/ @interface SimpleObject : NSObject { @public int iVarA, iVarB; int iVarC; } -(id) init; -(void) dealloc; @end /******************** End SimpleObject ***************/ /******************** Begin SimpleObject ***********/ @implementation SimpleObject - (id) init { return [super init]; } - (void) dealloc { // NSLog ( @"SimpleObject dealloc" ); [super dealloc]; } @end /******************** End SimpleObject ***************/
Zennichimaro
Dude, just update your post with the code instead of sticking it in a comment where most people will miss it
willcodejavaforfood
+2  A: 

The problem is in the implementation of SimpleObject. Please show us the code for the init and dealloc methods.

Make sure that everything you allocate in SimpleObject's init method gets released in its dealloc method. This applies to stuff you malloc and free too.

Make sure you return self from SimpleObject's init method.


Edit:

I've just compiled and run the code posted in the question with the following declaration for SimpleObject

@interface SimpleObject : NSObject
{
    char foo[10000];
}
@end

It runs fine with no leaks.


Edit 2:

Just seen the comment about this being an issue on the simulator. I could easily believe the simulator leaks. Try it on the device and see if the code still leaks.

JeremyP
His comment to my answer says that it is just primitive data types
willcodejavaforfood
@willcodejavaforfood: That's why I also advanced the possibility that he is not returning self from init. The code posted so far has no leaks in it, as you know.
JeremyP
@JeremyP - Just difficult to help people who already know best :)
willcodejavaforfood
@willcodejavaforfood: indeed
JeremyP
oh! I did not override init nor dealloc.. I thought a default one will be provided :) I will try your suggestion later as I'm not on my mac. Thanks a lot!
Zennichimaro
@Zennichimaro: a default one *is* provided.
JeremyP
I tried overriding them just now, but give the same result. The problem was NSZombieEnabled I had put earlier... Sorry for the trouble.. and thank you very much for your help!
Zennichimaro
+2  A: 

Are you, perchance, using the NSZombiedEnabled environment variable? That would explain the memory buildup, though it wouldn't explain why you'd see this problem using just malloc.

BJ Homer
yes, I remembered adding that! I'll check it later as I'm currently on my win machine, thanks a lot!
Zennichimaro
YES! THAT is the problem! I disabled it and everything turns fine including the malloc and free.. oh my gosh, I added it months earlier without understanding the implication clearly... Thanks a lot!
Zennichimaro
@Zennichimaro: if an answer resolves your question, click the checkbox next to it so that it is clearly marked as the answer.
BJ Homer
A: 

I created a SimpleObject class based on the interface in your question and ran the loop code you pasted. No crash. Allocations and Leaks both list everything as working fine.

Could you post ALL of the code for SimpleObject? That's where the problem is, so we need to see it in its entirety in order to help.

kevboh
It was just that, I thought [init] and [dealloc] will be inherited by default, so I did not provide any implementation. There was no other variables except the 3 integers..
Zennichimaro
The problem is with NSZombieEnabled environment variable I previously added. As BJ Horner mentioned earlier, it causes the memory build up... Thanks a lot for your help!
Zennichimaro