Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8825809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:58:12+00:00 2026-06-14T06:58:12+00:00

I have code like this: MyClass.h @interface MyClass : CCLayer { } @property (nonatomic,

  • 0

I have code like this:

MyClass.h

@interface MyClass : CCLayer {

}

@property (nonatomic, retain) CCSprite *spriteName;  //retain count = 1
@property (nonatomic, retain) CustomClass *customVariable; //retain count = 1

@end

MyClass.m

@implementation MyClass
@synthesize spriteName;
@synthesize customVariable;

//rough init method
-(void)init 
{
    self.spriteName = [CCSprite spriteWithFileName:@"a.png"]; //retain count = same
    self.customVariable = [[CustomClass alloc] init]; //retain count = 2
}

-(void)dealloc
{
    [self.spriteName release]; //retain count = 0
    self.spriteName = nil;

    [self.customVariable release]; //retain count = 1?
    self.customVariable = nil;

    [super dealloc];
}

I have some questions about this:

1) I have a CCSprite which I’ve made into a property, however when I assigned it to [CCSprite spriteWithFileName:@"a.png"], it is an autoreleased object. But because I have @property (nonatomic, retain) CCSprite *spriteName, I have to release it yes?

2) For my customVariable, I seem to get an error when I release it in the manner above, but when I changed them to underscores, [_customVariable release]; _customVariable = nil, I get no error. Why is this and what is the difference between the two?

3) Am I releasing these objects right? I have commented in the retain count which I think I’m having trouble understanding. I know the basic if there’s an alloc or retain then I should +1 but I’m confused when it comes to properties.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:58:13+00:00Added an answer on June 14, 2026 at 6:58 am

    So the full deal with these iVars goes as follows. The compiler silently treats your code like this:

    @interface MyClass : CCLayer {
        CCSprite *_spriteName;
    }
    
    @property (nonatomic,readwrite, retain) CCSprite *spriteName;  //retain count = 1
    
    @end
    
    @implementation MyClass
    @synthesize spriteName=_spriteName;
    
    @end
    

    in your code, the ‘readwrite’ and the iVar’s name are implicit (_spriteName), and the compiler generates the appropriate code. You could however have an iVar with a different name to point to the actual CCSprite. Using the self.spriteName semantic, the compiler converts that (auto-generates) the code under the hood. In you example the compiler has generated a setter and a getter for ‘self.spriteName’ (synthesized) as follows:

    -(void) setSpriteName:(CCSprite*) theSpriteName {
       // this setter can be quite tricky depending on the @property clause 
       // but for this example, a simplified setter :
    
        [_spriteName autorelease];  
        _spriteName = [theSpriteName retain]; 
    
    }
    -(CCSprite*) spriteName {
        return _spriteName;
    }
    

    so when you use self.spriteName , depending on the context (lhs or rhs), the appropriate ‘hidden’ routine is called. So the line

    self.spriteName = [CCSprite spriteWithFileName:@"a.png"];
    

    actually invokes the generated setter above (setSpriteName). Conversely

    CCSprite *tempSprite = self.spriteName;
    

    invokes the generated getter. You are always free to use the ‘underlying’ iVar.

    CCSprite *otherTempSprite = _spriteName; // exactly the same.
    

    be very cautious however about changing _spriteName’s content or altering its life cycle, as you could accidentally create either a leak or a zombie, until you fully understand the internals. In dealloc, use

    [_spriteName release]; // in dealloc, release immediately.
    

    or

    self.spriteName=nil;   // will invoke the setter above, and the 
                           //   actual release could be delayed
    

    So finally, back to your problem with customVariable. Your code actually becomes

    [_customVariable release];  // retain count is 0 !!! 
    [self setCustomVariable:nil]; 
    
    which when called will execute :
    
    [_customVariable autorelease]; // bad access : message sent to a deallocated variable 
    _customVariable=nil;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have code like this: string s = MyClass.GetString(); // Returns string containing
In my code, I have something that looks like this: @implementation MyClass - (id)
I have code like this in my view model: function ChatListViewModel(chats) { var self
I have code like this - (IBAction)onClick:(id)sender { NSThread *thread = [[NSThread alloc]initWithTarget:parser selector:@selector(adapter:)
I have code like this var MyObj = { f1 : function(o){ o.onmousedown =
I have code like this: ... entry.KeyPressEvent += EntryKeyPressEvent; ... } void EntryKeyPressEvent(object o,
I have code like this: string uriString = @C:\Temp\test.html; Uri uri = new Uri(uriString);
I have code like this: MediaElement me = myPlayer.MediaElement; WriteableBitmap wb = new WriteableBitmap(me.NaturalVideoWidth,
I have code like this (simplified): def outer(): ctr = 0 def inner(): ctr
I have code like this: <ul> <?php foreach($persons as $key) : ?> <?php if($this->session->userdata('id_user')

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.