开发者

Verifying Object Release

开发者 https://www.devze.com 2023-03-13 03:30 出处:网络
In my app at a certain point I\'m releasing on of the objects I created using: [myObject release]; I have a feeling something is not really working over there (I have a bug which I cannot catch, an

In my app at a certain point I'm releasing on of the objects I created using:

[myObject release];

I have a feeling something is not really working over there (I have a bug which I cannot catch, and I have an assumptio开发者_Python百科n it has something to do with the memory allocation). I'm trying to look it overt in the Debugger and it seems that the object still have the same values after the "release" line was running.

Am I missing anything?

Is the memory still allocated and being dismissed someplace else? If so, where?


When memory is being released, it’s usually not zeroed out. That means that objects appears to keep their status quo even after deallocated – but you can’t use them anymore, since the memory contents could be reused and overwritten by something else at any moment.

One common simple trick is adding a logging statement to dealloc:

- (void) dealloc
{
    NSLog(@"Say bye to %@, kids!", self);
    [super dealloc];
}

This is so simple that it can’t go wrong. Which is a good thing when you’re debugging and want to be 100 % sure about something.

Another nice tool is zombies. When you set a special environment variable, the machine will guard access to all objects and will scream when you try to access a deallocated one. It’s also quite a dependable tool.

And then there’s retainCount. It’s probably quite close to what you are looking for, but it’s not very dependable, as there are many things going on in the background. You should only use it if you know what you’re doing.


Instead of releasing it try,

myObject=NULL;

NSLog(@"Retain count of myObject is %d", [myObject retainCount]); // Since retainCount returns an integer, we use %d to display it 

You will see that its retaincount is 0.

You can also check myObject value

NSLog(@"myObject Value after NULL = %@", myObject); 

myObject Value after NULL = (null)

0

精彩评论

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

关注公众号