• 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: plugging NSString * leaks
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: plugging NSString * leaks


  • Subject: Re: plugging NSString * leaks
  • From: Fritz Anderson <email@hidden>
  • Date: Wed, 5 May 2004 13:29:02 -0500

If it is called on the main thread of an AppKit application, +[NSString stringWithFormat:] should not leak. The creation and destruction of an NSAutoreleasePool at each iteration of the run loop will ensure the string is released.

If you're using +[NSString stringWithFormat:] on another thread, or in a Foundation tool, you have to allocate and release an NSAutoreleasePool of your own making to purge the string.

If toLog:level:message: retains the message string, with the 'retain' message and not a copy or init..., there is no "new reference." The same string, in the same memory, is being referred to; the retain just makes one more claimant.

If you're nervous about accumulating autoreleased NSStrings (either because you don't have an autorelease pool, or you do many, many allocations between pool releases), you could instead do

NSString * temp = [[NSString alloc] initWithFormat: @"- %s entry -", __FUNCTION__];
[log toLog: self level: kLOG_OBJECT_DEBUG message: temp];
[temp release];

-- F


On 5 May 2004, at 12:23 PM, Ken Hawkins wrote:

is there a way to stop the following code from leaking:

[log toLog:self level:kLOG_OBJECT_DEBUG message:[NSString
stringWithFormat:@"- %s entry -",
__FUNCTION__]];

this appears to be leaking on the stringWithFormat call

i know that i can create an NSAutoReleasePool * and kill that at the
end however i was wondering if there is a more efficient way to do
this? it seems that even if i retain the value within log's toLog: this
of course is a new reference and not the original that was created to
pass in.

thoughts? is an NSAutoReleasePool my only recourse?

--
Fritz Anderson
Consulting Programmer
http://resume.manoverboard.org/
_______________________________________________
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: 
 >plugging NSString * leaks (From: Ken Hawkins <email@hidden>)

  • Prev by Date: Re: NSApplicationMain const warning
  • Next by Date: prevent selection of next table cell after hitting return
  • Previous by thread: plugging NSString * leaks
  • Next by thread: Uninstalling an app
  • Index(es):
    • Date
    • Thread