开发者

EXEC_BAD_ACCESS when i try to release a NSString that has an address allocated to it

I have a NSString that i try to release.

The problem is that in some cases i get EXC_BAD_ACCESS when i try to release it. The NSString has a address allocated to it (I can see in the bottom page that it has allocated memory at 0xABCDEF).

How can avoid this problem while realeasing when there is someth开发者_Python百科ing there ?


You can release an object, but the pointer to it can still have a value. It's just that it's a garbage value (i.e. a dangling pointer).

That's why you see a lot of code such as:

[myObject release];
myObject nil;

which first releases the object and then sets the pointer to nil. That way any message sent to it will fail silently (because it's safe to send messages to nil objects in Objective-C) rather than crashing, as seems to be happening with your app.

Since you are checking the pointer, I suspect that you are doing something weird with memory management. Don't. Just follow the Memory Management Rules and only release the objects you own.


You've likely called [release] on an already released string. How are you allocating it? When an object is released, or autoreleased, it is not automatically set to nil (0x0).

[[NSString alloc] initWithSomething] requires a release call.

[NSString stringWithSomething] does not, as by convention, it is autoreleased.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜