Re: GC pros and cons
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