iPhone - sending message to deallocated instance - why? [closed]
I keep geting this error and I just don't understand why. I am new to this so maybe someone can point out the problem.
The error :
-[ShareXML release]: message sent to deallocated instance
The code:
if(self.share){
NSLog(@"SHARE ALREADY EXISTS");
[self.share startSomeProcess];
}else{
NSLog(@"share xml");
ShareXML *shareXML = [[ShareXML alloc] init];
self.share = shareXML;
self.share.delegate = self;
[self.share startSomeProcess];
NSLog(@"SHARE XML RELEASED");
[shareXML release];
}
Sh开发者_StackOverflow社区areXML is an NSObject. I use almost identical code on a view controller and it works. Thanks!
To find the place where the message to the deallocated instance is sent, set the environment variable NSZombieEnabled as described on CocoaDev.
For a very good chance of having the compiler find the error for you, do an analyzer build. It is in the Build menu, "Build and Analyze", or you can just tap shift-ctrl-A.
When you have pinpointed exactly where the problem is it will probably be obvious but you could then post some more code.
If you show a view controller, it is retained by the navigation controller / presenting parent view / tab bar controller. In this case your ShareXML object is not retained by anything. As @Sam Ritchie was indicating, if you declare the @property (nonatomic, retain) ShareXML *share;
that'll retain it when you set self.share
.
Make sure you release it in your dealloc method. My preferred method is self.share = nil;
Which releases whatever is currently retained by self.share, and sets it to nil.
Sounds like share property is not "retain". So in destructor, when share member is released, it is released for the second time because the same object shareXML is already released.
I found it! I was releasing the delegate in the ShareXML. Thanks for all the comments though, it made me check the rest of my code!
精彩评论