• 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: GC pros and cons
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: GC pros and cons


  • Subject: Re: GC pros and cons
  • From: James Gregurich <email@hidden>
  • Date: Thu, 25 Jun 2009 22:06:19 -0700


I'll speak up and throw a wrinkle into this discussion. I found it useful and may some others will also.


I have read up on GC, and I don't like the idea of introducing non- deterministic behavior into my code base. However, it is true that manually retaining and releasing pointers is inherently error-prone.

Personally, my solution is objc++ with the boost::shared_ptr template. The following code works great...its easy to maintain and understand...it makes it easy to share objects across threads safely....it automatically manages the memory with a retain count in a manner completely compatible with NSAutoreleasePool.



void releaseNSObject(NSObject* theObjPtr)
{
	[theObjPtr release];
}


....

boost::shared_ptr<NSString> tmpStringSPtr([[NSString alloc] init], releaseNSObject);


Yes, it uses an extra int per object keeping an extra retain count, but I'll trade that for the reliability and maintainability I get in return. For the work I do, what is being pointed to is almost always far larger than 4 (or 8) bytes.




I also take advantage of references in C++ to pass objc objects around without having to do null checks all over the place to keep code resilent.


-(void) passMyObject: (NSObject&) theObj { NSLog(@"%@", &theObj); }

since the function takes a reference, client code knows never to pass a null to the function.





On Jun 25, 2009, at 2:00 PM, Peter Ammon wrote:


On Jun 25, 2009, at 1:42 PM, Bill Bumgarner wrote:

On Jun 25, 2009, at 3:14 PM, Peter Ammon wrote:
In any case, it's been my experience that GC makes memory management much easier, but precious resource management somewhat harder. It's harder because GC forces more of a divorce between the management of memory and precious resources, and the precious resource management techniques are about on the level with C circa 1989.

Really, retain/release requires such a separation, too. At least, it does for relatively complex, often concurrent, piles of code.



I totally agree, which is why I said "more of" a divorce. Since retain/release is more deterministic than GC, it allows you to tolerate ordering dependencies and tying resource lifetime to memory lifetime for longer. But it ultimately breaks down as complexity increases, just as you say.


The switch to GC has forced me to redesign a number of classes. But I usually find that the redesigned classes work better under retain/ release as well.

-Peter

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: GC pros and cons
      • From: Chris Idou <email@hidden>
    • Re: GC pros and cons
      • From: "Stephen J. Butler" <email@hidden>
References: 
 >Re: GC pros and cons (From: Quincey Morris <email@hidden>)
 >Re: GC pros and cons (From: Chris Idou <email@hidden>)
 >RE: GC pros and cons (From: Jeff Laing <email@hidden>)
 >Re: GC pros and cons (From: "Stephen J. Butler" <email@hidden>)
 >Re: GC pros and cons (From: Peter Ammon <email@hidden>)
 >Re: GC pros and cons (From: Kyle Sluder <email@hidden>)
 >Re: GC pros and cons (From: Peter Ammon <email@hidden>)
 >Re: GC pros and cons (From: Kyle Sluder <email@hidden>)
 >Re: GC pros and cons (From: Peter Ammon <email@hidden>)
 >Re: GC pros and cons (From: Bill Bumgarner <email@hidden>)
 >Re: GC pros and cons (From: Peter Ammon <email@hidden>)

  • Prev by Date: Re: Amount of Arguments per Method
  • Next by Date: Re: GC pros and cons
  • Previous by thread: Re: GC pros and cons
  • Next by thread: Re: GC pros and cons
  • Index(es):
    • Date
    • Thread