Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-25-2009, 04:04 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 10
BigBearStudios is on a distinguished road
Default Memory Leaks

Hey, i switched from C++ to Object C a few weeks back. I ran the XCode memory leak tool and its telling me im getting leaks, i may be mis-understanding some difference between C++ and OC.

The first leak is in the following small class:

Code:
@interface ZombiePhoneInputProcessor : NSObject {
@private
	ZombiePhoneWorld *p_World;
}

-(ZombiePhoneInputProcessor*) initWithWorldRef: (ZombiePhoneWorld*) world;
-(void) processInputPress: (CGPoint) pointPress;

@end

@implementation ZombiePhoneInputProcessor

-(ZombiePhoneInputProcessor*) initWithWorldRef: (ZombiePhoneWorld*) world {
	self = [super init];
	
    if ( self ) {		
        p_World = world;
    }
	
    return self;
}

@end
The leaks coming from this class but i dont see how. Im passing a pointer to an object and setting another object to point to this object.

Its called as follows:

Code:
// Initialise input processor
	m_pInput = [[ZombiePhoneInputProcessor alloc] initWithWorldRef: self];
	if( m_pInput == nil )
	{
		#ifdef DEBUG
		NSLog(@"Failed to initialise zombie input manager.\n");
		#endif
		
		return false;
	}
m_pInput is a pointer variable inside this class. Where am i going wrong?
BigBearStudios is offline   Reply With Quote
Old 06-25-2009, 04:26 AM   #2 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 10
BigBearStudios is on a distinguished road
Default

Quote:
Originally Posted by BigBearStudios View Post
Hey, i switched from C++ to Object C a few weeks back. I ran the XCode memory leak tool and its telling me im getting leaks, i may be mis-understanding some difference between C++ and OC.

The first leak is in the following small class:

Code:
@interface ZombiePhoneInputProcessor : NSObject {
@private
	ZombiePhoneWorld *p_World;
}

-(ZombiePhoneInputProcessor*) initWithWorldRef: (ZombiePhoneWorld*) world;
-(void) processInputPress: (CGPoint) pointPress;

@end

@implementation ZombiePhoneInputProcessor

-(ZombiePhoneInputProcessor*) initWithWorldRef: (ZombiePhoneWorld*) world {
	self = [super init];
	
    if ( self ) {		
        p_World = world;
    }
	
    return self;
}

@end
The leaks coming from this class but i dont see how. Im passing a pointer to an object and setting another object to point to this object.

Its called as follows:

Code:
// Initialise input processor
	m_pInput = [[ZombiePhoneInputProcessor alloc] initWithWorldRef: self];
	if( m_pInput == nil )
	{
		#ifdef DEBUG
		NSLog(@"Failed to initialise zombie input manager.\n");
		#endif
		
		return false;
	}
m_pInput is a pointer variable inside this class. Where am i going wrong?
Does the leak detetctor work properly? Its telling me theres leaks even when i just initialise a MutableArray like so:

m_entityArray = [[NSMutableArray alloc] initWithCapacity: (int)(MAX_WEAK_ZOMBIES + MAX_CHUNK_ZOMBIES )];

Im not adding anything after that, just reserving space.
BigBearStudios is offline   Reply With Quote
Old 06-25-2009, 09:50 AM   #3 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by BigBearStudios View Post
Does the leak detetctor work properly? Its telling me theres leaks even when i just initialise a MutableArray like so:

m_entityArray = [[NSMutableArray alloc] initWithCapacity: (int)(MAX_WEAK_ZOMBIES + MAX_CHUNK_ZOMBIES )];

Im not adding anything after that, just reserving space.
What you're describing makes sense to me. Maybe you don't fully understand what Instruments is telling you when it "reports" a leak? It is essentially saying: "I've found a chunk of memory that you have allocated, but nothing in your code is referencing (pointing to) this chunk, and here's the line of code that allocated the chunk." When you alloc/init an NSMutableArray, even though the array itself is empty, there is space (memory) allocated for the array container itself. That's what is leaking in this case, according to Instruments.

I suspect your leaks are occurring when you reassign variables that are currently pointing at something else, without first properly cleaning up the existing value. That's a classic coding "leak", and not unique to Obj-C, illustrated by this simple C code:
Code:
    unsigned char * buffer;

    buffer = malloc( 1024 * 1024 ); // allocate a buffer

    // do something with buffer

    buffer = malloc( 5 * 1024 * 1024 ); // allocate another buffer
    // Oops, just reassigned buffer without first cleaning it up. That's a 1MB leak.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 457
16 members and 441 guests
Domele, Duncan C, Feldspar, karatebasker, MacBook MH, Objective Zero, patapple, Paul Slocum, peterwilli, pipposanta, PixelInteractive, Punkjumper, rubyeim54, SLIC, taylor202, Today's Posts
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,694
Threads: 94,137
Posts: 402,950
Top Poster: BrianSlick (7,990)
Welcome to our newest member, peterwilli
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:02 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0