CocoaDev

Edit AllPages

Hi There:

I’m having some issues with Memory Leaking from NSURLConnection (either from calling it directly, or when it’s used in another method [eg initWithContentsOfURL]. The problem seems to appear when it’s used on a thread other than the main one. Here’s my code:

// This is released when the program begins, by a thread that is in turn released by the initWithFrame method

}

/// the method above in turn calls this method…

}

// which in turn calls this method…

I don’t think there are any leaks in the code… but MallocDebug says otherwise. It’s a screensaver, so it’s running for a long time - and thus memory leaks are a big issue.

I’ve tried to use curlHandle - but I couldn’t get it to link properly. Could this have something to do with it being a screensaver?

Any help would be appreciated…

Thanks, Alexandre


There’s a well-known bug that sendSynchronousRequest: will leak when used from multiple threads. The workaround is to use the asynchronous delegate-based NSURLConnection methods instead.


Hmm… OK, thanks. I’m assuming Apple is aware of this bug?

Alexandre

I wouldn’t asume that, I did submit a bug that was also well known, but it toke a long time before it became a duplicate. I would say submit it anyway, it will also get more and more awareness.

RvA


It looks like it’s pretty well known, both in and outside of Apple:

http://www.google.com/search?client=safari&rls=en&q=nsurlconnection+leak+site:cocoabuilder.com&ie=UTF-8&oe=UTF-8

However, filing a bug wouldn’t hurt; Apple uses the number of bugs filed about a particular problem to help determine which ones should be fixed first.


Ok, so right now I’m trying to set up the asynchronous connection. I can do it fine when I run it from the main thread, but I run into problems when I try doing it from a thread that I have detached from the main. The reason I need to do it is because it’s a screensaver, and if I do it in the AnimateOneFrame method the animation lags. I’ve created a new object “Downloader” to use as the delegate. I can set some variables in the Downloader instance that I create so that I know what to do with the data after It’s been received (I do several different types of requests, sometimes at the same time).

Anyways, here’s the code:

// this is in a different thread, in a loop… so the thread does not exit. Also, this doesn’t get called again until the connection is finished (there’s a variable called gettingMorePhotos that is set to true to avoid calling two at once).

Downloader *photoDownloader = Downloader alloc] initWithType:@”userPhotos” andMainClass:self];

[photoDownloader setString:[[filteredFriends objectAtIndex:0] uid;

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:photoDownloader];

if (theConnection) {

// Create the NSMutableData that will hold // the received data [photoDownloader createData];

NSLog(@”Connection created”);

} else {

NSLog(@”could not create connection..”); // inform the user that the download could not be made

}

This code works fine when it’s not in a new thread. But when it is in a new thread, the Connection gets created, but the delegate methods do not get called. What’s the deal here? What am I doing wrong?

Thanks, Alexandre