views:

62

answers:

1

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

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


-(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.

+1  A: 

Yes, this isn't a good way to release objects. By releasing object A multiple times in your dealloc method (if it's retain count > 1), you are potentially pulling the rug out from under any other object that might be using an object.

You should only be releasing the object once (assuming you only retain the object once or you alloc it yourself) if you own it.

For more information on "owning" objects, I recommend you read Apple's Memory Management Programming Guide at:

http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html

Using the Build and Analyze item in your XCode's Build menu should help find issues like this, once you understand the basics of Objective-C Memory Management

Typically, a dealloc method should look like this:

-(void) dealloc
{
    [self.objectA release];
    [self.objectB release];

    [super dealloc];
}

edit: To answer your original question as to why you are getting EXC_BAD_ACCESS, there is not enough information here to determine why. One possible reason may be that you are deallocing an object in your sub class that also gets dealloc'ed in your super class's dealloc method. But that is just a shot in the dark

AlexC
@AlexBut, I read that how many times we alloc the memory we should dealloc that many times. So, I did it. I will go through the link you posted.Thank You.
srikanth rongali
Srikanth - yes, the object should be "released" as many times as it has been "retained" (or alloc'ed). However, simply releasing an object repeatedly defeats the point of reference counting - as you are essentially saying "I don't care how many times this is referenced, just free up the memory used by this object".Imagine a case where you have A, B and Z. A and B both reference Z. You create Z, then A, then B. So now Z's refcount is 2.Then say you release A, and cause it to get dealloced. It will then release Z twice, and Z will get dealloced. If B then tries to use Z, it will fail.
AlexC