Re: Memory Leaks in CocoaEcho Sample
Re: Memory Leaks in CocoaEcho Sample
- Subject: Re: Memory Leaks in CocoaEcho Sample
- From: "Ken Ferry" <email@hidden>
- Date: Thu, 23 Oct 2008 02:27:12 -0700
Hi Marco,
> Still, my question remains: Is there a rule of thumb for the memory
> management of object returned by reference? NSError and NSGradient
> autorelease their objects, NSNetService does not. Because the docs don't say
> anything about this (with the exception of the Error Handling Programming
> Guide), how should one know if an object should be retained or not?
The rule for objects returned by reference is the same as the rule for
objects returned any other way: -copy, -alloc, -retain and -new methods
transfer ownership. Nothing else does unless specially documented. If you
think there's a leak with an object returned by reference, please file a
bug.
I see that there was a leak in -getInputStream:outputStream:, but it was
fixed for apps linked on or after the 10.4 SDK. This was probably in the
Foundation release notes, but the older Foundation release notes don't seem
to be up anymore.. <rdar://problem/6313911>.
-Ken
Cocoa Frameworks
On Thu, Oct 23, 2008 at 1:52 AM, Marco Masser <email@hidden>wrote:
> Then the bug is somewhere in your changes. The only thing you should do is
>>> remove the retain calls. If you also remove the release calls, you will
>>> still have the memory leaks.
>>>
>>> Here's what openStreams should look like:
>>>
>>> - (void)openStreams {
>>> [inputStream setDelegate:self];
>>> [outputStream setDelegate:self];
>>> [inputStream scheduleInRunLoop:
>>> [NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
>>> [outputStream scheduleInRunLoop:
>>> [NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
>>> [inputStream open];
>>> [outputStream open];
>>> }
>>>
>>> Here's what closeStreams should look like:
>>>
>>> - (void)closeStreams {
>>> [inputStream close];
>>> [outputStream close];
>>> [inputStream removeFromRunLoop:
>>> [NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
>>> [outputStream removeFromRunLoop:
>>> [NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
>>> [inputStream setDelegate:nil];
>>> [outputStream setDelegate:nil];
>>> [inputStream release];
>>> [outputStream release];
>>> inputStream = nil;
>>> outputStream = nil;
>>> }
>>>
>>
>> I have done exactly what you wrote above in a freshly dezipped CocoaEcho
>> Project. Running it first with the retain and then without. It didn't work.
>> This morning, I tried it again and it worked?!? I think I forgot to clean
>> the build or something like that... Thanks for your help
>>
>
>
> Still, my question remains: Is there a rule of thumb for the memory
> management of object returned by reference? NSError and NSGradient
> autorelease their objects, NSNetService does not. Because the docs don't say
> anything about this (with the exception of the Error Handling Programming
> Guide), how should one know if an object should be retained or not?
>
> Marco
>
> _______________________________________________
>
> 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