Iphone NSManagedObject - the right way to free it?

I have a subclass of NSManagedObject, most properties are dynamic (created by the managed entity), but I have some helper properties that are created as @synthesize.

What is the correct way to free these objects?

- (void)didTurnIntoFault { [self.randomVar release]; [super didTurnIntoFault]; } 

or

 - (void)dealloc { [self.randomVar release]; [super dealloc]; } 
+7
source share
3 answers

didTurnIntoFault and release them there. Managed objects are not necessarily freed when they occur.

From the documentation :

You are not recommended to override dealloc or finalize , because didTurnIntoFault usually better to clear values ​​- the managed object cannot be restored for some time after it has turned into a malfunction. Master data does not guarantee that either dealloc or finalize will be called in all scenarios (for example, when the application shuts down); therefore, you should not use these methods, including the required side effects (for example, saving or changing the file system, user settings, etc.).

+11
source

Perhaps you should take a look at the Master Data Programming Guide. These two passages are of particular importance.

If you define your own instance of variables, you must clear these variables in didTurnIntoFault than dealloc or complete.

and

Normally you should not override dealloc or terminate to clear the transient properties and other variables. Instead, you should override didTurnIntoFault.

+6
source

[This is really a comment because I point to another problem, but I need a code formatting function].

Never do this:

 [self.randomVar release]; 

This frees the object that ivar support points to, but does not make the pointer itself null. This means that now you may have a sagging pointer if the object is freed as a result of the release. Or do this:

 self.randomVar = nil; 

or

 [randomVar release]; // assumes the property is backed by an ivar of the same name. randomVar = nil; // can be omitted if you are in dealloc 

The first form is preferred everywhere except dealloc.

+4
source

All Articles