Creating and releasing objects in the same method, while using self as delegate
- by user200341
In objective-c you are responsible for releasing objects you allocate, but what happens when you allocate an object in a method, assign self as the objects delegate, and then release the object.
The callbacks from the newly created (and released) object fails at this point, or rather, doesn't happen.
- (void)doSomething
{
MyObj *myObj = [[MyObj alloc] init];
myObj.delegate = self;
[myObj performOperation];
[myObj release];
}
- (void)callbackMethodFromMyObj:(NSString *)message
{
NSLog(message);
}
I can't release the object until the callback has occurred, and I can't avoid releasing the object in the same method that creates it (because it exists outside the scope).
One way of doing it would be to pass the object along in the call-back and release it in the callback, but is this the right way to go about this?