AsyncSocket and Notifications - memory leak - iphone

I have a memory leak in the following scenario. I read data at every 30 seconds, use SBJSONParser to transform it to a dictionary, add a notification and after that use the data to bind it to a tableview:
// Read data and send notification
-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
NSString *content = [[NSString alloc] initWithData:[data subDataWithRange:NSMakeRange(0, [data length] - 2)] encoding: NSUTF8StringEncoding];
// Line where leaks appear
NSMutableDictionary* dict = [[NSMutableDictionary alloc] initWithDictionary:[content JSONValue]];
[content release];
// Post notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"BindData" object:nil userInfo:dict];
[dict release];
}
On a CustomViewController I have the observer:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(bindData) name:#"BindData" object:nil];
and the bindData method:
-(void)bindData:(NSNotification*)notification
{
NSAutoreleasePool* pool = [[NSAutoReleasePool alloc] init];
NSMutableArray* customers = [notification.userInfo objectForKey:#"Customers"];
for (NSDictionary* customer in customers)
{
Company* company = [[Company alloc] init];
company.name = [customer objectForKey:#"CompanyName"];
NSLog(#"Company name = %#", company.name);
[company release];
}
[pool drain];
}
The problem is: when I set company.name = something from that dictionary, I get a memory leak on the line: NSMutableDictionary* dict = [[NSMutableDictionary alloc] initWithDictionary:[content JSONValue]]; which keeps increasing since I'm reading at every 30 seconds.
I appreciate any help you can give. Thanks.

dict is leaking because you are using alloc and init (thus increasing its retain count by 1), but never releasing it. Since the dictionary will no longer be needed after the notification has been posted, you can safely release it on the following line, like so:
// Post notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"BindData" object:nil userInfo:dict]
[dict release];
See the Memory Management Programming Guide for more details.

Related

Multiple feeds with MWFeedParser, help with code

I currently have this code but it leaks because I don't know how to release feed parser object this many times, so I need help with memory management or some other way to feed multiple feeds with it.
Code:
for (NSString *imePredmeta in [Data variables].mojiPredmeti) {
NSString *link = [[Data variables].rss objectForKey: imePredmeta];
NSURL *feedURL = [NSURL URLWithString: link];
feedParser = [[MWFeedParser alloc] initWithFeedURL:feedURL];
feedParser.delegate = self;
feedParser.feedParseType = ParseTypeFull; // Parse feed info and all items
feedParser.connectionType = ConnectionTypeAsynchronously;
[feedParser parse];
}
Dealloc:
- (void)dealloc {
[formatter release];
[parsedItems release];
[itemsToDisplay release];
[feedParser release];
[super dealloc];
}
And initWithFeedUrl from MWFeedParser code is:
- (id)initWithFeedURL:(NSURL *)feedURL {
if ((self = [self init])) {
// Check if an string was passed as old init asked for NSString not NSURL
if ([feedURL isKindOfClass:[NSString class]]) {
feedURL = [NSURL URLWithString:(NSString *)feedURL];
}
// Remember url
self.url = feedURL;
}
return self;
}
The best way would be to add them to an array, and then in dealloc release that array.
// declare an iVar in your header file
NSMutableArray *feeds;
// instantiate the array, but release it first as you may call your method more
// than once
if (feeds) {
[feeds release];
}
feeds = [[NSMutableArray alloc] init];
for (NSString *imePredmeta in [Data variables].mojiPredmeti) {
...
// after calling parse on the feed, add it to the array and then release it.
// Objects added to an array are retained by the array so this is safe
[feedParser parse];
[feeds addObject:feedParser]; // <-- Add parser to array
[feedParser release]; // <-- Release parser
}
// then in your dealloc simply release the array, which will in-turn release all
// the parser objects it contains
- (void)dealloc {
[formatter release];
[parsedItems release];
[itemsToDisplay release];
[feeds release]; // <-- Release feeds array
// I've removed the call to release feedParser as it's already been released
[super dealloc];
}

NSOperation and EXC_BAD_ACCESS

I have a few apps which are largely data driven, so most screens are basically composed of:
Open the screen
Download the data via an NSOperation
Display data in a UITableView
Make a selection from the UITableView
Go to new screen, and start over from step 1
I am finding that everything works in normal usage, but if the user goes away from the app for a while and then comes back, I'm getting an EXC_BAD_ACCESS error when the next NSOperation runs. This doesn't seem to matter if the user sends the app into the background or not, and it only seems to occur if there's been at least a few mins since the previous data connection was made.
I realise this must be some form of over-releasing, but I'm pretty good with my memory management and I can't see anything wrong. My data calls generally look like this:
-(void)viewDidLoad {
[super viewDidLoad];
NSOperationQueue* tmpQueue = [[NSOperationQueue alloc] init];
self.queue = tmpQueue;
[tmpQueue release];
}
-(void)loadHistory {
GetHistoryOperation* operation = [[GetHistoryOperation alloc] init];
[operation addObserver:self forKeyPath:#"isFinished" options:0 context:NULL];
[self.queue addOperation:operation];
[operation release];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqual:#"isFinished"] && [object isKindOfClass:[GetHistoryOperation class]]) {
GetHistoryOperation* operation = (GetHistoryOperation*)object;
if(operation.success) {
[self performSelectorOnMainThread:#selector(loadHistorySuceeded:) withObject:operation waitUntilDone:YES];
} else {
[self performSelectorOnMainThread:#selector(loadHistoryFailed:) withObject:operation waitUntilDone:YES];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
-(void)loadHistorySuceeded:(GetHistoryOperation*)operation {
if([operation.historyItems count] > 0) {
//display data here
} else {
//display no data alert
}
}
-(void)loadHistoryFailed:(GetHistoryOperation*)operation {
//show failure alert
}
And my operations generally looks something like this:
-(void)main {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSError* error = nil;
NSString* postData = [self postData];
NSDictionary *dictionary = [RequestHelper performPostRequest:kGetUserWalkHistoryUrl:postData:&error];
if(dictionary) {
NSNumber* isValid = [dictionary objectForKey:#"IsValid"];
if([isValid boolValue]) {
NSMutableArray* tmpDays = [[NSMutableArray alloc] init];
NSMutableDictionary* tmpWalksDictionary = [[NSMutableDictionary alloc] init];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMdd"];
NSArray* walksArray = [dictionary objectForKey:#"WalkHistories"];
for(NSDictionary* walkDictionary in walksArray) {
Walk* walk = [[Walk alloc] init];
walk.name = [walkDictionary objectForKey:#"WalkName"];
NSNumber* seconds = [walkDictionary objectForKey:#"TimeTaken"];
walk.seconds = [seconds longLongValue];
NSString* dateStart = [walkDictionary objectForKey:#"DateStart"];
NSString* dateEnd = [walkDictionary objectForKey:#"DateEnd"];
walk.startDate = [JSONHelper convertJSONDate:dateStart];
walk.endDate = [JSONHelper convertJSONDate:dateEnd];
NSString* dayKey = [dateFormatter stringFromDate:walk.startDate];
NSMutableArray* dayWalks = [tmpWalksDictionary objectForKey:dayKey];
if(!dayWalks) {
[tmpDays addObject:dayKey];
NSMutableArray* dayArray = [[NSMutableArray alloc] init];
[tmpWalksDictionary setObject:dayArray forKey:dayKey];
[dayArray release];
dayWalks = [tmpWalksDictionary objectForKey:dayKey];
}
[dayWalks addObject:walk];
[walk release];
}
for(NSString* dayKey in tmpDays) {
NSMutableArray* dayArray = [tmpWalksDictionary objectForKey:dayKey];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"startDate" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray* sortedDayArray = [dayArray sortedArrayUsingDescriptors:sortDescriptors];
[sortDescriptor release];
[tmpWalksDictionary setObject:sortedDayArray forKey:dayKey];
}
NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO selector:#selector(localizedCompare:)];
self.days = [tmpDays sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
self.walks = [NSDictionary dictionaryWithDictionary:tmpWalksDictionary];
[tmpDays release];
[tmpWalksDictionary release];
[dateFormatter release];
self.success = YES;
} else {
self.success = NO;
self.errorString = [dictionary objectForKey:#"Error"];
}
if([dictionary objectForKey:#"Key"]) {
self.key = [dictionary objectForKey:#"Key"];
}
} else {
self.errorString = [error localizedDescription];
if(!self.errorString) {
self.errorString = #"Unknown Error";
}
self.success = NO;
}
[pool release];
}
-(NSString*)postData {
NSMutableString* postData = [[[NSMutableString alloc] init] autorelease];
[postData appendFormat:#"%#=%#", #"LoginKey", self.key];
return [NSString stringWithString:postData];
}
----
#implementation RequestHelper
+(NSDictionary*)performPostRequest:(NSString*)urlString:(NSString*)postData:(NSError**)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:#"%#/%#", kHostName, urlString]];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
[urlRequest setHTTPMethod:#"POST"];
if(postData && ![postData isEqualToString:#""]) {
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
[urlRequest setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding]];
[urlRequest setValue:postLength forHTTPHeaderField:#"Content-Length"];
[urlRequest setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
}
NSURLResponse *response = nil;
error = nil;
NSData *jsonData = [NSURLConnection sendSynchronousRequest:(NSURLRequest *)urlRequest returningResponse:(NSURLResponse **)&response error:(NSError **)&error];
NSString *jsonString = [[NSString alloc] initWithBytes: [jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
NSLog(#"JSON: %#",jsonString);
//parse JSON
NSDictionary *dictionary = nil;
if([jsonData length] > 0) {
dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:error];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
return dictionary;
}
If I have the autorelease pool in place, the crash occurs on [pool release]. If I don't, then the crash just looks to appear in the main.m method, and I don't seem to get any useful information. It's difficult to track down when I have to wait 10 mins in between every test!
If anyone can offer any clues or directions to go, that'd be much appreciated.
It's almost certain you're overreleasing something in your code, seeing that the crash is occurring during a [pool release] (There's a autorelease pool in the main method as well).
You can find it using Xcode - use build and analyze to have the static analyser pinpoint potential problems. Run it and post the results.
try this:
http://cocoadev.com/index.pl?NSZombieEnabled
also, you should avoid:
1) calling UIKit methods from secondary threads
2) making (synchronous) url requests from the main thread.
you must be doing one in any case in RequestHelper's performPostRequest method.
My guess is this section
GetHistoryOperation* operation = (GetHistoryOperation*)object;
if(operation.success) {
[self performSelectorOnMainThread:#selector(loadHistorySuceeded:) withObject:operation waitUntilDone:YES];
} else {
[self performSelectorOnMainThread:#selector(loadHistoryFailed:) withObject:operation waitUntilDone:YES];
}
If the sleep happens at a bad point here, you have an object being passed to another thread. I'd find a way around having to pass the operation as the object.
This is a really old question, so sorry for the dredge, but there is no accepted answer.
I was also getting a EXC_BAD_ACCESS on NSOperationQueue -addOperation for seemingly no reason, and after a few days of hunting down memory leaks, and turning on all the debugger options i could find (malloc guard, zombies) and getting nothing, I found an NSLog warning that said: "[NSoperation subclass] set to IsFinished before being started by the queue."
When I modified my base operation subclass, so that its -cancel function only set (IsRunning = NO) and (IsFinished = YES) IF AND ONLY IF (IsRunning == YES), NSOperationQueue stopped crashing.
So if you're ever calling NSOperationQueue -cancelAllOperations, or you're doing that manually (i.e. for (NSOperation *op in queue.allOperations) ) double check to make sure that you don't set IsFinished on those operations in your subclass implementation.

Incorrect decrement of the reference count of an object that is not owned at this point by the caller

Incorrect decrement of the reference count of an object that is not owned at this point by the caller on iPhone. It is happening with NSString which I clearly init and release within the for loop. I have tried to do the same as an autoreleases string but I get leaks. I assume the culprit is the stringbytrimming call. Any suggestions, by the way this does not leak, but I get the warning in build and analyze. Everything also works fine and the app does not crash.
for(int i=0;i<storyQuantity;i++) {
NSString *imageString = [[NSString alloc] init];
imageString = [[[storiesArray objectAtIndex:i] objectForKey: #"image"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; // must add trimming to remove characters
imageLoader *imageOperation = [[imageLoader alloc] initWithImageURL:imageString target:self action:#selector(didImageLoad:) number:i];
AppDelegate_iPad *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.queue_ addOperation:imageOperation];
[imageOperation release];
[imageString release];
}
UPDATE - added my imageLoader class, which to the best of my knowledge does not have a leak
- (id)initWithImageURL:(NSString *)url target:(id)target action:(SEL)action number:(int)number {
if(self = [super init]) {
_action = action;
_target = target;
_number = number;
if(url == nil) {
return nil;
} else {
_imgURL = [[NSURL alloc] initWithString:[url copy]];
}
}
return self;
}
- (id)main {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
if ([self isCancelled]) {
NSLog(#"OPERATION CANCELLED");
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[pool drain];
return nil;
} else {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSData *imgData = [[NSData alloc] initWithContentsOfURL:_imgURL];
UIImage *image = [[UIImage alloc] initWithData:imgData];
[imgData release];
if ([self isCancelled]) {
NSLog(#"OPERATION CANCELLED");
[image release];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[pool drain];
return nil;
} else {
NSNumber *tempNumber = [NSNumber numberWithInt:_number];
NSDictionary *tempDict = [NSDictionary dictionaryWithObjectsAndKeys:tempNumber, #"number", image, #"image", nil];
[image release];
if([_target respondsToSelector:_action])
[_target performSelectorOnMainThread:_action withObject:tempDict waitUntilDone:NO];
}
}
[pool drain];
return nil;
}
- (void)dealloc {
[_imgURL release];
[super dealloc];
}
Since you are reassigning the imageString variable, the reference to the original object is lost. Why allocate an empty string anyway? Just change the code to
NSString *imageString = [[[storiesArray objectAtIndex:i] objectForKey: #"image"]
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
and remove the [imageString release] and you're good to go.
Don't track reference counts as a way into understanding memory management. It's only going to confuse you. Things manipulate your objects' reference counts from deep in the framework, and if you watch those numbers jump around for (apparently) no reason, you'll just go insane and post a series of increasingly crazy questions here, which we'll then have to deal with. Believe me--we've seen it before.
So just ignore the reference count number, and make sure you're retaining and releasing objects properly.

iPhone Objective-C: Exact same method call, working the first time, but getting "unrecognized selector" the second

I am working with a UITableViewController. The method that fails is:
- (void) testLoc {
self.dictionary = [self.delegate getLocationsWithLatitude:#"0.0" longitude:#"0.0"]; //HERE
[self setUpTableArray:dictionary];
}
Test results have shown that the exception is thrown in the first line of testLoc.
- (NSDictionary *) getLocationsWithLatitude:(NSString *)latitude longitude:(NSString *)longitude;
All I do in the above method is make an HTTP request and return an NSDictionary.
Below is my viewDidLoad (the first method call, which works) for the UITableViewController:
- (void)viewDidLoad {
self.delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[self testLoc];
[super viewDidLoad];
}
And here is my viewWillAppear, which I get "unrecognized selector" from:
- (void)viewWillAppear:(BOOL)animated {
self.delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[self testLoc];
[super viewWillAppear:animated];
}
Here is the error that I am getting:
-[NSCFString key]: unrecognized selector sent to instance 0x141770
The reason I am doing this is because I have a table view that I want to update every time I tab back to it with a tab bar.
This is for the first real app that I am building, and I would really appreciate any kind of insight. Thanks!!
UPDATE
Here is getLocationsWithLatitude:
- (NSDictionary *) getLocationsWithLatitude:(NSString *)latitude longitude:(NSString *)longitude {
OAMutableURLRequest *locationsRequest = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"http://somerequesturl"]
consumer:self.globals.consumer
token:self.globals.requestToken
realm:nil signatureProvider:nil];
[locationsRequest setHTTPMethod:#"GET"];
[locationsRequest setParameters:[NSArray arrayWithObjects:
[OARequestParameter requestParameter:#"geolat" value:latitude],
[OARequestParameter requestParameter:#"geolong" value:longitude],
nil]];
[locationsRequest prepare];
NSData *locationsData = [NSURLConnection sendSynchronousRequest:locationsRequest returningResponse:nil error:nil];
NSString *locationsString = [[[NSString alloc] initWithData:locationsData encoding:NSUTF8StringEncoding] autorelease];
[locationsRequest release];
SBJSON *jsonParser = [SBJSON new];
NSError *error = nil;
return [jsonParser objectWithString:locationsString error:&error];
}
Here is setUpTableArray:
- (void) setUpTableArray:(NSDictionary *)dict {
NSArray *array = [dict objectForKey:#"groups"];
if (array != nil) {
self.placesArray = array;
}
}
What happens if you change viewWillAppear like this:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self testLoc];
}
Wow, I definitely found my own problem this time. Just get rid of [locationsRequest prepare]; I definitely saw code somewhere that has that, but this tells me otherwise.
EDIT
Turns out this was a memory-memory allocation error. Getting rid of that line wasn't the actual fix. It seems to be some problem with a string being auto-released. I asked a follow up question here.

Why would delaying a thread response fix view corruption?

6 times out of 10 my very simple iPhone app is getting a corrupted display on launch or crashes randomly. But it behaves fine in the simulator. The display corruption looks like mis-colored fonts, out of place font text, wrong background colors, etc.
I've found a strange work-around.. when my thread delays by 2 seconds before calling the "done" notification, everything works swimmingly. The thread reads a web page and the "done" notification loads up a PickerView with strings. So what gives? Can I not safely initiate a threaded task from viewDidLoad?
- (void) loadWebPage:(NSString *)urlAddition {
NSAutoreleasePool *subPool = [[NSAutoreleasePool alloc] init];
NSString *pageSource;
NSError *err;
NSString *urlString = [NSString stringWithFormat:#"http://server/%#",
urlAddition];
pageSource = [NSString stringWithContentsOfURL:[NSURL URLWithString: urlString]
encoding:NSUTF8StringEncoding
error:&err];
[NSThread sleepForTimeInterval:2.0]; // THIS STOPS THE DISPLAY CORRUPTION
[[NSNotificationCenter defaultCenter] postNotificationName:#"webDoneNotification"
object:nil];
[subPool drain];
}
- (void) webDoneNotification: (NSNotification *)pNotification {
[mediaArray release];
mediaArray = [[NSArray arrayWithObjects:
[NSString stringWithString:#"new pickerview text"],
nil] retain];
[mediaPickerView reloadAllComponents];
[mediaPickerView selectRow:0
inComponent:0
animated:NO];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
mediaArray = [[NSArray arrayWithObjects:
[NSString stringWithString:#"init pickerview text"],
nil] retain];
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
myWebThread = [[WebThread alloc] initWithDelegate:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(webDoneNotification:)
name:#"webDoneNotification"
object:nil];
[myWebThread performSelectorInBackground:#selector(loadWebPage:) withObject:#""];
}
Thanks!
Update: Even a delay of 0.1 seconds is enough to completely fix the problem.
You're updating the view from a background thread. This is what's causing your problems; UIKit views are not thread-safe. When you need to alter the view, do it in the main thread.