开发者

'ptr = nil' vs '[ptr release]' in Objective-C?

开发者 https://www.devze.com 2023-03-18 04:37 出处:网络
I\'m new to Objective-C and having to allocate and deallocate memory. I find that sometimes when I try to use [ptr release] before reassigning a pointer, I end up with a SIGABRT later on, whereas if I

I'm new to Objective-C and having to allocate and deallocate memory. I find that sometimes when I try to use [ptr release] before reassigning a pointer, I end up with a SIGABRT later on, whereas if I use ptr = nil I don't, but I'm not sure if that actually deallocates what that pointer wa开发者_如何学Gos pointing to. So I'd like to know exactly what ptr = nil does, as opposed to [ptr release].


This might be a problem with a lack of understanding of Cocoa's memory management rules, specifically to do with object ownership. Depending on how you create an object, either you own it or something else does. Whether you need to release an object depends on whether you own the object (you've either retained it, or you've created it using a method beginning with alloc, new, copy, or mutableCopy). You can read more about the rules in detail in Apple's documentation on memory management.

In addition, you should pay attention to any rules on properties. As sergio mentioned, if a property specifies that it's retaining an object, it will handle calls to retain and release on assignment. So, for example, if you allocate something using alloc, you own that object. If you then assign it to a property that retains the object, you and the property are retaining the object, so depending on what you're doing afterward, you may also want to release the object to relinquish ownership of the object.

Setting a simple pointer to nil won't deallocate the object, nor will it count as a release. So, depending on how you create the object, you should know whether you need to release the object or not. The documentation linked above includes examples that should help you get up to speed on this as well. One caveat: this stuff sort of changes with automatic reference counting, since you don't do any handling of retain, release, etc. and the compiler takes over for the most part, but if you're not using ARC, don't worry about that just yet (but do worry about it later).


Using :

[ptr release];

you are actually decrementing the retainCount of ptr. If the retainCount for ptr drops down to 0, ptr will be deallocated and the memory too.

When you write ptr = nil;, you are just 'reseting' the memory address ptr is pointing to.


ptr = nil will make your ptr variable has a nil value and will not release your pointer; so the reason why it is working without SIGABRT is the object does not get dealloc'ed.

Assigning nil will also imply a release when you are assigning to a retain property:

@property(nonatomic, retain) .... ptr;
.....
self.ptr = nil;

It is good practice to follow a release statement with an assignment to nil:

[ptr release];
ptr = nil;

this will prevent any unwanted use of the deallocated object after release (this mostly is meaningful with class ivars that could be used in a different method).

If you could provide some code showing the SIGABRT, it would be easier to be spot on.

0

精彩评论

暂无评论...
验证码 换一张
取 消