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 7883335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:30:15+00:00 2026-06-03T04:30:15+00:00

I have cocos2d class which produces memory leak if I do not release autoreleased

  • 0

I have cocos2d class which produces memory leak if I do not release autoreleased object (self.graphicsContainer). Code:

@property (nonatomic, retain) CCNode * graphicsContainer; // I create property

@synthesize graphicsContainer = _graphicsContainer; // I synthesize it

-(id)init 
{
    if ((self = [super init])) {
        self.graphicsContainer = [CCNode node]; // which returns autoreleased object!
    }
    return self;
}

-(void) dealloc 
{
    [self.graphicsContainer release]; // If I do not release it I get memory leak warning!
    [super dealloc];
}

Anyone knows why I have to release it? As far as I know I shouldn’t release autoreleased objects?

  • 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-03T04:30:16+00:00Added an answer on June 3, 2026 at 4:30 am

    That’s exactly the behavior I’d expect from that code. Because graphicsContainer is a retained property, the line

    self.graphicsContainer = [CCNode node]; // which returns autoreleased object!
    

    …will retain the the node, so you need to release it in dealloc as you’re doing. There’s basically nothing wrong with what you’re doing there.

    One caveat: it’s perhaps controversial, but for various reasons, it’s generally frowned upon to use property accessors in init and dealloc. This SO question has more information on that (even better: read Mike Ash’s excellent analysis of the subject).

    If you fall into the ‘property accessors in init/dealloc is bad’ camp (I do), you could change it to this:

    -(id)init 
    {
        if ((self = [super init])) {
            _graphicsContainer = [[CCNode node] retain]; // which returns autoreleased object!
        }
        return self;
    }
    
    -(void) dealloc 
    {
        [_graphicsContainer release];
        [super dealloc];
    }
    

    The code above is exactly equivalent to what you’re doing, except that it’s not calling the property accessors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have this code in my UIViewController: //Cocos2D methods -(id) init { if((self=[super
I have written my code in following way in cocos2d. id actionTo = [CCFadeOut
Scenario: I have run and compiled an existing c++ code of cocos2d-x on android
I'm using cocos2d with objective C. I have a class called CrystalineBubble that is
I have a class Hero, subclassed from CCSpriteBatchNode (cocos2d): @interface Hero : CCSpriteBatchNode {...}
I have game application (cocos2d) in which, a ball is to be shooted from
i'm using cocos2d, however the question is general. i have a class CCNode *scaleLayer
I have a class called GameScene, with is a subclass of a cocos2d Scene.
I have a Cocos2D game I want to install on my devices with Testflight.
Currently I am using xCode 3.2.5 and I have already installed cocos2d template cocos2d

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.