Quote:
Originally Posted by PhoneyDeveloper
There are a number of memory leaks and other oddnesses in this code.
Objects created with mutableCopy must be released, yours are not. Objects created with alloc/init, like your assiString, must be released, yours is not.
This line is wrong and will crash:
Code:
NSArray *keys = [NSArray arrayWithObjects:[nextDict2 allKeys]];
arrayWithObjects is a varargs function that expects a nil terminated list of objects, you've only passed a single object, so it will crash. It is also wrong because there is no need to convert one array into another array, which seems to be what you're trying to do. This would be the usual way to get an array of the keys:
Code:
NSArray* keys = [nextDict2 allKeys];
There is no need to convert a string into another string with stringWithFormat. Code like the following is just weird:
Code:
NSString *assiString = [[NSString alloc] initWithFormat:@"%@", [assignmentsArray objectAtIndex:indexPath.row]];
Try this instead:
Code:
NSString *assiString = [assignmentsArray objectAtIndex:indexPath.row];
You don't need to release the string in this case.
I would not have the synchronize call inside the loop.
|
thanks for the suggestions, but I tried them and it still crashes, but it ha an error now but i cant find out what it means so here is my code:
Code:
- (void) cancelPressed {
HWPlannerAppDelegate *appDelegate = (HWPlannerAppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate resetAll];
NSMutableDictionary *startDict2 = [NSMutableDictionary dictionaryWithDictionary:[[[NSUserDefaults standardUserDefaults] objectForKey:nameString] mutableCopy]];
NSMutableDictionary *nextDict2 = [NSMutableDictionary dictionaryWithDictionary:[[startDict2 objectForKey:@"Assignments"] mutableCopy]];
if ([nextDict2 count] > 0) {
NSArray *keys = [nextDict2 allKeys];
NSString *assiString = [NSString stringWithFormat:@"%@", [nameAndDesc objectAtIndex:0]];
int k = 0;
for (k = 0;k < [keys count];k++) {
if ([assiString isEqualToString:[NSString stringWithFormat:@"%@", [keys objectAtIndex:k]]]) {
[nextDict2 removeObjectForKey:[NSString stringWithFormat:@"%@", [keys objectAtIndex:k]]];
}
}
[startDict2 setObject:nextDict2 forKey:@"Assignments"];
[[NSUserDefaults standardUserDefaults] setObject:startDict2 forKey:nameString];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[reloadTimer invalidate];
[self removeFromSuperview];
}
and the error i am getting is:
Code:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIButtonContent objectAtIndex:]: unrecognized selector sent to instance 0x54a860'
thanks