【问题标题】:Why my application crash on applicationDidEnterBackground?为什么我的应用程序在 applicationDidEnterBackground 上崩溃?
【发布时间】:2014-01-06 23:50:33
【问题描述】:

我有一个名为writeToServer 的静态方法,当应用程序进入后台模式时会调用它。

在我的AppDelegate.m:

- (void)applicationDidEnterBackground:(UIApplication *) application {
    [LogZone writeToServer];
    NSLog(@"Log sended to server. Done.");
}

在我的LogZone.m:

+ (void) writeToServer {
    NSString *qStr = [[NSString alloc]
        initWithFormat:@"%@?ip=%@&uid=%@&platform=%@&model=%@&lat=%@&lon=%@",
            LOG_SERVER_URL,
            _LOG_IP, _LOG_UID, _LOG_PLAT, _LOG_MOD, _LOG_LAT, _LOG_LON];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:qStr]];    
    [request setHTTPMethod: @"POST"];
    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
}

大写的 var 是以这种方式创建的静态字符串:

.h

extern NSString* _LOG_UID;

.m

NSString* _LOG_UID = @"-1";

当我进入后台模式时,它会因“classic”错误而崩溃:

* -[CFString respondsToSelector:]:消息发送到释放的实例 0x6a4c800

但是为什么呢?
我什么都不发布!=!

怎么了?

【问题讨论】:

  • 从调试器发布堆栈跟踪。
  • 我做不到。它只显示“2011-01-22 21:02:02.314 myApp[8709:207] *** -[CFString respondsToSelector:]: message sent to deallocated instance 0x6942bf0”在这一行:“NSString *qStr[... ]"
  • 注意objective-c没有static方法....
  • 我们中间有一位 ARC 开发人员。

标签: iphone objective-c nsurlconnection


【解决方案1】:

但是为什么呢? 我什么都不发布!

当然,但是您是否保留了正确的对象?

在正确的时间保留与不在错误的时间释放一样重要......

【讨论】:

  • 对。 +1!我解决了向我的变量添加保留的问题。 (_LOG_IP = [[设备 getIPAddress] 保留];)。可以,谢谢。
  • 优秀。如果需要,我希望您也添加了适当的版本!
【解决方案2】:

为什么不只使用以下内容:

NSString *qStr = [NSString stringWithFormat:@"%@?ip=%@&uid=%@&platform=%@&model=%@&lat=%@&lon=%@", LOG_SERVER_URL, _LOG_IP, _LOG_UID, _LOG_PLAT, _LOG_MOD, _LOG_LAT, _LOG_LON];

【讨论】:

  • 这与崩溃无关。
  • 正如预期的那样。它可以解决可能的泄漏,但绝对不能解决崩溃。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-03
  • 2014-09-20
  • 2010-09-20
  • 2020-08-22
  • 1970-01-01
相关资源
最近更新 更多