2

私はメンテナンス段階にあるプロジェクトの 1 つに取り組んでいるので、大きなチャンクを一度に変更する自由はあまりありません。これに取り組んでいる間、アプリケーションの実行中にシミュレーターとデバイスの両方でこのランダムなクラッシュが見られます。サーバーと同期します。フローは、 ログイン -> ローカル データをサーバーにアップロード -> サーバーからデータをダウンロードします。

ここに画像の説明を入力

私がデバッグできる唯一のポイントは、私が添付したスクリーンショットであり、そこから多くの推論を引き出すことはできません。 ここに画像の説明を入力

ネットワーク通信は、次のようなカスタム クラスを介して促進されます。

httpClient.h -------

#define TIMEOUT_SEC     30.0

@protocol HttpClientEventHandler_iPhone
@optional
- (void)requestSucceeded;
- (void)requestFailed:(NSError*)error;
@end

@class Reachability;
@interface HttpClient_iPhone : NSObject{
    NSURLConnection *connection;
    NSMutableData *recievedData;
    int statusCode; 
    id delegate;
    Reachability* hostReachable;
    BOOL networkChecked;
}
@property (nonatomic, retain) NSMutableString *previousRequest;
@property (readonly) NSMutableData *recievedData; // XX should be (readonly, retain)
@property (readonly) int statusCode;
@property (nonatomic, assign) id delegate;

+ (NSString*)stringEncodedWithBase64:(NSString*)str;
+ (NSString*) stringOfAuthorizationHeaderWithUsername:(NSString*)username password:(NSString*)password;
- (NSMutableURLRequest*)makeRequest:(NSString*)url;
- (NSMutableURLRequest*)makeRequest:(NSString*)url username:(NSString*)username password:(NSString*)password;
- (void)prepareWithRequest:(NSMutableURLRequest*)request;
- (void)requestGET:(NSString*)url;
- (void)requestPOST:(NSString*)url body:(NSString*)body type:(NSString*)type;
- (void)requestGET:(NSString*)url username:(NSString*)username password:(NSString*)password;
- (void)requestPOST:(NSString*)url username:(NSString*)username password:(NSString*)password body:(NSString*)body type:(NSString*)type;
- (void)requestPOST:(NSString*)url username:(NSString*)username password:(NSString*)password bodydata:(NSData*)body contenttype:(NSString*)type;
- (void)uploadImage:(NSString*)requesturl
           username:(NSString*)username
           password:(NSString*)password
          imagename:(NSString*)imagename 
        contenttype:(NSString*)contenttype
          imagedata:(NSData*)imagedata;

- (void)cancelTransaction;
- (void)reset;
- (BOOL)checkNetworkStatus;
@end

//HttpClient.m -----------

#import "HttpClient_iPhone.h"
#import "Base64.h"
#import "Reachability.h"
#import "SyncLiteral.h"

@implementation HttpClient_iPhone

@synthesize recievedData, statusCode;
@synthesize delegate,previousRequest;

- (id)init {
    if (self = [super init]) {
        [self reset];
        delegate = nil;
        networkChecked = NO;
    }
    return self;
}

- (void)dealloc {
    [connection release];
    [recievedData release];
    [super dealloc];
}

- (void)reset {
    [recievedData release];
    recievedData = [[NSMutableData alloc] init];
    [connection release];
    connection = nil;
    statusCode = 0; 
    networkChecked = NO;
}

+ (NSString*)stringEncodedWithBase64:(NSString*)str {
    static const char *tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    const char *s = [str UTF8String];
    int length = [str length];
    char *tmp = malloc(length * 4 / 3 + 4);

    int i = 0;
    int n = 0;
    char *p = tmp;

    while (i < length) {
        n = s[i++];
        n *= 256;
        if (i < length) n += s[i];
        i++;
        n *= 256;
        if (i < length) n += s[i];
        i++;

        p[0] = tbl[((n & 0x00fc0000) >> 18)];
        p[1] = tbl[((n & 0x0003f000) >> 12)];
        p[2] = tbl[((n & 0x00000fc0) >>  6)];
        p[3] = tbl[((n & 0x0000003f) >>  0)];

        if (i > length) p[3] = '=';
        if (i > length + 1) p[2] = '=';
        p += 4;
    }
    *p = '\0';
    NSString *ret = [NSString stringWithCString:(const char*)tmp encoding: NSUTF8StringEncoding];
    free(tmp);
    return ret;
}

#pragma mark - HTTP Request creating methods

- (NSMutableURLRequest*)makeRequest:(NSString*)url {
    NSString *encodedUrl = (NSString*)CFURLCreateStringByAddingPercentEscapes(
                                                                              NULL, (CFStringRef)url, NULL, NULL, kCFStringEncodingUTF8);
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:encodedUrl]];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
    [request setTimeoutInterval:TIMEOUT_SEC];
    [request setHTTPShouldHandleCookies:FALSE];
    [encodedUrl release];
    return request;
}

- (NSMutableURLRequest*)makeRequest:(NSString*)url username:(NSString*)username password:(NSString*)password {
    NSMutableURLRequest *request = [self makeRequest:url];
    [request setValue:[HttpClient_iPhone stringOfAuthorizationHeaderWithUsername:username password:password]
   forHTTPHeaderField:@"Authorization"];
    return request;
}

+ (NSString*) stringOfAuthorizationHeaderWithUsername:(NSString*)username password:(NSString*)password {
    return [@"Basic " stringByAppendingString:[HttpClient_iPhone stringEncodedWithBase64:
                                               [NSString stringWithFormat:@"%@:%@", username, password]]];
}

- (void)prepareWithRequest:(NSMutableURLRequest*)request {
    // do nothing (for OAuthHttpClient)
}

#pragma mark -
#pragma mark HTTP Transaction management methods

/* Sending the Http Request for "GET" */
- (void)requestGET:(NSString*)url {

    //Reseting the http client
    [self reset];

    //Checking the internet connection
    if ([self checkNetworkStatus] == NO){
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
            [self.delegate performSelector:@selector(requestFailed:) withObject:nil];
        }       
        return;
    }
    //Sending the http requqest
    NSMutableURLRequest *request = [self makeRequest:url];
    [self prepareWithRequest:request];

    if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
        self.previousRequest = [NSMutableString string];
    [self.previousRequest appendFormat:@"GET Requested API - %@\n",url];
    [self.previousRequest appendFormat:@"\n\n\n"];

    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}

/* Sending the Http Request for "POST" */
- (void)requestPOST:(NSString*)url body:(NSString*)body type:(NSString*)type {

    //Reseting the http client
    [self reset];
    //Checking the internet connection
    if ([self checkNetworkStatus] == NO){
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
            [self.delegate performSelector:@selector(requestFailed:) withObject:nil];
        }       
        return;
    }
    //Sending the http requqest
    NSMutableURLRequest *request = [self makeRequest:url];
    [request setHTTPMethod:@"POST"];
    if (type != nil && ![type isEqualToString:@""])
        [request setValue:type forHTTPHeaderField:@"Content-Type"]; 
    if (body) {
        [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [self prepareWithRequest:request];
    if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
        self.previousRequest = [NSMutableString string];
    [self.previousRequest appendFormat:@"Post Requested API - %@\n",url];
    [self.previousRequest appendFormat:@"Post Data - %@\n",body];
    [self.previousRequest appendFormat:@"\n\n\n"];
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

/* Sending the Http Request for "GET" with username and password */
- (void)requestGET:(NSString*)url username:(NSString*)username password:(NSString*)password {
    //Reseting the http client
    [self reset];

    //Checking the internet connection
    if ([self checkNetworkStatus] == NO){
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
            [self.delegate performSelector:@selector(requestFailed:) withObject:nil];
        }       
        return;
    }
    //Sending the http requqest
    NSMutableURLRequest *request = [self makeRequest:url username:username password:password];
    if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
        self.previousRequest = [NSMutableString string];
    [self.previousRequest appendFormat:@"Post Requested API - %@\n",url];
    [self.previousRequest appendFormat:@"Authorization user - %@\n",username];
    [self.previousRequest appendFormat:@"Authorizating password - %@\n",password];
    [self.previousRequest appendFormat:@"\n\n\n"];
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}

/* Sending the Http Request for "POST" with username and password */
- (void)requestPOST:(NSString*)url username:(NSString*)username password:(NSString*)password body:(NSString*)body type:(NSString*)type {
    //Reseting the http client
    [self reset];
    //Checking the internet connection
    if ([self checkNetworkStatus] == NO){
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
            [self.delegate performSelector:@selector(requestFailed:) withObject:nil];
        }       
        return;
    }
    //Sending the http requqest
    NSMutableURLRequest *request = [self makeRequest:url username:username password:password];
    [request setHTTPMethod:@"POST"];
    if (type != nil && ![type isEqualToString:@""])
        [request setValue:type forHTTPHeaderField:@"Content-Type"]; 
    if (body) {
        [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [self prepareWithRequest:request];
    if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
        self.previousRequest = [NSMutableString string];
    [self.previousRequest appendFormat:@"Post Requested API - %@\n",url];
    [self.previousRequest appendFormat:@"Authorization user - %@\n",username];
    [self.previousRequest appendFormat:@"Authorizating password - %@\n",password];
    [self.previousRequest appendFormat:@"Post Data - %@\n",body];
    [self.previousRequest appendFormat:@"\n\n\n"];
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

/* Sending the Http Request for "POST" with username and password */
- (void)requestPOST:(NSString*)url username:(NSString*)username password:(NSString*)password bodydata:(NSData*)body contenttype:(NSString*)type {
    //Reseting the http client
    [self reset];
    //Checking the internet connection
    if ([self checkNetworkStatus] == NO){
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
            [self.delegate performSelector:@selector(requestFailed:) withObject:nil];
        }       
        return;
    }
    //Sending the http requqest
    NSMutableURLRequest *request = [self makeRequest:url username:username password:password];
    [request setHTTPMethod:@"POST"];
    if (type != nil && ![type isEqualToString:@""])
        [request setValue:type forHTTPHeaderField:@"Content-Type"]; 
    if (body) {
        [request setHTTPBody:body];
    }
    if (body != nil && [body length] > 0){
        NSString* length_str = [NSString stringWithFormat:@"%d", [body length]];
        [request setValue:length_str forHTTPHeaderField:@"Content-Length"];
    }
    [self prepareWithRequest:request];
    if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
        self.previousRequest = [NSMutableString string];
    [self.previousRequest appendFormat:@"Post Requested API - %@\n",url];
    [self.previousRequest appendFormat:@"Authorization user - %@\n",username];
    [self.previousRequest appendFormat:@"Authorizating password - %@\n",password];
    [self.previousRequest appendFormat:@"Post Data - %@\n",[[[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding]autorelease]];
    [self.previousRequest appendFormat:@"\n\n\n"];

    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

/* Sending the Http Request for uploading the image from database */
- (void)uploadImage:(NSString*)requesturl
           username:(NSString*)username
           password:(NSString*)password
          imagename:(NSString*)imagename 
        contenttype:(NSString*)contenttype
          imagedata:(NSData*)imagedata {

    //Reseting the http client
    [self reset];

    //Checking the internet connection
    if ([self checkNetworkStatus] == NO){
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
            [self.delegate performSelector:@selector(requestFailed:) withObject:nil];
        }       
        return;
    }
    //Sending the http requqest
    NSString *boundary = @"---------------------------14737809831466499882746641449"; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
    NSURL *url = [NSURL URLWithString:requesturl];

    NSMutableURLRequest *request = [self makeRequest:requesturl username:username password:password];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *postbody = [NSMutableData data]; 
    [postbody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadfile\"; filename=\"%@\"\r\n", imagename] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    NSData* encoded_data = [Base64 encode:imagedata];
    [postbody appendData:[NSData dataWithData:encoded_data]];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [encoded_data release];
    [request setHTTPBody:postbody]; 
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

/* Canceling the HTTP Transaction */
- (void)cancelTransaction {
    [connection cancel];
    [self reset];
}

#pragma mark -
#pragma mark NSURLConnectionDelegate methods

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
    return nil;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    statusCode = [(NSHTTPURLResponse*)response statusCode];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [recievedData appendData:data];
#ifdef _DEBUG
    NSLog(@"Receieved the http body data : \n%@\n", [[[NSString alloc] initWithData:data 
                                        encoding:NSASCIIStringEncoding] autorelease]);
#endif
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
#ifdef DEBUG
    NSLog(@"Receieved the http body data : \n%@\n", [[[NSString alloc] initWithData:recievedData
                                                                           encoding:NSASCIIStringEncoding] autorelease]);
#endif
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestSucceeded)]){
        [self.delegate performSelector:@selector(requestSucceeded) withObject:nil];
    }
    [self reset];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError*) error {
#ifdef _DEBUG   
    NSLog(@"didFailWithError \n %@",[error description]);
#endif
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
        [self.delegate performSelector:@selector(requestFailed:) withObject:error];
    }   
    [self reset];
}

//check network status chages
- (BOOL)checkNetworkStatus {
    BOOL result = NO;
    Reachability* reachability = [Reachability reachabilityWithHostName:SERVER_ADDRESS];
    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];  

    if(remoteHostStatus == NotReachable) { 
#ifdef _DEBUG       
        NSLog(@"\nNetwork Status : not reachable\n");
#endif
        result = NO;
    }else if (remoteHostStatus == ReachableViaWWAN) { 
#ifdef _DEBUG       
        NSLog(@"\nNetwork Status : reachable via wwan\n");
#endif
        result = YES;
    }else if (remoteHostStatus == ReachableViaWiFi) { 
#ifdef _DEBUG
        NSLog(@"\nNetwork Status : reachable via wifi\n");
#endif
        result = YES;       
    }
    return result;
}

@終わり

誰かが私を正しい方向に導くことができれば、それは大きな助けになるでしょう. プロジェクトは、展開ターゲット 4.3 で SDK 6.1 を使用します。

//私の質問に編集

現在の実装は次のとおりです。

@interface HttpClient_iPhone : NSObject{
    NSURLConnection *connection;
    NSMutableData *recievedData;
    int statusCode; 
    id delegate;
    Reachability* hostReachable;
    BOOL networkChecked;
}

@property (retain,atomic) NSMutableString *previousRequest;
@property (retain,readonly) NSMutableData *recievedData;
@property (readonly) int statusCode;
@property (nonatomic, assign) id delegate;

対応しています.mは今です

@implementation HttpClient_iPhone

@synthesize recievedData, statusCode;
@synthesize delegate,previousRequest;

- (id)init {
    if (self = [super init]) {
        [self reset];
        delegate = nil;
        networkChecked = NO;
    }
    return self;
}

- (void)dealloc {
    if (connection) {
        [connection cancel];
    }
    [connection release];
    connection = nil;

    if (recievedData) {
        [recievedData release];
        recievedData = nil;
    }
    [super dealloc];
}

- (void)reset {
    if (recievedData) {
        [recievedData release];
        recievedData = nil;
    }

    recievedData = [[NSMutableData alloc] init];

    if (connection) {
        [connection cancel];
        [connection release];
        connection = nil;
    }
    statusCode = 0;
    networkChecked = NO;
}

- (void)requestGET:(NSString*)url {

    //Reseting the http client
    [self reset];

    //Checking the internet connection
    if ([self checkNetworkStatus] == NO){
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
            [self.delegate performSelector:@selector(requestFailed:) withObject:nil];
        }       
        return;
    }

    NSMutableURLRequest *request = [self makeRequest:url];
    [self prepareWithRequest:request];
    if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
        self.previousRequest = [NSMutableString string];
    [self.previousRequest appendFormat:@"GET Requested API - %@\n",url];
    [self.previousRequest appendFormat:@"\n\n\n"];

    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}

- (void)requestPOST:(NSString*)url body:(NSString*)body type:(NSString*)type {
    [self reset];
    if ([self checkNetworkStatus] == NO){
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
            [self.delegate performSelector:@selector(requestFailed:) withObject:nil];
        }       
        return;
    }
    NSMutableURLRequest *request = [self makeRequest:url];
    [request setHTTPMethod:@"POST"];
    if (type != nil && ![type isEqualToString:@""])
        [request setValue:type forHTTPHeaderField:@"Content-Type"]; 
    if (body) {
        [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [self prepareWithRequest:request];
    if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
        self.previousRequest = [NSMutableString string];
    [self.previousRequest appendFormat:@"Post Requested API - %@\n",url];
    [self.previousRequest appendFormat:@"Post Data - %@\n",body];
    [self.previousRequest appendFormat:@"\n\n\n"];
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)requestGET:(NSString*)url username:(NSString*)username password:(NSString*)password {
    [self reset];
    if ([self checkNetworkStatus] == NO){
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
            [self.delegate performSelector:@selector(requestFailed:) withObject:nil];
        }       
        return;
    }
    NSMutableURLRequest *request = [self makeRequest:url username:username password:password];
    if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
        self.previousRequest = [NSMutableString string];
    [self.previousRequest appendFormat:@"Post Requested API - %@\n",url];
    [self.previousRequest appendFormat:@"Authorization user - %@\n",username];
    [self.previousRequest appendFormat:@"Authorizating password - %@\n",password];
    [self.previousRequest appendFormat:@"\n\n\n"];
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}

- (void)requestPOST:(NSString*)url username:(NSString*)username password:(NSString*)password body:(NSString*)body type:(NSString*)type {
    [self reset];
    if ([self checkNetworkStatus] == NO){
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
            [self.delegate performSelector:@selector(requestFailed:) withObject:nil];
        }       
        return;
    }
    NSMutableURLRequest *request = [self makeRequest:url username:username password:password];
    [request setHTTPMethod:@"POST"];
    if (type != nil && ![type isEqualToString:@""])
        [request setValue:type forHTTPHeaderField:@"Content-Type"]; 
    if (body) {
        [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [self prepareWithRequest:request];
    if(![[url lastPathComponent] isEqualToString:@"pda_errorlogs"])
        self.previousRequest = [NSMutableString string];
    [self.previousRequest appendFormat:@"Post Requested API - %@\n",url];
    [self.previousRequest appendFormat:@"Authorization user - %@\n",username];
    [self.previousRequest appendFormat:@"Authorizating password - %@\n",password];
    [self.previousRequest appendFormat:@"Post Data - %@\n",body];
    [self.previousRequest appendFormat:@"\n\n\n"];
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)requestPOST:(NSString*)url username:(NSString*)username password:(NSString*)password bodydata:(NSData*)body contenttype:(NSString*)type {
    [self reset];
}

- (void)cancelTransaction {
    [self reset];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    statusCode = [(NSHTTPURLResponse*)response statusCode];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [recievedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestSucceeded)]){
        [self.delegate performSelector:@selector(requestSucceeded) withObject:nil];
    }
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError*) error {
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(requestFailed:)]){
        [self.delegate performSelector:@selector(requestFailed:) withObject:error];
    }   
}

// Crashlytics で報告されたクラッシュ ログの画像

パート 1/2 ここに画像の説明を入力

パート 2/2 ここに画像の説明を入力

4

2 に答える 2

2

2 つのコメント:

1) ヘッダーの recievedData プロパティ タイプを「retain」に変更します (コードでプロパティとして使用されていなくても)。

2) NSURLConnection を解放する前に、それを送信する必要がありcancelます[connection cancel]。dealloc と reset で解放します。また、最初に行うことをキャンセルするようにしてください。事前に使用される可能性のあるデータを解放しないでください。

編集:現在のコードに基づいて、resetメソッドは変更可能なデータを変更する前にキャンセルします。メソッドをミラーリングするために、以下に示すようにコードを変更しましdeallocた( nil ステートメントは必要ありません)。

- (void)reset {
    if (connection) {
        [connection cancel];
        [connection release];
        connection = nil;
    }
    if (recievedData) {
        [recievedData release];
        recievedData = nil;
    }

    recievedData = [[NSMutableData alloc] init];

    statusCode = 0;
    networkChecked = NO;
}

- (void)dealloc {
    if (connection) {
        [connection cancel];
        [connection release];
    }

    if (recievedData) {
        [recievedData release];
    }
    [super dealloc];
}

それを試してみてください。

于 2013-05-17T13:15:10.370 に答える
2
  1. Zombies インストゥルメントでインストゥルメントを実行します。ほとんどの場合、これでこのような問題のトラブルシューティングに必要なすべてが得られますが、フレームワークの奥深くでバグに遭遇した場合はあまり役に立たないことがあります。あなたが提供した情報を見ると、それは可能ですが、そうではありません。クラッシュログを追加すると役立ちます。

  2. 私は ivar の使用から離れて、合成されたプロパティの使用に切り替えます。ivar への直接アクセスは、init と dealloc でのみ行う必要があります。それ以外の場合は、このプロパティを使用してください。

  3. NSURLConnection デリゲートを実装する十分な理由があることを確認してください。必要に応じて、それsendAsynchronousRequest:queue:completionHandler:を使用してください。デリゲートの複雑さに対処する必要はありません。connection:didReceiveData:NSMutableData インスタンスへのバッファリングを実装する際のミスや、正しく処理されていないために、非常に多くのクラッシュが発生するのを見てきましたcancel(キャンセルが呼び出された後は、デリゲート コールバックを取得できなくなります。これにより、デリゲートが適切にクリーンアップされない可能性があります)。 )。

  4. これらのリクエストごとにユーザー名とパスワードを取得しているようで、独自の HTTP Basic 認証ヘッダーを作成しているようです。しないでください。代わりに、NSURLCredential を使用して認証資格情報を管理します。NSURLConnection が認証チャレンジを取得すると、資格情報を適用するために NSURLCredentialStorage を調べます。これはアプリケーションに対してシームレスであり、潜在的な問題を大幅に軽減します。クレデンシャルを作成および設定する方法の簡単な例を次に示します。これは、接続ごとではなく、一度だけ行う必要があります。

    NSURLCredential         *credential         = nil;
    NSURLProtectionSpace    *protectionSpace    = nil;
    
    protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:[url host] port:[[url port] integerValue] protocol:[url scheme] realm:nil authenticationMethod:NSURLAuthenticationMethodHTTPBasic];
    credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistencePermanent];
    [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];
    [protectionSpace release];
    

利用可能なさまざまな永続化オプションがありNSURLCredentialPersistencePermanent、資格情報をキーチェーンに永続的に保存することに注意してください (必要に応じて後で削除できます)。

于 2013-07-24T19:01:47.600 に答える