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

Memory leak in simple code?


  • Subject: Memory leak in simple code?
  • From: Jason von Nieda <email@hidden>
  • Date: Wed, 06 Apr 2005 23:45:07 -0500

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Memory leak in simple code?
      • From: Johnny Deadman <email@hidden>
    • Re: Memory leak in simple code?
      • From: Johnny Deadman <email@hidden>
  • Prev by Date: Re: ADC Core Data article
  • Next by Date: Re: Memory leak in simple code?
  • Previous by thread: Re: Accessing undo stack?
  • Next by thread: Re: Memory leak in simple code?
  • Index(es):
    • Date
    • Thread