开发者

In objective-c , if an object is referenced more than once, would it be possible to release it?

开发者 https://www.devze.com 2023-03-01 06:51 出处:网络
For example; TheClass * aInstance = [[TheClass alloc] init]; TheClass * bInstanc开发者_运维知识库e;

For example ;

TheClass * aInstance = [[TheClass alloc] init];
TheClass * bInstanc开发者_运维知识库e;
bInstance = aInstance;
[bInstance release]

Would the memory allocated by aInstance be freed in this case?


Yes, it would be, because there's only one instance-- you just have two different pointers to it.


As in plain C, the assignment of the address (the pointer) for some object to different variables has no bearing on the actual memory allocation used for that object. In Objective-C, the same concept applies. If you want to "keep" two different references to one object, you'll need to have both references "own" it. In this case, that would be by having the second reference retain the object, likely because you're storing it in an instance variable, etc.


Yes.

It sounds like you're a bit confused about what's going on, so I'll explain a couple things:

  1. The memory was not allocated by aInstance. That's simply a spot in memory that holds a number. The memory was actually allocated by the TheClass class.
  2. The allocation method allocated a chunk of memory in a totally seperate section of memory, and returned the location of that chunk as the result of the method.
  3. aInstance simply holds that location (which is a big number). That's it. This number is interpreted as a pointer (ie, a reference to another location in memory), but it doesn't have to be. You could use it as an int, if you're feeling adventurous.
  4. When you do: bInstance = aInstance;, you're simply copying that number from one memory slot to another. You're not doing anything to the object reference by that variable. You're just duplicating an already existing pointer.
  5. When you release the object, it will be deallocated, because only one thing owns it (you, by invoking the allocation method).
  6. Deallocating an object does nothing to the things that were referencing it, so your variables will still contain the now-former address in memory of where the object used to live. In other words, they are "dangling" or "stale" pointers, and it's recommended that you zero them out (by assigning nil to them).

Whew, that's a lot of typing for an iPhone keyboard. :P


Yes, it would be freed and neither aInstance not bInstance should be used safely after the release.


TheClass * aInstance = [[TheClass alloc] init]; //retain count = 1

TheClass * bInstance; //this is a pointer so retain count remain 1
bInstance = aInstance; //the pointer refer to the instance (retain count =1)
[bInstance release] //release => retain count =0, object released


Yes it will be freed.

If u replace "bInstance = aInstance;" with:

bInstance = [aInstance retain];

it will not free the memory until you release it one more time. Though you might want to use autorelease for short term usage.


Yes. You only initialized it once, and only released it once. So the retain count is 0.

The rules are:

  • alloc sets the retain count to 1
  • Calling "retain" will increase retain count with 1
  • Callng "release" will decrease retain count with 1
  • If retaincount is after a release 0, object is removed.
0

精彩评论

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