Re: Garbage collection leak in simple Core Data application
Re: Garbage collection leak in simple Core Data application
- Subject: Re: Garbage collection leak in simple Core Data application
- From: Rob Keniger <email@hidden>
- Date: Fri, 28 Nov 2008 08:42:07 +1000
On 28/11/2008, at 7:13 AM, Mathieu Coursolle wrote:
Hi Cocoa developers,
I created a simple garbage collected Core Data application using the
Xcode template.
I did not change much except that I added an empty window controller
for my window,
which is the file's owner of my .xib.
I added logs in the init and finalize methods to make sure that each
object created was
eventually finalized. However, I realized that even if the document
is always finalized when
I close the window, its window controller is not. Let's say I open a
new document and close
it right away and repeat the operation a few dozen times, I end up
with all documents collected,
but a few window controller still to be collected. Even if I force
the collector, they are not collected.
Even stranger, if I delete my only entity from my .xcdatamodel
(which does nothing), then
everything gets collected as it should. How can a entity prevent an
object from being collected?
You are adding the NSWindowController to your document using -
addWindowController:, which retains the window controller. You must
then release the window controller using -removeWindowController: so
that the collector frees the object.
Do this in MyDocument.h:
#import <Cocoa/Cocoa.h>
@class MyWindowController;
@interface MyDocument : NSPersistentDocument {
MyWindowController* newWindowController;
}
@end
and change these methods in MyDocument.m:
- (void)makeWindowControllers
{
// Create the project window controller and keep a reference to it.
newWindowController = [[MyWindowController alloc] init];
[newWindowController setShouldCloseDocument:YES];
// Add it to the list of window controllers for this document.
[self addWindowController:newWindowController];
}
- (void)finalize
{
[self removeWindowController:newWindowController];
NSLog(@"Document finalized");
UpdateDocumentCount(0);
[super finalize];
}
--
Rob Keniger
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden