如何解决委托线程1出现问题:程序收到错误信号“ EXC_BAD_ACCESS”
| 在applicationDidEnterBackground方法中,我的程序收到错误信号“ EXC_BAD_ACCESS”错误。在[self goingOffline]上;线-(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 {
// Override 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];
}
解决方法
您的某些变量是自动释放的,因此不得释放(它们将自动释放):
数据
请求
网址
响应
呃
responseData
另外,userID属于另一个对象(theController)
您只有在拨打电话时才拨打释放电话
分配
新
复制
保留
明确地在变量上或以copy / new(copyWithZone:,newWithFoo :)开头的任何方法上...
所以更换
[userID release];
[data release];
[request release];
[url release];
[userString release];
[response release];
[err release];
[responseData release];
通过
[userString release];
因为userString是您明确分配的唯一变量。
这将解决问题,并且您的对象不应泄漏。
另外,Controller似乎是一个实例变量,因此您可能希望拥有所有权:
if ( [viewController isMemberOfClass:[profileViewController class]] ) {
theController = [(profileViewController *)viewController retain];
}
或者,如果它是与
if ( [viewController isMemberOfClass:[profileViewController class]] ) {
self.theController = (profileViewController *)viewController;
}
代替
if ( [viewController isMemberOfClass:[profileViewController class]] ) {
theController = (profileViewController *)viewController;;
}
(我刚刚意识到您在行尾也有两个分号)
并添加您的dealloc方法:
-(void) dealloc
{
//...release the other objects you have ownership on
[theController release];
[super dealloc];
}
如果您不熟悉Objective-C,则可能需要查看有关内存管理的Apple文档(或该主题的任何资源)。尝试仔细阅读它,这不是很困难,但是如果您做得不对,那将是最令人困惑的事情。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。