【问题标题】:Weird bug - can access NSURLConnection & NSMutableData's description method, but not their ivars奇怪的错误 - 可以访问 NSURLConnection 和 NSMutableData 的描述方法,但不能访问它们的 ivars
【发布时间】:2011-07-27 18:22:39
【问题描述】:

所以我上课了:
DataFetcher.h

#import <Foundation/Foundation.h>

@interface DataFetcher : NSObject 
{
    NSURLConnection *networkConnection;
    NSMutableData *recievedData;
    BOOL isFetchingFinished;
}

@property (nonatomic, retain) NSMutableData *recievedData;
@property (nonatomic, retain) NSURLConnection *networkConnection;

- (void)fetchDataWithRequest:(NSMutableURLRequest *)request;

DataFetcher.m:

#import "DataFetcher.h"

@implementation DataFetcher

@synthesize recievedData;
@synthesize networkConnection;

- (id)init
{
    self = [super init];
    if (self) {
        NSMutableData *data = [[NSMutableData alloc] initWithLength:0];
        self.recievedData = data;
        [data release];
    }

    return self;
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Connection Error:%@\n Failure Reason:%@", [error localizedDescription], [error localizedFailureReason]);
    [networkConnection release];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
}

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

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [networkConnection release];
    NSLog(@"%@", self.recievedData.length);
    [self processData];
}

- (void)fetchDataWithRequest:(NSMutableURLRequest *)request
{
    NSURLConnection *aConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
    self.networkConnection = aConnection;
    [aConnection release];
    [self.networkConnection start];
    if( !self.networkConnection )
    {
        NSLog(@"Connection failed! recieved nil at alloc point");
    }
}

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

@end



通常它会因为 EXC_BAD_ACCESS 而崩溃(NSZombie 不会检测到任何东西)。 我注意到,如果您将connectionDidFinishLoadingself.recievedData.length 切换为self.recievedData,它不会崩溃并且看起来里面有一些数据。 self.networkConnection 得到的结果相同 - 如果我只打印 self.networkConnection,没关系,但如果我打印 self.networkConnection.retainCount(例如,任何 ivar 都可以),它会因相同的信号而崩溃。

我似乎无法理解问题出在哪里,我已经安静地寻找了一段时间,我将不胜感激:)
天呐!

哦,我用这个代码调用它:
DataFetcher *fetcher = [[DataFetcher alloc] init];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]]; [fetcher fetchDataWithRequest:request]; [fetcher release]; [request release];

【问题讨论】:

    标签: nsurlconnection exc-bad-access nsmutabledata


    【解决方案1】:
    NSLog(@"%@", self.recievedData.length);
    

    这里的 %@ 格式说明符意味着您将记录 Objective-c 对象,而您实际上将纯整数传递给它 - 这会使您的应用程序崩溃。将您的日志更改为

     NSLog(@"%d", self.recievedData.length); 
    

    【讨论】:

      猜你喜欢
      • 2011-07-10
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      • 2021-02-08
      • 1970-01-01
      相关资源
      最近更新 更多