• 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: Johnny Deadman <email@hidden>
  • Date: Thu, 7 Apr 2005 00:59:40 -0400

I think the problem is that the autorelease pool is not being emptied when you expect it to be. Objects are not autoreleased when they go out of scope but at each runloop or something like that. So if you have your app just sitting there without user input the autorelease pool will just accumulate unless you do something about it.

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 = [NSString stringWithFormat:@"http://admin:%@@%@:% d/apigetstatus.asp", configPassword, configHostname, configPortNumber];
url = [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:
40johnbrownlow.com


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>)

  • Prev by Date: Memory leak in simple code?
  • Next by Date: Re: ADC Core Data article
  • Previous by thread: Memory leak in simple code?
  • Next by thread: Re: Memory leak in simple code?
  • Index(es):
    • Date
    • Thread