Zennichimaro
Zennichimaro

Reputation: 5306

IPhone Leaking Memory in tight loop

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!

Upvotes: 1

Views: 251

Answers (4)

JeremyP
JeremyP

Reputation: 86671

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.

Upvotes: 2

kevboh
kevboh

Reputation: 5245

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.

Upvotes: 0

BJ Homer
BJ Homer

Reputation: 49054

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.

Upvotes: 3

willcodejavaforfood
willcodejavaforfood

Reputation: 44093

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.

Upvotes: 0

Related Questions