• 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: Memory leak in simple code?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory leak in simple code?


  • Subject: Re: Memory leak in simple code?
  • From: Jason von Nieda <email@hidden>
  • Date: Thu, 07 Apr 2005 09:51:08 -0500

Thanks for the tip. It sounds like this is the right path, but I gave it a quick try on the way out the door this morning and it didn't work. I added:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; as the first line of the function and
[pool release]; as the last and it still leaks the same stuff.
I'll poke more at it when I get home tonight but if anyone has any additional ideas I'd love to hear them.
Is there something specific about using the NSTimer I need to worry about?

Thanks,
Jason


Johnny Deadman wrote:
more specifically, the ADC says this (look at the last sentence):

NSAutoreleasePools are automatically created and destroyed in applications based on the Application Kit, so your code normally does not have to worry about them. (The Application Kit creates a pool at the beginning of the event loop and releases it at the end.) There are three cases, though, where you might wish to create and destroy your own autorelease pools. If you are writing a program that is not based on the Application Kit, such as a command-line tool, there is no built-in support for autorelease pools; you must create and destroy them yourself. Likewise, if you spawn a secondary thread, you must create your own autorelease pool as soon as the thread begins executing; otherwise, you will leak objects. (See “Autorelease Pools and Threads” for details.) Finally, if you write a loop that creates many temporary objects, you might wish to create an NSAutoreleasePool inside the loop to dispose of those objects before the next iteration.

On 7-Apr-05, at 12:45 AM, Jason von Nieda wrote:

Hi all, my first post to the list, so be gentle please :)

I have a Cocoa application I wrote a while back to learn the framework and I am now trying to clean it up for distribution. It's leaking memory and I have narrowed the leak down to one line, but I can't figure out why it's leaking.

The line is in a function being called by a NSTimer every 3 seconds, and ObjectAlloc is showing that I am leaking 12 NSStrings every 3 seconds. Below is the code with some notes in comments. I'd appreciate if someone can give me a helping hand :)

// The timer function, called every 3 seconds from an NSTimer
// This function is normally much longer, but I've commented everything else out (and even removed it from the source) in the process of finding which line was causing the leak
- (void)getAudioTronStatus:(NSTimer *) timer
{
   NSString *url = "" stringWithFormat:@"http://admin:%@@%@:%d/apigetstatus.asp", configPassword, configHostname, configPortNumber];
   url = "" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
   NSURL *nsURL = [NSURL URLWithString:url];
   // !!!!!!!
   // The next line is the one causing the leak. If I comment it out the program does not leak and if I try to
   // release or autorelease "results" the program crashes.
   NSString *results = [NSString stringWithContentsOfURL:nsURL];
   //NSLog(@"%@", results);
}

// this is the line in awakeFromNib that starts the timer
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(getAudioTronStatus:) userInfo:nil repeats:YES];

My understanding of Cocoa memory management in ObjC is that any object I don't specifically call alloc or retain on should be autoreleased when it goes out of scope. So I feel like "results" should be autoreleased at the end of the function.

Thanks,
Jason von Nieda
_______________________________________________
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

References: 
 >Memory leak in simple code? (From: Jason von Nieda <email@hidden>)
 >Re: Memory leak in simple code? (From: Johnny Deadman <email@hidden>)

  • Prev by Date: Temporarily change the preferred language setting?
  • Next by Date: Re: Application Icon Blues
  • Previous by thread: Re: Memory leak in simple code?
  • Next by thread: Re: Memory leak in simple code?
  • Index(es):
    • Date
    • Thread