开发者

EXC_BAD_ACCESS error while accessing valid, non-released object

I have a weird issue that comes up while releasing an object. In the object's dealloc method I am releasing another (sub) object. When this sub object is released I get an EXC_BAD_ACCESS error. I am pretty sure the sub-object is a valid pointer right before I call release on it.

This is how I've confirmed the weirdness - I set a break point inside the dealloc method, right before the release o开发者_JS百科f the sub-object, and it is allocated! I can send it messages and it responds correctly. Another weird bug is that, if NSZombieEnabled is set to YES, I dont get an error at all, not even NSZombie's usual error that says I've sent a message to a deallocated object, the code runs properly.

Does anybody have any idea how to solve this?

Thanks in advance!


* EDIT 1 *

I've found out that if I place the [super dealloc] in the end of the dealloc, it crashes, but if I place it at start, it won't. I've had something similar before, only the other way - app crashed if I [super dealloc] in start of my dealloc, and didn't at the end.

Why does it make a difference and when should you use what?


* EDIT 2 *

Scratch Edit #1! It doesn't work either way.


Do your dealloc routine happen to look like this:

- (void)dealloc
{
   [super dealloc];
   [otherObject release];
}

if so, change the order. If you call [super dealloc], your object gets vanished from memory, trying to acces otherObject later will cause you to access non-allocated memory -> EXC_BAD_ACCESS.


Without seeing how the object is initialized, will be hard for anyone to know for sure.

MyObject *someObject = [MyObject alloc] init...]; // Make sure you aren't autoreleasing here
// do some stuff with someObject

[someObject release];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜