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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:31:04+00:00 2026-05-27T04:31:04+00:00

I copied this example from this topic. It’s better to release the ivar directly.

  • 0

I copied this example from this topic. It’s better to release the ivar directly.

It’s better to release the ivar directly. If a subclass overrides the
setter methods of a property, your object might leak because your
setter is not called.

@interface ClassA
@property (readwrite, retain) id anObject;
@end

@interface ClassB : ClassA
@end

@implementation ClassA
@synthesize anObject;

- (void)dealloc {
self.anObject = nil;

[super dealloc];
}
@end

@implementation ClassB
- (void)setAnObject: (id)anObject {
// do nothing!
}
@end

I don’t see any difference between [anObject release] and self.anObject = nil.
Because

self.anObject = nil

is equal to

[anObject release];
anObject=nil;

Why I don’t have memory leak with [anObject release] ?

  • 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-27T04:31:04+00:00Added an answer on May 27, 2026 at 4:31 am

    Because

    self.anObject = nil

    is equal to

    [anObject release];

    anObject=nil;

    This is incorrect, and is the source of your confusion.

    self.anObject = nil does NOT translate into a direct ivar access. It instead becomes

    [self setAnObject:nil];
    

    And since you’ve overridden the -setAnObject: method to do nothing, the underlying instance variable is never getting released, and you are thus leaking memory.

    Incidentally, this is exactly why you should avoid using setter methods inside your init and dealloc methods. Subclasses can override them to do unconventional things.

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

Sidebar

Related Questions

I copied this code from an example . I've read it 100 times. Array.prototype.map
I copied this example from perldoc -f sort . I added the @old array
I'm at a loss debugging this code. I copied the example from a guide,
This is copied example from: How to read custom config section in app.config in
I have copied this code from the official example... I just replaced the api
This is copied ditto from example given in Sql2008 R2 doc - Syntax: DATENAME
I copied this code from another post. I tried the example, however, I am
This example below illustrates how to prevent derived class from being copied. It's based
I copied this example from api.jquery.com $('.target').change(function() { alert('Handler for .change() called.'); }); I
I copied this code from the libjpeg example and im passing it standard files;

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.