6

I have Object:

MyClass *obj= [[MyClass alloc] init];

What's the difference between:

[obj release]; // Only obj own this object.

and:

obj = nil;

Does iOS deallocs obj when i set obj = nil?

I have a pointer, sometime i set it point to an object, sometime do not. So, when i want release a pointer i must check is it nil?

Steven Fisher
  • 44,462
  • 20
  • 138
  • 192
asedra_le
  • 3,079
  • 8
  • 38
  • 56

4 Answers4

13

This answer from the previous decade,

is now only of historic interest.

Today, you must use ARC.

Cheers


The very short answer is DO NOT just set it to nil. You must release it. Setting it to nil has no connection to releasing it. You must release it.

However it's worth remembering that if it is a property, then

self.obj = nil;

will in a fact release it for you. Of course, you must not forget the "self." part !!!!

Indeed,

self.obj = anyNewValue;

will indeed release the old memory for you, clean everything up magically and set it up with the new value. So, self.obj = nil is just a special case of that, it releases and cleanses everything and then just leaves it at nil.

So if anyone reading this is new and completely confused by memory,

  1. You must release it, [x release] before setting it to nil x=nil

  2. IF you are using a property, "don't forget the self. thingy"

  3. IF you are using a property, you can just say self.x=nil or indeed self.x=somethingNew and it will take care of releasing and all that other complicated annoying stuff.

  4. Eventually you will have to learn all the complicated stuff about release, autorelease, blah blah blah. But life is short, forget about it for now :-/

Hope it helps someone.

Again note, this post is now totally wrong. Use ARC.

Historic interest only.

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • The bit about properties being magically released is not entirely true. If you declare a property with the "retain" attribute you have to release it yourself, only if you declare it with the "copy" attribute it is released for you in the described manner. See http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html – dertoni Nov 18 '10 at 08:30
  • I think he may have meant "assign", not "retain". Also, wouldn't self.obj = someNewValue, trigger KVO listeners? – DJ Bouche Nov 18 '10 at 14:36
  • 1
    To clarify, self.obj = newValue DOES release the old value, if you're using properties correctly. I'd like to add that self.obj = newValue is identical to [self setObj:newValue], which is how the release occurs (the setObj method contains [oldValue release]). Also, if you're releasing the object, wouldn't you WANT KVO to be triggered? – andyvn22 Nov 18 '10 at 23:56
  • 2
    It's funny that with Swift, this answer is now doubly, trebly, antique! What a world... – Fattie Aug 11 '14 at 11:07
5

Is It Necessary to Set Pointers to nil in Objective-C After release?

Release, Dealloc, and the Self reference

Setting an object nil versus release+realloc

Read the above. They answer your Q comprehensively

Community
  • 1
  • 1
Bourne
  • 10,094
  • 5
  • 24
  • 51
5

iOS does not support garbage collection, which means that doing obj = nil would result in a memory leak. If you want automatic control over deallocation, you should do something like: obj = [[[NSObject alloc] init] autorelease] (you must NOT release it if you do that). Autorelease would cause the object to be automatically released when the current NSRunloop event ends. The NSRunloop automatically drains it's NSAutoReleasePool for each event iteration, which is usually very helpful.

Moshe Gottlieb
  • 3,963
  • 25
  • 41
  • I have a pointer, sometime i set i point to an object, sometime i do not do it. So, when i want release a pointer i must check is it nil? – asedra_le Nov 18 '10 at 08:33
  • 1
    Why do you have to check for nil on release? It's perfectly valid in objective-c to send a message to a nil pointer, it just does nothing. You should always initialize the pointer though, to nil or some other valid object. – ImHuntingWabbits Nov 18 '10 at 09:21
  • I don't understand why it's a good idea to check for nil before releasing. Sending release to nil is perfectly valid, and "real" non-beginner coders do it all the time. Why learn to avoid something that's accepted common practice? – andyvn22 Nov 18 '10 at 23:52
2

Setting an object nil will create a memory leak(if you are taking ownership by alloc,retain or copy) because we are not having a pointer to that particular memory location.

so if you want to dealloc an object you have to take out all ownership of an object before making it nil by calling release method.

[obj release];
obj=nil;
edorian
  • 38,542
  • 15
  • 125
  • 143