iphone - 为什么我在网络通信期间会发生随机崩溃 - (CFRelease 中的崩溃)?
<p><p>我正在处理我的一个项目,该项目处于维护阶段,所以我没有太多的自由来一次更改主要 block ,在处理这个问题时,我在模拟器和设备上都看到了这个随机崩溃而应用程序与服务器同步。流动的存在
<strong>登录->上传本地数据到服务器->从服务器下载数据。</strong></p>
<p> <img src="/image/P0Tvd.png" alt="enter image description here"/> </p>
<p>我能够调试的唯一一点是我附上的屏幕截图,我无法从中得出很多推论。
<img src="/image/k4fVK.png" alt="enter image description here"/> </p>
<p>网络通信是通过自定义类促进的,如下所示</p>
<p>httpClient.h -------</p>
<pre><code>#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
</code></pre>
<p>//HttpClient.m ------------</p>
<pre><code>#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 = ) {
;
delegate = nil;
networkChecked = NO;
}
return self;
}
- (void)dealloc {
;
;
;
}
- (void)reset {
;
recievedData = [ init];
;
connection = nil;
statusCode = 0;
networkChecked = NO;
}
+ (NSString*)stringEncodedWithBase64:(NSString*)str {
static const char *tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char *s = ;
int length = ;
char *tmp = malloc(length * 4 / 3 + 4);
int i = 0;
int n = 0;
char *p = tmp;
while (i < length) {
n = s;
n *= 256;
if (i < length) n += s;
i++;
n *= 256;
if (i < length) n += s;
i++;
p = tbl[((n & 0x00fc0000) >> 18)];
p = tbl[((n & 0x0003f000) >> 12)];
p = tbl[((n & 0x00000fc0) >>6)];
p = tbl[((n & 0x0000003f) >>0)];
if (i > length) p = '=';
if (i > length + 1) p = '=';
p += 4;
}
*p = '\0';
NSString *ret = ;
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 = [[ init] autorelease];
];
;
;
;
;
return request;
}
- (NSMutableURLRequest*)makeRequest:(NSString*)url username:(NSString*)username password:(NSString*)password {
NSMutableURLRequest *request = ;
forHTTPHeaderField:@"Authorization"];
return request;
}
+ (NSString*) stringOfAuthorizationHeaderWithUsername:(NSString*)username password:(NSString*)password {
return [@"Basic " stringByAppendingString:[HttpClient_iPhone stringEncodedWithBase64:
]];
}
- (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
;
//Checking the internet connection
if ( == NO){
if (self.delegate != nil && ){
;
}
return;
}
//Sending the http requqest
NSMutableURLRequest *request = ;
;
if(![ isEqualToString:@"pda_errorlogs"])
self.previousRequest = ;
;
;
connection = [ 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
;
//Checking the internet connection
if ( == NO){
if (self.delegate != nil && ){
;
}
return;
}
//Sending the http requqest
NSMutableURLRequest *request = ;
;
if (type != nil && !)
;
if (body) {
];
}
;
if(![ isEqualToString:@"pda_errorlogs"])
self.previousRequest = ;
;
;
;
connection = [ 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
;
//Checking the internet connection
if ( == NO){
if (self.delegate != nil && ){
;
}
return;
}
//Sending the http requqest
NSMutableURLRequest *request = ;
if(![ isEqualToString:@"pda_errorlogs"])
self.previousRequest = ;
;
;
;
;
connection = [ 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
;
//Checking the internet connection
if ( == NO){
if (self.delegate != nil && ){
;
}
return;
}
//Sending the http requqest
NSMutableURLRequest *request = ;
;
if (type != nil && !)
;
if (body) {
];
}
;
if(![ isEqualToString:@"pda_errorlogs"])
self.previousRequest = ;
;
;
;
;
;
connection = [ 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
;
//Checking the internet connection
if ( == NO){
if (self.delegate != nil && ){
;
}
return;
}
//Sending the http requqest
NSMutableURLRequest *request = ;
;
if (type != nil && !)
;
if (body) {
;
}
if (body != nil && > 0){
NSString* length_str = ];
;
}
;
if(![ isEqualToString:@"pda_errorlogs"])
self.previousRequest = ;
;
;
;
initWithData:body encoding:NSUTF8StringEncoding]autorelease]];
;
connection = [ 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
;
//Checking the internet connection
if ( == NO){
if (self.delegate != nil && ){
;
}
return;
}
//Sending the http requqest
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = ;
NSURL *url = ;
NSMutableURLRequest *request = ;
;
NSMutableData *postbody = ;
dataUsingEncoding:NSUTF8StringEncoding]];
dataUsingEncoding:NSUTF8StringEncoding]];
];
NSData* encoded_data = ;
];
dataUsingEncoding:NSUTF8StringEncoding]];
;
;
;
;
connection = [ initWithRequest:request delegate:self];
}
/* Canceling the HTTP Transaction */
- (void)cancelTransaction {
;
;
}
#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 {
;
#ifdef _DEBUG
NSLog(@"Receieved the http body data : \n%@\n", [[ initWithData:data
encoding:NSASCIIStringEncoding] autorelease]);
#endif
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
#ifdef DEBUG
NSLog(@"Receieved the http body data : \n%@\n", [[ initWithData:recievedData
encoding:NSASCIIStringEncoding] autorelease]);
#endif
if (self.delegate != nil && ){
;
}
;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError*) error {
#ifdef _DEBUG
NSLog(@"didFailWithError \n %@",);
#endif
if (self.delegate != nil && ){
;
}
;
}
//check network status chages
- (BOOL)checkNetworkStatus {
BOOL result = NO;
Reachability* reachability = ;
NetworkStatus remoteHostStatus = ;
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;
}
</code></pre>
<p>@结束</p>
<p>如果有人可以指导我正确的方向,那将有很大帮助。该项目使用 SDK 6.1 和部署目标 4.3。</p>
<p>//编辑我的问题</p>
<p>目前的实现如下</p>
<pre><code>@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;
</code></pre>
<p>现在对应的.m是</p>
<pre><code>@implementation HttpClient_iPhone
@synthesize recievedData, statusCode;
@synthesize delegate,previousRequest;
- (id)init {
if (self = ) {
;
delegate = nil;
networkChecked = NO;
}
return self;
}
- (void)dealloc {
if (connection) {
;
}
;
connection = nil;
if (recievedData) {
;
recievedData = nil;
}
;
}
- (void)reset {
if (recievedData) {
;
recievedData = nil;
}
recievedData = [ init];
if (connection) {
;
;
connection = nil;
}
statusCode = 0;
networkChecked = NO;
}
- (void)requestGET:(NSString*)url {
//Reseting the http client
;
//Checking the internet connection
if ( == NO){
if (self.delegate != nil && ){
;
}
return;
}
NSMutableURLRequest *request = ;
;
if(![ isEqualToString:@"pda_errorlogs"])
self.previousRequest = ;
;
;
connection = [ initWithRequest:request delegate:self startImmediately:YES];
}
- (void)requestPOST:(NSString*)url body:(NSString*)body type:(NSString*)type {
;
if ( == NO){
if (self.delegate != nil && ){
;
}
return;
}
NSMutableURLRequest *request = ;
;
if (type != nil && !)
;
if (body) {
];
}
;
if(![ isEqualToString:@"pda_errorlogs"])
self.previousRequest = ;
;
;
;
connection = [ initWithRequest:request delegate:self];
}
- (void)requestGET:(NSString*)url username:(NSString*)username password:(NSString*)password {
;
if ( == NO){
if (self.delegate != nil && ){
;
}
return;
}
NSMutableURLRequest *request = ;
if(![ isEqualToString:@"pda_errorlogs"])
self.previousRequest = ;
;
;
;
;
connection = [ initWithRequest:request delegate:self startImmediately:YES];
}
- (void)requestPOST:(NSString*)url username:(NSString*)username password:(NSString*)password body:(NSString*)body type:(NSString*)type {
;
if ( == NO){
if (self.delegate != nil && ){
;
}
return;
}
NSMutableURLRequest *request = ;
;
if (type != nil && !)
;
if (body) {
];
}
;
if(![ isEqualToString:@"pda_errorlogs"])
self.previousRequest = ;
;
;
;
;
;
connection = [ initWithRequest:request delegate:self];
}
- (void)requestPOST:(NSString*)url username:(NSString*)username password:(NSString*)password bodydata:(NSData*)body contenttype:(NSString*)type {
;
}
- (void)cancelTransaction {
;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
statusCode = [(NSHTTPURLResponse*)response statusCode];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if (self.delegate != nil && ){
;
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError*) error {
if (self.delegate != nil && ){
;
}
}
</code></pre>
<p>//Crashlytics 上报告的崩溃日志图像</p>
<p>第 1 部分,共 2 部分
<img src="/image/0za4V.png" alt="enter image description here"/> </p>
<p>第 2 部分,共 2 部分
<img src="/image/ASrE5.png" alt="enter image description here"/> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>两条评论:</p>
<p>1) 将 header 中的 recievedData 属性类型更改为“保留”(即使它没有在您的代码中用作属性)</p>
<p>2) 在你释放一个NSURLConnection之前,你应该发送它<code>cancel</code>,即<code></code>。你在 dealloc 和 reset 中释放它。另外,首先要取消它 - 不要事先发布可能被它使用的数据。</p>
<p>编辑:根据您当前的代码,您的 <code>reset</code> 方法在修改可变数据之前仍然会取消。我修改了您的代码,如下所示,以反射(reflect)您的 <code>dealloc</code> 方法(您不需要 nil 语句):</p>
<pre><code>- (void)reset {
if (connection) {
;
;
connection = nil;
}
if (recievedData) {
;
recievedData = nil;
}
recievedData = [ init];
statusCode = 0;
networkChecked = NO;
}
- (void)dealloc {
if (connection) {
;
;
}
if (recievedData) {
;
}
;
}
</code></pre>
<p>试试看。</p></p>
<p style="font-size: 20px;">关于iphone - 为什么我在网络通信期间会发生随机崩溃 - (CFRelease 中的崩溃)?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/16602738/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/16602738/
</a>
</p>
页:
[1]