简体   繁体   English

保留财产被释放

[英]Retained property being deallocated

I am fairly new with Objective-C memory management and although I thought I understood it, I have a problem that I cannot manage to solve. 我对Objective-C内存管理还很陌生,尽管我以为我理解了它,但是我有一个无法解决的问题。

I have this property: 我有这个财产:

@property (nonatomic, retain) NSDate *dateDisplayed;

that I assign in my viewDidLoad with a custom method: 我使用自定义方法在viewDidLoad中分配的内容:

self.dateDisplayed = [self dbDateFormatToNsDate:@"15/11/2011"];

My dbDateFormatToNsDate method looks like this: 我的dbDateFormatToNsDate方法看起来像这样:

- (NSDate *) dbDateFormatToNsDate:(NSString *) date {
    NSDateFormatter *d = [[NSDateFormatter alloc] init];
    [d setDateFormat:@"dd/MM/yyyy"];
    NSDate *toReturn = [d dateFromString:date];
    [d release];
    return toReturn;
}

So it returns an autoreleased object (if NSDate follows the convention). 因此,它将返回一个自动释放的对象(如果NSDate遵循约定)。 But when I get out from viewDidLoad in another function trying to read dateDisplayed : 但是当我从另一个试图读取dateDisplayed函数的viewDidLoad中退出时:

[dateDisplayed isEqualToDate:[self dbDateFormatToNsDate:@"15/11/2011"]]

I get an NSZombie exception. 我收到一个NSZombie异常。 Thanks for any help! 谢谢你的帮助!

When assigning using self.property the property is retained because the setter methos is called but when just assigning without using self. 当使用self.property进行分配时,该属性将保留,因为调用了setter方法,但是仅在不使用self.情况下进行分配self. it isnt. 它不是。 Assuming of course that you have retain in the propery definition of the .h file. 当然,假设您retain了.h文件的属性定义。


You could [d autorelease]; 您可以[d autorelease]; instead. 代替。 I might be totaly off on this, but the toReturn NSDate might need to keep the formatter around even after youve released it, causing the bad access: 我可能对此toReturn ,但是toReturn NSDate可能即使在发布它之后也需要保留格式化程序,从而导致错误的访问:

Try: 尝试:

- (NSDate *) dbDateFormatToNsDate:(NSString *) date {
    NSDateFormatter *d = [[NSDateFormatter alloc] init];
    [d setDateFormat:@"dd/MM/yyyy"];
    NSDate *toReturn = [d dateFromString:date];
    [d autorelease];
    return toReturn;
}

Since you are returning an object created by a method that does not start with alloc, copy, mutableCopy, convention says you should autorelease it. 由于返回的对象不是以alloc,copy,mutableCopy开头的方法创建的,因此约定应该自动释放该对象。

Autorelease means that it will be release in the future. 自动发布意味着它将在将来发布。 If the caller of the method needs it to stick around, then they will retain it. 如果方法的调用者需要它坚持下去,那么他们将保留它。

Read the memory management guide: 阅读内存管理指南:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

Here's the rules: 规则如下:

1 & 3 apply to that method. 1&3适用于该方法。 2 applies to a calling class that may need to hold it. 2适用于可能需要保留它的调用类。

1 - You own any object you create You create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy). 1-您拥有自己创建的任何对象使用名称以“ alloc”,“ new”,“ copy”或“ mutableCopy”开头的方法(例如alloc,newObject或mutableCopy)创建对象。

2 - You can take ownership of an object using retain A received object is normally guaranteed to remain valid within the method it was received in, and that method may also safely return the object to its invoker. 2-您可以使用保持对对象的所有权使用接收到的对象通常可以保证在接收该对象的方法中保持有效,并且该方法还可以安全地将该对象返回给其调用者。 You use retain in two situations: (1) In the implementation of an accessor method or an init method, to take ownership of an object you want to store as a property value; 在以下两种情况下使用保留:​​(1)在访问器方法或init方法的实现中,将要存储的对象的所有权作为属性值; and (2) To prevent an object from being invalidated as a side-effect of some other operation (as explained in “Avoid Causing Deallocation of Objects You're Using”). (2)防止对象因其他操作的副作用而失效(如“避免引起正在使用的对象的重新分配”中所述)。

3 - When you no longer need it, you must relinquish ownership of an object you own You relinquish ownership of an object by sending it a release message or an autorelease message. 3-不再需要它时,必须放弃对自己拥有的对象的所有权通过发送释放消息或自动释放消息来放弃对象的所有权。 In Cocoa terminology, relinquishing ownership of an object is therefore typically referred to as “releasing” an object. 因此,在可可术语中,放弃对象的所有权通常称为“释放”对象。

4 - You must not relinquish ownership of an object you do not own This is just corollary of the previous policy rules, stated explicitly. 4-不得放弃不拥有的对象的所有权这只是以前的策略规则的推论,明确规定。

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

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