Re: Can't keep from leaking memory
Re: Can't keep from leaking memory
- Subject: Re: Can't keep from leaking memory
- From: "Tony S. Wu" <email@hidden>
- Date: Sat, 25 Jun 2005 21:45:50 +0800
in your code, someData should never be nil.
even if otherData is nil, someData would only be an empty NSData
object, but it still is not nil.
only time it will be nil is if its super class fail to initialize.
(someone corrects me if i am wrong)
and if that's the case, the code should never go into the if loop,
and without the retain message it still should be OK.
so, where are you checking if your application is leaking memory?
under what circumstances do you expect the if loop be executed?
how about copy-and-pasting your code?
i think you might want to check if otherData is nil, instead of
checking someData
also, why not use NSData's + dataWithData: class method instead?
Tony S. Wu
email@hidden
On Jun 25, 2005, at 9:15 PM, John Timmer wrote:
OK, I'm feeling dumb tonight...
No matter what I try I can't keep this from leaking memory
- (NSData*)someData {
NSData *someData = [[NSData alloc] initWithData:otherData];
if (someData == nil)
someData = [[NSData alloc] initWithData:someOtherData];
return [[someData retain] autorelease];
}
Despite what I try if someData == nil during that call it will
end up
leaking.
Any thoughts on how to correct this?
You have two alloc calls there, both of which can potentially be
called by
this method. If the second one is called, the first alloc'ed
instance leaks.
Actually, I'm not so sure about this - what happens when you alloc
some
memory, but init returns nil - do you still have to worry about what's
alloc'ed?
JT
_______________________________________________
This mind intentionally left blank
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden