Re: memory leak in readInBackgroundAndNotify
Re: memory leak in readInBackgroundAndNotify
- Subject: Re: memory leak in readInBackgroundAndNotify
- From: "Gurmit Teotia" <email@hidden>
- Date: Thu, 20 Apr 2006 11:04:32 +0530
Hi Greg,
Thanks for suggestion. I'm still wondering about the autorelease pool
behavior but let me first clear my understanding about Cocoa application.
When Cocoa application start it create a autorelease pool before calling the
[NSApplication sharedApplication] which is not accessible to outside
method(As per documentation). I have verified above by subclassing
NSApplication and overriding the sharedApplication class. I can auto release
the string even befor calling [super sharedApplication] in overriding
method.
Second pool is created in event loop before sending the event to application
and at end of event cycle this pool is also released.
Now my first question: Is a autorelease pool is also created before my
method is called back through notification? If a autorelease pool is created
then why mData is not released at the end of each call back of my method.
Instance Browser in ObjectAlloc clearly show each allocated instance of
"mData"and it is never released.
If I create the autorelease string in button action then it is never
retained. Current count never increased for that string in ObjectAlloc
program. Here also a autorelease pool is created and released at end of
event cycle. Then what is difference between autorelease pool created on
button click message and notification called back method?
I agree that if I use init.... message instead of string... problem will be
resolved but I'm not able to understand above behavior.
This for Apple guys if any of them wathching this thread.
Apple documentation seriously lacking the vital information for
understanding the Cocoa framework. It's very difficult for a newbie to
easily grasp the concept. It is very very general in concept. In one line
you people finish the description. It is more like a directionless hint.
Gurmit
On 4/19/06, Greg Herlihy <email@hidden> wrote:
>
> 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