简体   繁体   中英

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 something 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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM