2011-06-18 63 views
0

内applicationDidEnterBackground方法我的程序收到错误信号“EXC_BAD_ACCESS”错误。在[self goingOffline]上;线问题代理线程1:程序收到坏信号“EXC_BAD_ACCESS”

-(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]; 
} 

回答

2

你的一些变量都自动释放,所以它们不能被释放(他们将被自动relaeased):

  • 数据
  • 要求
  • 网址
  • 响应
  • err
  • responseData

另外,用户ID属于其他对象(theController)

您只需致电,当你调用释放

  • 页头
  • 副本
  • 保留

明确一个变量,或通过复制/新的任何方法出发(copyWithZone:,newWithFoo:)...

所以更换

[userID release]; 
[data release]; 
[request release]; 
[url release]; 
[userString release]; 
[response release]; 
[err release]; 
[responseData release]; 

通过

[userString release]; 

因为userString是唯一的变量你明确地分配。

这将解决问题,并且您的对象不应该泄漏。

而且theController似乎是一个实例变量,所以你可能想拥有它的所有权:

 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 documentation about memory management(或任何资源对这个主题)。尽量仔细阅读,这不是很困难,但如果你做得不对,这是最令人困惑的。

+0

你怎么知道是否有东西是自动释放的,因为在我的代码中没有引用它。 – mintuz

+0

无论未分配,复制,保留还是新增,都是自动释放 – Julien

+1

像[NSURL URLWithString:@“http://www.blah.net/offline.php”]使用[NSURL URLWithString:]创建一个对象,而URLWithString不包含任何神奇的单词:alloc,retain,new,copy,所以它是自动释放的 – Julien

相关问题