Re: Problem reloading data for NSOutlineViewDataSource without memory leak
Re: Problem reloading data for NSOutlineViewDataSource without memory leak
- Subject: Re: Problem reloading data for NSOutlineViewDataSource without memory leak
- From: "Alexander F. Hartner" <email@hidden>
- Date: Sun, 23 Jan 2005 10:56:36 +0200
Thanks Jeremy,
I tried what you suggested but it didn't work. I added a getter and setter as follows:
- (void)refreshContact
{
NSLog(@"Refresh Contact :");
[self setContacts:[ldapConnector search:[searchFor stringValue]]];
[contactsView reloadData];
}
- (NSArray *) contacts
{
NSLog(@"Retrieving contacts");
return [[contacts retain] autorelease];
}
- (void)setContacts:(NSArray *) newContacts
{
NSLog(@"Replacing contacts");
id oldContacts = contacts;
contacts = [newContacts retain];
[oldContacts release]; //Causes application to crash.
}
As soon as I reload my NSOutlineView my application crashes. My original intentions where to implements the logic from the set method as my application does not require a get method.
What I was trying to do was to retrieve a reference to the original contacts and assign it to oldContacts. After having replaced the contacts the old contacts were to be released via the reference to oldContacts. I understood this to be the core message of the article. However I perceive to be the problem with NSOutlineView as it seem to be maintaining a reference to the original contacts as the application only crashes after reloadData and only if the original contacts have been released. This leads me to believe that the NSOutline view is still referencing the original contacts rather then the updated / new contacts.
Thanks for your helps
Alex.
- (void)refreshContact
{
NSArray * oldContacts = nil;
//Check if current contacts exist and copy reference to current contact
if (contacts != nil)
{
oldContacts = contacts;
}
//construct new array and assign to current reference
contacts = ... //Construct new array here
[contacts retain];
//release old array
if (oldContacts != nil)
{
[oldContacts release];
}
[contactsView reloadData];
}
On 20 Jan 2005, at 14:16, Jeremy Dronfield wrote:
On 20 Jan 2005, at 5:24 am, Alexander F. Hartner wrote:
Within my implementation of NSOutlineViewDataSource I am reloading it's data which is stored in an array (NSArray). When the data changes I am trying to replace the current array with a new array and dispose of the old array as show below:
- (void)refreshContact
{
NSArray * oldContacts = nil;
//Check if current contacts exist and copy reference to current contact
if (contacts != nil)
{
oldContacts = contacts;
}
//construct new array and assign to current reference
contacts = ... //Construct new array here
[contacts retain];
Here's your leak. You retain contacts and never release it. Then you get rid of oldContacts (which is merely a pointer to contacts) without having done anything with it.
//release old array
if (oldContacts != nil)
{
[oldContacts release];
}
[contactsView reloadData];
}
I got this technique from Memory Management 101 (www.cocoadevcentral.com).
It looks to me as if you've misunderstood the article. You appear to be trying to create a sort of mangled accessor method inside your -refreshContact method. I suggest you re-read the CocoaDevCentral article, which describes (among other things) the use of accessor methods. Accessor methods are used for setting and accessing your instance variables in a systematic way which is intended to avoid leaks and other memory management problems. In the case of your contacts variable (and in keeping with the style suggested in the article), I would suggest doing something like this for setting/getting your array:
- (NSArray *)contacts {
return [[contacts retain] autorelease];
}
- (void)setContacts:(NSArray *)newContacts {
id old = [self contacts];
contacts = [newContacts retain];
[old release];
}
- (void)refreshContact
{
NSArray *newContacts;
// Build your array
[self setContacts:newContacts];
[contactsView reloadData];
[newContacts release]; // (Only if you retained it. If newContacts is an autoreleased object, omit this line.)
}
Hope this helps. Regards,
Jeremy
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden