开发者

Do I need to release id's in Obj-C?

开发者 https://www.devze.com 2023-03-08 06:04 出处:网络
Example: MyClass *funkStation = [[MyClass alloc] init]; [funkStation dance]; id tmp = funkStation; ... [funckStation release开发者_如何学JAVA];

Example:

MyClass *funkStation = [[MyClass alloc] init];
[funkStation dance];
id tmp = funkStation;
...
[funckStation release开发者_如何学JAVA];

I know that after I'm done with the funkStation object I need to release it, but what about the id tmp? Am thinking that it's not a copy of the original object, but just a pointer to the memory space.


That is correct. You only release funkStation, but not tmp.

tmp should only be released if you gave it a retained pointer or a copy of the original object:

id tmp = [funkStation retain];


tmp and funkstation refer to the same object. You only need to release this object once. Sending release to funkstation and tmp do the same thing.


If you allocate an object explicitly, with alloc, you need to release it. Same with copy.

ID is a pointer, so when you assign it, it only assign the pointer value, so you don't need to release both variables, as they will both reference the same object. If you do so, you'll certainly get a segfault.

0

精彩评论

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