Re: Memory Mania Revisted
Re: Memory Mania Revisted
- Subject: Re: Memory Mania Revisted
- From: email@hidden
- Date: Thu, 7 Feb 2002 22:40:07 -0800
Here's some hypothetical code:
NSDecimalNumber *myDN = (NSDecimalNumber *)[NSDecimalNumber
numberWithFloat:2.5];
NSDecimalNumber *myOtherDN = (NSDecimalNumber *)[NSDecimalNumber
numberWithFloat:4.5];
myDN = [myDN decimalNumberByAdding:myOtherDN];
OK... I _think_ there's a leak here, because - (NSDecimalNumber
*)decimalNumberByAdding:(NSDecimalNumber *)dn creates a new instance of
NSDecimalNumber, right? then the original myDN memory is lost because
I've reassigned the pointer without deallocating this. My question is,
what is the best strategy for eliminating this leak? I do this step a
lot in my code, and I don't want to have to write 4 lines whenever I do
it...
There is no leak. The NSDecimalNumbers you made initially are
autoreleased. In fact, if you took the trouble to release them, you
would introduce a crasher, as the numbers would finally be released by
the autorelease pool after they had already been deallocated.
The general rule is that anything you make using +alloc, +new, or
-copy (and variants of these) has a retain on it. All other ways of
creating objects typically are autoreleased (which is a quick way of
saying they have a retain count of one with a pending release scheduled
by the autorelease pool in whose context they were created). You can
generally ignore retain/release issues with autoreleased objects, since
they will just magically go away when the autorelease pool pops.
The specific policy on what is returned retained and what is returned
autoreleased is in the Foundation doc somewhere. The policy in
CoreFoundation is more explicit and somewhat different, I think; you can
read about that in the CF doc.
I realize I'm being a bit vague, but I hope I'm pointing you in the
right direction. :->
So I would recommend re-reading whatever you just read, and then going
and reading the reference doc for NSAutoreleasePool. :->
Ben Haller
Stick Software
_______________________________________________
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.