奇怪的错误 - 可以访问 NSURLConnection & NSMutableData 的描述方法,但不是它们的 ivar

发布于 2024-11-26 16:04:26 字数 2650 浏览 1 评论 0原文

所以我上课了:
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 code> (例如,任何 ivar 都可以工作)它会因相同的信号而崩溃。

我似乎无法掌握问题出在哪里,而且我已经安静地寻找它一段时间了,我将不胜感激一些帮助:)
Tnx!

哦,我用以下代码调用它:
<代码> DataFetcher *fetcher = [[DataFetcher alloc] init];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]]; [获取器 fetchDataWithRequest:请求]; [获取器释放]; [请求释放];

so I got class:
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

Normally it crashes on account of EXC_BAD_ACCESS ( NSZombie wont detect anything ).
I noticed that if you switch on connectionDidFinishLoading the self.recievedData.length to just self.recievedData, it wont crash and looks like there is some data in there. the same thing I get for the self.networkConnection - if I just print self.networkConnection, its ok, but if I print self.networkConnection.retainCount ( for example, any ivar will work ) it will crash with the same signal.

I can't seem to grasp where the problem is, and I've been looking for it for quiet a while, And I will appreciate some help with this :)
Tnx!

Oh, and I call it with this code:

DataFetcher *fetcher = [[DataFetcher alloc] init];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[fetcher fetchDataWithRequest:request];
[fetcher release];
[request release];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦开始←不甜 2024-12-03 16:04:26
NSLog(@"%@", self.recievedData.length);

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

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

Here %@ format specifier means that you're going to log objective-c object, while you actuall pass plain integer to it - that makes your app crash. Change your log to

 NSLog(@"%d", self.recievedData.length); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文