• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Problem reloading data for NSOutlineViewDataSource without memory leak
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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: Thu, 20 Jan 2005 12:16:54 +0000

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


  • Follow-Ups:
    • Re: Problem reloading data for NSOutlineViewDataSource without memory leak
      • From: "Alexander F. Hartner" <email@hidden>
References: 
 >Problem reloading data for NSOutlineViewDataSource without memory leak (From: "Alexander F. Hartner" <email@hidden>)

  • Prev by Date: Re: Authorization without permanent setuid on helper
  • Next by Date: Re: Defining Accessors in a Custom Palette for a NSArray for Cocoa Bindings
  • Previous by thread: Problem reloading data for NSOutlineViewDataSource without memory leak
  • Next by thread: Re: Problem reloading data for NSOutlineViewDataSource without memory leak
  • Index(es):
    • Date
    • Thread