I make a object that use the camera to decode a bar code. If it was successful it send a delegate message to next action, if not, it only go out, and next screen start to work.
If am I in the last command of the object, can I send [self release] to free the memory? But, the problem is: the retainCount can be more than on one?
Like
ObjectToDealloc *test = [[ObjectToDealloc alloc] init];
[test doYourJob];
//can't release here, it broke my program
in the last thing that this object do, it run:
-(void)destroyYourself {
[self release];
[self dealloc];
}
This will work? I will release the memory? Or I have to make “test” in global visibility (not only function visibility) and do a:
[test release];
There are a couple of instances where I have seen classes that retain themselves in order to keep themselves around until they perform work. NEVER, EVER, EVER CALL DEALLOC*. Dealloc is not for you, but the runtime. If you no longer need an object, just appropriately release it. It is only the runtime’s duty to call that method.
Here’s an example of some code that I regularly use where the object retains itself and then appropriately releases itself when done with its work: UIAlertView+Blocks This code does this because the object is suppose to stick around, even after it has been released by the instantiating class so that it can run and finish the blocks it was given to execute.
*Except when calling your super’s dealloc within your own dealloc, but this goes without saying.