• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [newbie] NSString code so it won't leak
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [newbie] NSString code so it won't leak


  • Subject: Re: [newbie] NSString code so it won't leak
  • From: Paul Nelson <email@hidden>
  • Date: Wed, 11 Sep 2002 00:10:25 -0400

I have a short discussion on memory management on my FAQ:

http://gigue.peabody.jhu.edu/~pnelson/NewbieFAQ.html

Everyone is assuming that strings returned by "stringFromString:" are
auto-released, which means that when you manually release them with
"release" then they will be released again when the Autorelease pool is
itself released (usually done at the end of each event loop), which will
then crash.

Perhaps you should test this by setting up your own auto-release pool? Then
you can see if the strings are, in-fact, released when your auto-release
pool is released.

Also, is it possible that your application never releases the autorelease
pool? Perhaps you don't have an event loop in your application?

Paul


on 9/10/02 11:44 PM, Dean Davis at email@hidden wrote:

> --- Ondra Cada <email@hidden> wrote:
>>
>> On Tuesday, September 10, 2002, at 03:58 , Dean
>> Davis wrote:
>>
>>> NSString *string1,*myString
>>>
>>> string1 = [myString stringToIndex:41];
>>> myString = [myString stringFromIndex:41];
>>>
>>> This works but leaks.
>>
>> It not only works; also, it does not leak. Read
>> memory management, focus
>> on autoreleasing.
>>
>
> I wrote a small test app that uses NSURLHandle to
> download yahoo.com then sent the resource data to a
> method. I extract the NSData to an NSString.
> I do some code very similar to...
> string1 = [myString stringToIndex:41];
> myString = [myString stringFromIndex:41];
>
> As far as I can tell with ObjectAlloc (thanks for the
> tip) it leaks.
>
>>> myString = [myString2 stringFromString:myString2];
>>> // or? myString = [myString2 copy];
>>> [myString2 release];
>>
>> This would crash, on the other hand.
>> ---
>> Ondra Cada
>> OCSoftware: email@hidden
>> http://www.ocs.cz
>> private email@hidden
>> http://www.ocs.cz/oc
>>
> Replaced the my first leaky example with the above
> code. Not only does it not crash the application it
> seems to stop the leak.
>
> I posted my test project here...
> http://www.afterten.com/Downloads/LeakTest.sit
> For someone to verify or tell me what I'm doing wrong.
> Cocoa is nice but fustrating that doesn't seem to be
> any real authority on what needs to be released and
> when. But I am reading all the links everyone has
> posted on this thread.
>
> Thanks.
> Dean Davis
>
>
> __________________________________________________
> Yahoo! - We Remember
> 9-11: A tribute to the more than 3,000 lives lost
> http://dir.remember.yahoo.com/tribute
> _______________________________________________
> 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.

--

Paul Nelson
Peabody Student Composer

http://www.peabody.jhu.edu/~pnelson
_______________________________________________
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.

References: 
 >Re: [newbie] NSString code so it won't leak (From: Dean Davis <email@hidden>)

  • Prev by Date: Re: [newbie] NSString code so it won't leak
  • Next by Date: Validating NSControl
  • Previous by thread: Re: [newbie] NSString code so it won't leak
  • Next by thread: Re: [newbie] NSString code so it won't leak
  • Index(es):
    • Date
    • Thread