保留/分配内存管理
我正在尝试了解 iOS 上的内存管理。我创建了这个接口:
@interface Player : NSObject {
PlayerType pType;
PlayerWeapon pWeapon;
}
@property(nonatomic, readwrite, retain) pType;
@property(nonatomic, readwrite, retain) pWeapon;
@end
在实现文件中:
@synthesize pType;
@synthesize pWeapon;
在头文件中,我使用 retain
属性,因为 pType 和 pWeapon 不是标准 C 结构。据我了解,如果它们是 C 结构,我会使用 assign
代替。由于我使用了 retain
,这是否意味着此类保留该对象或实例化它的任何类?例如,如果我在另一个类中执行此操作:
Player *player = [[Player alloc] init];
这个新类是否必须调用 [player release]
还是该对象会自动释放?
I'm trying to understand memory management on iOS. I created this interface:
@interface Player : NSObject {
PlayerType pType;
PlayerWeapon pWeapon;
}
@property(nonatomic, readwrite, retain) pType;
@property(nonatomic, readwrite, retain) pWeapon;
@end
and this in the implementation file:
@synthesize pType;
@synthesize pWeapon;
In the header file, I use the retain
property because pType and pWeapon are not standard C structs. From what I understand, if they were C structs, I would use assign
instead. Since I've used retain
, does that mean this class retains the object or whichever class instantiates it? For example, if I do this in another class:
Player *player = [[Player alloc] init];
Does this new class have to call [player release]
or will the object automatically be released?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个好的通用规则是,无论您分配/初始化或复制什么,您都已经“创建”并拥有了它,因此您必须释放它。所以是的,拥有玩家的对象在使用完它后需要释放它。如果 Player 对象是为方法内的本地范围创建的,或者它是 ivar,则这适用。
但请记住,如果您决定创建自动释放的 Player 对象,则需要通过属性点语法或实际的保留消息来保留该对象,以防止 Player 对象在本地方法执行完成后自动释放。
或
祝你好运
A good, general rule is that whatever you alloc/init or copy you have "created" and own, therefore you will have to release it. So yes, the object that owns player will need to release it when it is done using it. This applies if the Player object is created just for a local scope within a method, or if it is an ivar.
Remember though, that if you ever decide to create an autoreleased Player object, you will need to retain the object either through the property dot syntax or an actual retain message to keep the Player object from being autoreleased after the local method has finished executing.
or
Good luck
当您“保留”属性时,编译器生成的 setter 方法负责确保正确释放和保留对象。该属性本质上处理释放它所引用的前一个对象的工作,并保留(取得所有权)所分配的对象。您还需要在实现文件中添加以下代码,以便在释放 Player 对象时释放这些对象:
这意味着即使内部属性被“保留”,当分配“Player”对象时,您仍然会拥有在某个时候释放它。
When you make a property "retained" the compiler generated setter method takes responsibility of making sure objects are release and retained correctly. This property essentially handles the work of releasing the previous object it was referencing and retains (takes ownership) of the object that was assigned. You will also need to add the following code in the implementation file to release these objects when the Player object is released:
This means that even though the internal properties are "retained," when a "Player" object is allocated, you will still have to release it at some point.
[[Player alloc] init]
的调用者负责向新的 Player 对象发送release
消息。 Player 对象的属性不会影响该责任。The caller of
[[Player alloc] init]
is responsible for sending the new Player object arelease
message. The Player object's properties don't affect that responsibility.