• 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: Memory Management Mismanaged
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory Management Mismanaged


  • Subject: Re: Memory Management Mismanaged
  • From: Marcel Weiher <email@hidden>
  • Date: Thu, 8 May 2003 12:00:30 +0200

- Mark and sweep garbage collection does not work well with distributed
objects. Reference counting does.

<SNIP>Most excellent reply</SNIP>

Agreed

And there you have it.

As one who recently had the "Aha!" experience with Cocoa memory management,
I have a piece of advice for anyone struggling with it...

When you create a new class (say, MyFoo), also create a class method to
allocate new instances, as many of the Cocoa classes do (e.g.,
[NSDictionary dictionary]).

Yes, this is excellent advice.

For example,

+ (MyFoo*) myFoo
{
MyFoo* aFoo = [[[myFoo alloc] init] autorelease];
return aFoo;
}

Three minor nits:

1. Don't use a static return type, this will break for subclasses. Use id
2. For the same reason, use 'self' to refer to the current class
3. I don't think the temporary variable is buying you anything here, so:

+myFoo
{
return [[[self alloc] init] autorelease];
}



Now you can say...

MyFoo* newFoo = [MyFoo myFoo]; // this guy will die in the next event cycle

More accurate and simpler: // I am not responsible for releasing this guy.

So, if you want to keep the object longer than one event cycle, do this
instead:

MyFoo* newFoo = [[MyFoo myFoo] retain];

Possibly easier:

[self setFoo:[MyFoo myFoo]];


It's much better, IMHO, to create temporary objects by default, since that
eliminates a large number of potential leaks (and it was leaks that lead me
to the "Aha!" moment), and you'll soon find out which objects need to be
retained, since you'll get a crash if try to access one which wasn't.

Yes. Although I don't get many crashes, because I uses accessors for all instance variable access and only instance variables stick around. So memory management has almost completely left the picture.

Actualy, I was amazed at how much simpler the code got after I adopted this
approach. In my app, I was generating a lot of temporary objects simply to
pass around as values from one method to another, plus a few instances
which were suppposed to stay alive for the lifespan of the document. Long
story short, things got a lot better :o)

Yup. Couldn't agree more with what Duncan is saying, except I would say it can get even simpler... ;-)

Marcel



--
Marcel Weiher Metaobject Software Technologies
email@hidden www.metaobject.com
Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Memory Management Mismanaged
      • From: Danny Swarzman <email@hidden>
    • Re: Memory Management Mismanaged
      • From: Jim Rankin <email@hidden>
References: 
 >Re: Memory Management Mismanaged (From: Andrew Duncan <email@hidden>)

  • Prev by Date: Re: NSComboBoxCell and NSTableView
  • Next by Date: Re: How drag files from an NSTableView?
  • Previous by thread: Re: Memory Management Mismanaged
  • Next by thread: Re: Memory Management Mismanaged
  • Index(es):
    • Date
    • Thread