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

  • Home
  • SEARCH
  • 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 7129565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:15:22+00:00 2026-05-28T11:15:22+00:00

Let us say we have some code that looks like below: @interface SomeClass :

  • 0

Let us say we have some code that looks like below:

@interface SomeClass : NSObject
@property (nonatomic, retain) NSString *someString;
@end

@implementation SomeClass
@synthesize someString;
-(id)init {
    if (self=[super init]) {
        someString = [NSString stringWithString:@"some string"];
    }
    return self;
}
@end

Am I supposed to release the someString property in the dealloc method of SomeClass, even if someString was set to autorelease and I never actually retained it in my init method? If so, I’d simply add [someString release] before [super dealloc] in the -release method. Correct?

Now the real issue I am having is that while using Cocos2D, I’ve reached a contradicting situation. My code looks like below:

@interface SomeLayer : CCLayer
@property (nonatomic, retain) CCSprite *someSprite;
@end

@implementation SomeLayer
@synthesize someSprite;
-(id)init {
    if (self=[super init]) {
        someSprite = [CCSprite spriteWithFile:@"SomeFile.png"];
        [self addChild:someSprite];
    }
    return self;
}
@end

Now, I have added someSprite as a child to my layer SomeLayer. So, what should I do to make sure I have no memory leaks here? I could think of the following:

  1. Obviously, I’d think of calling [someSprite release] in SomeLayer‘s -dealloc method. but it gives me EXC_BAD_ACCESS in [super dealloc] (the next line). Most likely because I didn’t remove the child, and the superclass tries to access the child that I just released.
  2. I call [self removeChild:someSprite cleanup:YES] in the -dealloc method, which would remove the child and also release it. So I do not need to follow up with [someSprite release]. But hey, the -dealloc method of the superclass CCNode already does all that for me.
  3. I do nothing. I wouldn’t override the -dealloc method at all. Well, this seems to work fine, but it contradicts the statement: “if you retain something, you’re supposed to release it”.

Any help on why I must release the object in case I, and why not in case II at an early stage would help save a lot of memory related issues in the long run.

Thanks

  • 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-05-28T11:15:22+00:00Added an answer on May 28, 2026 at 11:15 am
    someString = [NSString stringWithString:@"some string"];
    

    This is wrong. You are keeping a pointer to an autoreleased object that will disappear soon, and when you’ll try to use the someString pointer bad things will happen. You should use the accessor ([self setSomeString:…]), retain the autoreleased value (someString = [… retain]) or use a method that returns a retained value (someString = [[NSString alloc] init…]).

    In your real use case you should do the same with the sprite, you are getting EXC_BAD_ACCESS because you over-release the sprite: you call release without ever retaining the value. Read the Cocoa Memory Management Guide, you will save yourself a lot of trouble in the long run.

    By the way, I think your main problem is that you think that a simple assignment to the someString variable retains the assigned value. That is not the case (not without ARC, to be more precise). Assignment to the instance variable is just that, a plain assignment. If you want to go through the accessors, you have to send a message ([self setSomeString:…]) or use the dot notation (self.someString = …).

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

Sidebar

Related Questions

Let's say I have some code (using CherryPy) that looks like this: import cherrypy
Let's say I have a line of code that looks like this: setInterval(ajaxFunction,3000); where
Let's say I have a function that looks like this: public void saveBooking(/* some
Let's say I have some code like this: <html> <head><title>Title</title></head> <body> <?php if (!$someCondition){
Let's say I have some code like this if(isset($_GET['foo'])) //do something if(isset($_GET['bar'])) //do something
Let's say I have some Java code: public class SomeClass { static { private
I have some code that looks like this: someFunc(value) { switch(value){ case 1: case
Let's say I have some code like this in AS3 for(...) thing = new
Let's say I have some code that is ordered, but the ordering is not
Let's say I have some XML like this <channel> <item> <title>This is title 1</title>

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.