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: Jeremy Dronfield <email@hidden>
- Date: Mon, 24 Jan 2005 10:47:13 +0000
On 23 Jan 2005, at 8:56 am, Alexander F. Hartner wrote:
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.
}
Try this instead:
- (void)setContacts:(NSArray *)newContacts {
if (contacts != newContacts) {
[contacts release];
contacts = [newContacts copy];
}
}
This is the setter pattern I always use. (I suggested the other merely
because it was the style used in the CDC article.)
As soon as I reload my NSOutlineView my application crashes.
What do your delegate/data source methods look like? If you're crashing
on -reloadData, the bug is likely to be there.
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.
The core message of the article is that you should use a systematic,
patterned approach to getting and setting instance variables. The
purpose of creating a pointer to the original value in the setter is to
protect you from the consequences of doing this: [self
setContacts:contacts]. In my opinion, the method suggested in the
article isn't the most intuitive way of achieving this. The test if
(contacts != newContacts) provides the same protection.
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.
The outline view doesn't maintain a reference to anything. It loads
whatever your data source tells it to load. Your data source is
responsible for creating/retaining/releasing the data. As I said, the
problem is most likely in your data source/delegate methods. Or equally
likely in the method(s) you use to create contacts. (Incidentally, is
there a reason why you're using an array for your data rather than some
tree-type data structure?)
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