简体   繁体   English

保留/分配内存管理

[英]Retain/assign memory management

I'm trying to understand memory management on iOS. 我正在尝试了解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. 在头文件中,我使用retain属性,因为pType和pWeapon不是标准的C结构。 From what I understand, if they were C structs, I would use assign instead. 根据我的理解,如果它们是C结构,我会使用assign Since I've used retain , does that mean this class retains the object or whichever class instantiates it? 既然我使用了retain ,这是否意味着这个类保留了对象或者哪个类实例化它? 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? 这个新类必须调用[player release]还是会自动释放该对象?

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. 如果仅为方法中的局部范围创建Player对象,或者如果它是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. 但请记住,如果您决定创建一个自动释放的Player对象,则需要通过属性点语法或实际的保留消息来保留对象,以防止Player对象在本地方法执行完毕后自动释放。

// Retaining an autoreleased object
self.player=[Player playerWithName: @"George"];

or 要么

player=[[Player playerWithName:  @"George"] retain]; 

Good luck 祝好运

When you make a property "retained" the compiler generated setter method takes responsibility of making sure objects are release and retained correctly. 当您使属性“保留”时,编译器生成的setter方法负责确保正确释放和保留对象。 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: 您还需要在实现文件中添加以下代码,以便在释放Player对象时释放这些对象:

- (void) dealloc
{
    [pType release];
    [pWeapon release];
    [super dealloc];
}

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”对象时,您仍然必须在某个时刻释放它。

The caller of [[Player alloc] init] is responsible for sending the new Player object a release message. [[Player alloc] init]的调用者负责向新的Player对象发送release消息。 The Player object's properties don't affect that responsibility. Player对象的属性不会影响该责任。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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