issue with delegate Thread 1:Program recieve bad signal "EXC_BAD_ACCESS"
within the applicationDidEnterBackground method my program receives the bad signal "EXC_BAD_ACCESS" error. on the [self goingOffline]; line
-(void)goingOffline
{
NSLog(@"going offline");
profileViewController * theController;
NSArray * viewControllers = rootController.viewControllers;
for ( UIViewController * viewController in viewControllers ) {
if ( [viewController isMemberOfClass:[profileViewController class]] ) {
theController = (profileViewController *)viewController;;
}
}
NSString *userID = theController.userId;
NSMutableData *data = [NSMutableData data];
NSMutableString *userString = [[NSMutableString alloc] initWithFormat:@"id=%@", userID];
//NSLog(userString);
//NSLog(numberString);
[data appendData:[userString dataUsingEncoding:NSUTF8StringEncoding]];
NSURL *url = [NSURL URLWithString:@"http://www.blah.net/offline.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"responseData: %@", responseData);
[userID release];
[data release];
[request release];
[url release];
[userString release];
[response release];
[err release];
[responseData release];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Overr开发者_运维技巧ide point for customization after application launch.
[self.window addSubview:rootController.view];
[window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
[self goingOffline];
}
Some of your variables are autoreleased, so they mustn't be released (they will be relaeased automatically) :
- data
- request
- url
- response
- err
- responseData
Plus, userID belongs to another object (theController)
You only call release when you call
- alloc
- new
- copy
- retain
explicitly on a variable, or any method starting by copy/new (copyWithZone:,newWithFoo:)...
So replace
[userID release];
[data release];
[request release];
[url release];
[userString release];
[response release];
[err release];
[responseData release];
By
[userString release];
since userString is the only variable you alloc explicitly.
This will fix the issue, and your object should not leak.
Also theController seems to be an instance variable, so you may want to have ownership on it :
if ( [viewController isMemberOfClass:[profileViewController class]] ) {
theController = [(profileViewController *)viewController retain];
}
Or, if it is a property synthesized with retain
if ( [viewController isMemberOfClass:[profileViewController class]] ) {
self.theController = (profileViewController *)viewController;
}
Instead of
if ( [viewController isMemberOfClass:[profileViewController class]] ) {
theController = (profileViewController *)viewController;;
}
(I just realized you also have two semicolons at the end of the line)
And add in your dealloc method :
-(void) dealloc
{
//...release the other objects you have ownership on
[theController release];
[super dealloc];
}
If you're new to Objective-C, you may want to have a look at the Apple documentation about memory management (or any resource on this topic). Try to read it carefully, it's not very difficult, but it's the most confusing thing if you don't do it right.
精彩评论