开发者

Why my application crash on applicationDidEnterBackground?

开发者 https://www.devze.com 2023-02-06 17:55 出处:网络
I have a static method called writeToServer that is called when application enter in background mode.

I have a static method called writeToServer that is called when application enter in background mode.

in my AppDelegate.m:

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

in my 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];
}

Uppercase vars are static strings created in this way:

.h

extern NSString* _LOG_UID;

.m开发者_如何学JAVA

NSString* _LOG_UID = @"-1";

When I enter in background mode it crash with this "classic" error:

* -[CFString respondsToSelector:]: message sent to deallocated instance 0x6a4c800

But why?

I don't release anything!=!

What's wrong?


But why? I don't release anything!

Sure, but do you retain the right objects?

Retaining at the right time is every bit as important as not releasing at the wrong time...


Why not just use the following:

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号