[super dealloc] Program received signal: “EXC_BAD_ACCESS”.

Discussion in 'iOS Programming' started by srikanthrongali, Jun 22, 2010.

  1. srikanthrongali macrumors newbie

    Joined:
    Apr 30, 2010
    #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

    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.
     
  2. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #2
    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.
     
  3. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #3
    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).
     
  4. srikanthrongali thread starter macrumors newbie

    Joined:
    Apr 30, 2010
    #4
    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.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Not in the dealloc. In this case, you want to refer to the ivar and not the property.
     
  6. seepel macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #6
    Is there a specific reason you are using assign and not retain? I find retain makes life a lot simpler.
     
  7. cleexiang macrumors newbie

    Joined:
    Jun 24, 2010
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Huh? :confused:
     
  9. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #9
    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.
     
  10. CodeSpyder macrumors 6502a

    CodeSpyder

    Joined:
    Jun 23, 2010
    Location:
    Orlando, FL
    #10
    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.
     
  11. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #11
    This is not correct and is the reason why you should never rely on retain counts.
     
  12. CodeSpyder macrumors 6502a

    CodeSpyder

    Joined:
    Jun 23, 2010
    Location:
    Orlando, FL
    #12
    Excuse me. If you read what I wrote previously, you will see that I said that he should NOT have to check retain counts.
     
  13. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #13
    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).
     
  14. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #14
    This pattern is quite common, and valid also in the dealloc method.

    Code:
    self.objectName = nil;
     
  15. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #15
    Valid but not advised. It's better to just release the variable directly.
     

Share This Page