IOS memory management. dealloc vs nil?

Regarding this post: iPhone - dealloc - Release vs. noil

1) [foo release]; 2) self.bar = nil; 

The explanation was as follows:

  • Releases an object by accessing it through the instance variable panel. The instance variable will become a dangling pointer. This is the preferred method in dealloc.

  • Assigns the nil property bar to itself, which in practice frees up anything that currently saves the property. Do this if you have a custom parameter for the property, which should clear more than just an instance variable that supports the property.

Can someone clarify explanation # 1? Access through the instance variable panel?

For example, I set private var to the title of my object like this:

 SomeObject *oPointer; 

I do not use the setter property with this pointer in the header file, and when creating an instance of the object, it synthesizes NOT .

In my code, under certain conditions, I then have to select and assign this pointer to this object.

 obj = [[SomeObject alloc] initWith....]; 

So now this is done through the obj instance variable. I have a UIButton that is configured to RESET this object that it attached disables it. I do this through:

 [obj release]; obj = nil; 

After all that explains the question, why do I also have to declare obj = nil? Calling [obj release] also seems to kill the pointer. I thought that [obj release] would free the memory that it points to and also set obj to nil all in one shot, but it seems to also kill the pointer because my application crashes when it tries to reference obj after [ obj release];

Does this question make sense? Does this just explain that [obj release] does ALL cleanup, including killing a pointer, and I need to know about it?

If I set the hold property for the SomeObject pointer, will the pointer remain after release?

Thanks in advance!

+3
source share
4 answers

The calling release will decrease the reference count for obj. If the reference count is 0, it will be freed. The obj pointer still points to the same memory location, but access to it is likely to cause the program to crash. Installing obj on nil is not strictly necessary, but reinforces the idea that obj is no longer valid. This can be useful when debugging or later in the program if you want to additionally create an obj object and use "if (obj! = Nil)" to check if it has already been created.

If you set a property on it as a property, then when it is called as "self.obj = someObj", Objective-C will add it to the reference counter someObj. You should never call, preserving yourself on what is created through alloc-init, since alloc-init already sets the reference count to 1. If you saved this object, then when it was released, the reference count will only go back to 1, and it will become a memory leak.

+3
source

Nile is preferred for two reasons:

  • nil safe to call methods, while the link that was issued is not.

If this property is retain , self.thinger = nil; will also call release. If not, it will not.

  • If you use the retain or assign properties, this is the code that you want to keep DRY, which means you donโ€™t want to switch to the tag other than the assign/retain tag and execute. Using release in your deallocs should be kept in sync with properties.

If you strictly adhere to autorelease , you almost never need to call release yourself, except for the custom settings for retain properties.

Check out the original NARC article. Car advertising is your friend.

+4
source

[obj release]; Reduces the number of times the object is obj held and potentially causes it to be freed. This does not change the obj pointer, which continues to point to some place in memory.

obj = nil; Sets the pointer obj to nil. This is recommended but not required. If you do not, but continue to use the obj pointer, you will probably accidentally try to access the memory location that was used to store the object you released above. If this object was freed, it will lead to a crash or at least inadvertent behavior. By setting obj to nil, you will ensure that any use of obj in the future will send messages to zero with the normal correct behavior for this.

+3
source

Releasing an object still points to the same object. I always do both using viewDidLoad and applicationDidFinishLaunching

0
source

All Articles