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

The Archive Base Latest Questions

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

Is mystring over-released? -(void)dealloc { [mystring release]; [mystring release]; [super dealloc]; } I assume

  • 0

Is mystring over-released?

-(void)dealloc {
    [mystring release];
    [mystring release];
    [super dealloc];
}

I assume this will not based on [nil release] does nothing:

-(void)dealloc {
    [mystring release];
    mystring = nil;
    [mystring release];
    [super dealloc];
}

-EDIT-
Let’s say I allocate mystring in init and release it in doSomething:

-(id)init {
    if (self = [super init]) {
        mystring = [[NSString string] retain];
    }
    return self;
}
-(void)doSomething {
    [mystring release]; // for some good reason
    // ...etc
}

Now to avoid over-releasing in dealloc based on my example above do I have to explicitly do this in the doSomething method?

-(void)doSomething {
    [mystring release];
    mystring = nil; // <- is this mandatory to avoid over-releasing in dealloc?
}

The big question is do I have to explicitly set it to nil when I release it somewhere else in the class to avoid over-releasing in dealloc?

So could I do as many releases as I want in dealloc based if I explicitly set to nil in doSomething?

-(void)dealloc {
    [mystring release];
    [mystring release]; // <- does nothing because mystring explicitly nil?
}
  • 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-15T00:28:52+00:00Added an answer on May 15, 2026 at 12:28 am

    For your first example, the answer is probably!

    In general, each object that holds a reference to another object should retain it until they’re done, then call release once, however in some highly exotic (and generally poorly written) cases this may not hold. If you retain the object x times, you need to release it x times, and other cases of poor memory management. But best practice, yes, one retain, one release, don’t release more than you retain!

    There are two risks with over-releasing like this:

    The first is if the first release call makes the refcount of mystring equal to 0, it will be dealloc’d, and then you’re sending a message to a piece of memory that is no longer a valid object. Objective-C doesn’t really like this, and may react in a variety of ways, including CRASHING. Sending messages to nil, kosher, messages to things dealloc’d, not so much.

    The second is if the refcount isn’t zero, you just released someone else’s reference, so at some point in the future, an object that has a reference to mystring may think that reference is valid and it won’t be because it was dealloc’d when the refcount hit zero on a subsequent release call. This will be harder to detect than the previous error, where a debugger will at least show you the real area the problem originates from in the stack frame trace.

    Your second example is correct – sending a message to nil does nothing. If you fear you’ll do things like this, be sure to set your variables to nil after releasing.

    EDIT: Yes. That’s what you should do if you intend to release in more than one place like that, however depending on the application, you might consider using an auto-release pool.

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

Sidebar

Ask A Question

Stats

  • Questions 467k
  • Answers 467k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Robert is correct, Windsor is disposing the session when it… May 16, 2026 at 1:57 am
  • Editorial Team
    Editorial Team added an answer Copy constructors and assignment operators are very important in C++… May 16, 2026 at 1:57 am
  • Editorial Team
    Editorial Team added an answer Yes, but they are not accessible, so looking at it… May 16, 2026 at 1:57 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.