aryaxt
aryaxt

Reputation: 77646

Objective C - Core Data memory management?

1- Is these 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

Upvotes: 1

Views: 675

Answers (4)

Martin Bissegger
Martin Bissegger

Reputation: 71

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.

Upvotes: 2

drewag
drewag

Reputation: 94773

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.

Upvotes: 2

David
David

Reputation: 14414

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.

Upvotes: 0

Anomie
Anomie

Reputation: 94834

  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.

Upvotes: 4

Related Questions