奇怪的错误 - 可以访问 NSURLConnection & NSMutableData 的描述方法,但不是它们的 ivar
所以我上课了:
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 不会检测到任何东西)。 我注意到,如果您将 connectionDidFinishLoading
的 self.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里 %@ 格式说明符意味着您将记录 Objective-C 对象,而您实际上将普通整数传递给它 - 这会使您的应用程序崩溃。将您的日志更改为
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