• 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: Andrew Duncan <email@hidden>
  • Date: Thu, 8 May 2003 20:59:20 +1200

> Below is a long explanation because is was requested:
>
> On Wednesday, May 7, 2003, at 09:53 PM, Dustin Voss wrote:
>
>> On Wednesday, May 7, 2003, at 04:46 AM, lbland wrote:
>>
>>> On Wednesday, May 7, 2003, at 06:59 AM, Pete Yandell wrote:
>>>
>>>> Seriously though, grafting full garbage collection onto objective-C
>>>> would clearly be pretty close to impossible
>>>
>>> just for fun...
>>>
>>> http://gcc.gnu.org/onlinedocs/gcc-3.2.2/gcc/Garbage-
>>> Collection.html#Garbage Collection
>>
>> So, wait, this works? Then why the heck are we all using
>> retain/release?!
>>
>
> - Mark and sweep garbage collection does not work well with distributed
> objects. Reference counting does.

<SNIP>Most excellent reply</SNIP>

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]).

For example,

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

Now you can say...

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

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

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

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.

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)

Cheers,
--
Andrew Duncan
----------------------------------------------------------------------
voice: +64 9 623 2926
mobile: 021 297 3174
web: http://web.webwerks.co.nz/
----------------------------------------------------------------------

Programmers do it while (1)
_______________________________________________
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: Marcel Weiher <email@hidden>
  • Prev by Date: Re: NSComboBoxCell and NSTableView
  • Next by Date: Loosing threads in cocoa
  • Previous by thread: Re: Memory Management Mismanaged
  • Next by thread: Re: Memory Management Mismanaged
  • Index(es):
    • Date
    • Thread