How can I check if an object is released?

13,237

Solution 1

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.

Solution 2

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

Share:
13,237

Related videos on Youtube

rdelfin
Author by

rdelfin

Updated on June 04, 2022

Comments

  • rdelfin
    rdelfin about 2 years

    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?

  • hamstergene
    hamstergene over 12 years
    calling retainCount on destroyed object will give you crash or other undefined behavior, not zero.
  • Chuck
    Chuck over 12 years
    In fact, the most likely result in that particular code sample is that retainCount would return 1 even when it was deallocated!
  • Richard J. Ross III
    Richard J. Ross III almost 12 years
    It should be noted, that for historical purposes, this answer is no longer correct. Objective-C has weak references, and in fact, it always did, with enough runtime hacking. However, for the official version, they require iOS 5 and greater.