Re: memory leak in readInBackgroundAndNotify
Re: memory leak in readInBackgroundAndNotify
- Subject: Re: memory leak in readInBackgroundAndNotify
- From: Greg Herlihy <email@hidden>
- Date: Wed, 19 Apr 2006 08:36:53 -0700
- Thread-topic: memory leak in readInBackgroundAndNotify
The mData NSString in all likelihood is not "leaking" - rather it simply
takes a long time for the app to get around to releasing its autorelease
pool because it is busy with the file I/O.
In the meantime these mData objects are likely to accumulate and use up
memory, so it would make sense to release each mData object as soon as it is
no longer needed:
{
NSString *mData=[[NSString alloc] initWithUTF8String:[data bytes]];
...do something with mData...
[mData release]; // release when no longer needed
}
One further suggestion: I would suggest not naming the NSString object
"mData" since I would expect mData to be the name of an NSData object and
not of an NSString object.
Greg
On 4/19/06 3:23 AM, "Gurmit Teotia" <email@hidden> wrote:
> Could someone help me on this topic? Resending it again as I didn't get any
> reply.
> I would like as one more finding here, if I declare mData as instance
> variable and try to print in message of button click then my application
> crashes. It means it is released, but then why current count in ObjectAlloc
> increase by one upon receiving the data?
>
> Regards,
> Gurmit
>
>
> On 4/18/06, Gurmit Teotia <email@hidden> wrote:
>>
>> Hi All,
>>
>>
>>
>> I have created a GUI application in Cocoa. I'm using the NSFileHandle's
>> readInBackgroundAndNotify method to read the data from socket. Data
>> receiving method is something like this:-
>>
>>
>>
>> -(void) receivedData: (NSNotification *) notification
>>
>> {
>>
>> NSData *data=[[notification userInfo] objectForKey:
>> NSFileHandleNotificationDataItem];
>>
>> NSString *mData=[NSString stringWithUTF8String:[ data bytes];
>>
>>
>>
>> .. do something with mData
>>
>>
>>
>> [fileHandle readInBackgroundAndNotify];
>>
>>
>>
>> }
>>
>>
>>
>> In above method "mData" is not released which results in increased
>> memoryfootprint of application. I'm wondering in which autorelease pool
>> "mData" is
>> allocated? ObjectAlloc program is only showing one instance of
>> NSAutoreleasePool.
>>
>>
>>
>> If I create a new NSAutoreleasePool object at entry point of method and
>> release it at exit point then no memory leaks happen.
>>
>> Could any one please explain me this behaviour? I'm also new to Cocoa
>> development.
>>
>>
>>
>> Regards,
>>
>>
>>
>> Gurmit
>>
> _______________________________________________
> 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