Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

srikanthrongali

macrumors newbie
Original poster
Apr 30, 2010
21
0
Hi,
I am writing the code using cocos2d.
I want to release all the memory I allocated. I have done it in dealloc method in the following way. All the objects I released are declared in interface file and property (assign) was set and are synthesized in implementation file.
I used alloc method to create them like

Code:
self.PlayerA = [[CCSprite alloc] initWithFile:@" PlayerImage_01.png"];

Code:
-(void)dealloc
{
	int count , i ;

	count = [self.PlayerA retainCount];
	for(i = 0; i < count; i++)
		[self.PlayerA release];

	count = [self.targetLayer retainCount];
	for(i = 0; i < count; i++)
		[self.targetLayer release];

		count = [self.playerGunSlowDrawSheet retainCount];
	for(i = 0; i < count; i++)
		[self.playerGunSlowDrawSheet release];

	count = [self.playerGunSlowDrawAnimation retainCount];
	for(i = 0; i < count; i++)
		[self.playerGunSlowDrawAnimation release];

	count = [self.numberFinishedTime retainCount];
	for(i = 0; i < count; i++)
		[self.numberFinishedTime release];

	count = [self.backGroundImage retainCount];
	for(i = 0; i < count; i++)
		[self.backGroundImage release];

	[[CCTextureCache sharedTextureCache] removeAllTextures];
	[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];

	[super dealloc];
}
But I am getting: Program received signal: “EXC_BAD_ACCESS”.
I debugger it is showing the error at [super dealloc];

Am I totally wrong in the memory management ? Or am I missing anything in this.
Thank you.
 
- (NSUInteger)retainCount

[...]

Important: This method is typically of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.

http://developer.apple.com/mac/libr...ml#//apple_ref/occ/intfm/NSObject/retainCount

In any case, objects generally shouldn't be being retained more than once in a class.
 
Am I totally wrong in the memory management ?

Yes; there is almost always no need to check retain counts of objects and you certainly shouldn't be writing dealloc as you have done. All you should need to do is call release on your objects and then call super. You certainly shouldn't be calling release * an object's retain counts. Those retains probably mean something else owns that object and you're destroying it from under its feet.

The memory rules are quite simple but I suggest you re-read the Cocoa memory management guide. The rule of thumb is:

* If you create any instance of an object using alloc/init, copy or a factory/convenience method beginning with new, you should release it..
* If you retain (own) an object, you should release it (just once)
* If none of the above, assume the object is either auto-released or owned by something else that will take care of releasing it.

You should also avoid using property accessors (either directly or using dot-notation) in init and dealloc as they may have side-effects. Assign directly to your instance variables in init, and just call release on the instance variables in dealloc. If you are destroying an object elsewhere within your class, you can use your property accessor to set it to nil (which will also release it assuming your property is set to retain rather than assign).
 
You should also avoid using property accessors (either directly or using dot-notation) in init and dealloc as they may have side-effects. Assign directly to your instance variables in init, and just call release on the instance variables in dealloc. If you are destroying an object elsewhere within your class, you can use your property accessor to set it to nil (which will also release it assuming your property is set to retain rather than assign).

Is it not good to use self.object ?
I am declaring the objects and setting assign property in interface file maximum times. And using self.object every where in implementation file.
And releasing it in dealloc.
I think this way is not good.
Thank you.
 
Is it not good to use self.object ?
I am declaring the objects and setting assign property in interface file maximum times. And using self.object every where in implementation file.
And releasing it in dealloc.
I think this way is not good.
Thank you.

Is there a specific reason you are using assign and not retain? I find retain makes life a lot simpler.
 
This is an awesome example of not knowing nearly enough of the underlying language to be able to just jump in and start using it.
 
In your dealloc method, don't use

[self.objectName release]

Use

[objectName release]

And you should not have to check the retain count. You should know what it is, or should be, at this stage, 1.
 
Excuse me. If you read what I wrote previously, you will see that I said that he should NOT have to check retain counts.

You did, but you also said that the retain count should be 1 at that stage, which is not correct; I pointed this out as it serves as a very good reason *why* you should not rely on or check retain counts (i.e. you can never be sure what the retain count should be at any given point).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.