Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if an object is released?

I need to be able to check if I have already released a variable in objective-c. I tried check if it changed to null:

//Checks if buildview is null and returns respective output
if(BuildView == NULL)
    NSLog(@"Build View Value (pointer): Null");
else
    NSLog(@"Build View Value (pointer): Not Null");

//allocates memory and initalizes value
BuildView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];

//Checks if buildview is null and returns respective output again
if(BuildView == NULL)
    NSLog(@"Build View Value (pointer): Null");
else
    NSLog(@"Build View Value (pointer): Not Null");

//Releases the view
[BuildView release];

//Checks if buildview is null and returns respective output again
if(BuildView == NULL)
    NSLog(@"Build View Value (pointer): Null");
else
    NSLog(@"Build View Value (pointer): Not Null");

The results were:

Build View Value (pointer): Null
Build View Value (pointer): Not Null
Build View Value (pointer): Not Null

Is there any easier way to check if it is deallocated?

like image 217
rdelfin Avatar asked Oct 17 '11 18:10

rdelfin


2 Answers

You probably mean deallocated (destroyed), not released. Being released does not mean being deallocated, that's the point of reference counted memory management. Being released is not a state, you can't check for it; being destroyed is.

If you mean deallocated, then no, there is none. It is called weak reference, and Objective-C does not have them for reference counting. When an object is deallocated, nothing is automatically done to pointers to it; they become dangling pointers.

One technique is to have the object send a notification during deallocation, so that everything that holds a pointer can reset it to nil.

Generally, you must design your program in a way that no object pointer is used again after you called release on it. In the sample code you've given, you must not use BuildView again for anything else except assigning a new value.

like image 130
hamstergene Avatar answered Oct 20 '22 06:10

hamstergene


Usually, you shouldn't need to check if a pointer points to a deallocated object: You should know :) Your variables just hold a memory address. If the contents of the memory that the variable is pointing to is deallocated, the value of your variable (that holds the address) will not magically be set to nil or NULL. So you should rather rethink your design if you find it necessary to check whether a pointer might point to an address space that already has been released/deallocated.

During development time, you can do things like activate NSZombies or use Instruments to find out where objects are being allocated or deallocated.

Update 6/26/2015: When you are using weak pointers on OS X 10.7 and above and on iOS 5 and above, they will automatically be set to nil when the referenced object is released. See https://en.wikipedia.org/wiki/Automatic_Reference_Counting#Zeroing_Weak_References

like image 43
Johannes Fahrenkrug Avatar answered Oct 20 '22 07:10

Johannes Fahrenkrug