开发者

Objective C - Core Data memory management?

1- Is the开发者_Python百科se anything wrong with the way i am deallocing the object?

2- Does my dealloc override the dealloc for NSManagedObject?

3- Do i need to dealloc super even though my object is an NSManagedObject type and core data ia responsible for it?

@interface MyClass : NSManagedObject

@property (nonatomic, retain) NSString *coreDataString;
@property (nonatomic, retain) NSNumber *coreDataNumber;
@property (nonatomic, retain) CoolObject *coolObject;

@end

.

@implementation MyClass
@dynamic coreDataString;
@dynamic coreDataNumber;
@synthesize coolObject;

- (void)dealloc
{
   [self.coolObject release];
}

@end


  1. Yes, you are not calling [super dealloc].
  2. Yes.
  3. Yes, you must always call [super dealloc] at the end of your dealloc method. Otherwise memory will not be freed correctly.


You should call release on the member variable directly instead of using the property. You also should still call the super dealloc. So your dealloc would look like this:

- (void)dealloc
{
    [coolObject release];
    coolObject = nil;

    [super dealloc];
}

Otherwise, you can set the property to nil which will automatically release the local variable if necessary. The above way is preferred so you don't accidentally run a complicated function that could be overriding the property's setter.


You should always call [super dealloc] in the dealloc method. But in subclasses of NSManagedObject you should never use the dealloc method at all. use - (void)didTurnIntoFault instead.


You are overriding the parents - (void)dealloc method. When you override a parent object's method, the parent's method is never called until you explicitly call the super's method. In your case, the parent's - (void)dealloc is not called. To fix this, you must call [super dealloc] to ensure that the parent's instance variables are deallocated too.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜