开发者

NSURLRequest autorelease: crash

I am having problem releasing NSMutableURLRequest object. Application crashes during the deallocation of request object.

The object is created via [[NSMutableURLRequest alloc] in开发者_运维百科itWithURL:my_http_url];

As mainstream flow of control, I try to release the connection object in response to connectionDidFinishLoading handler being invoked.

At first I tried autoreleasing NSMutableURLRequest right inside connectionDidFinishLoading handler. This caused crashing, so I assumed it might be because connection class pushes autorelease pool internally before calling connectionDidFinishLoading and still expects connection object to be valid when the handler returns, so it is impossible then to release or autorelease the connection object inside connectionDidFinishLoading.

If I do not release NSMutableURLRequest at all, according to Instruments it leaks with reference count 1.

Therefore I decided to do a delayed release via firing an NSRunLoop event that autoreleases NSMutableURLRequest passed to it. This still causes a crash.

retainCount before calling autorelease is 1.

Crash stack is:

#

0   0x9448aedb in objc_msgSend
#1  0x04a47ce0 in ??
#2  0x02e51501 in HTTPMessage::~HTTPMessage
#3  0x02945621 in _CFRelease
#4  0x02e516a9 in HTTPRequest::~HTTPRequest
#5  0x02e50967 in URLRequest::~URLRequest
#6  0x02945621 in _CFRelease
#7  0x0032fb70 in -[NSURLRequestInternal dealloc]
#8  0x0032fb1a in -[NSURLRequest dealloc]
#9  0x002f27a5 in NSPopAutoreleasePool
#10 0x003b5dd0 in __NSFirePerformTimer
#11 0x0299e8a2 in __CFRunLoopDoObservers
#12 0x0296a39e in CFRunLoopRunSpecific
#13 0x0296a048 in CFRunLoopRunInMode
#14 0x031d289d in GSEventRunModal
#15 0x031d2962 in GSEventRun
#16 0x0058ede1 in UIApplicationMain
#17 0x00002b9c in main at main.m:14

Thanks for advise.


After using NSDebugEnabled/NSZombieEnabled/NSAutoreleaseFreedObjectCheckEnabled I identified the problem:

result of

http_url = [NSURL URLWithString:...]

should have been either retained or not autoreleased on its own.


Try to instantiate it as an autoreleased object using:

[NSMutableURLRequest requestWithURL:my_http_url];


I believe Instruments are not useful for any tracing in this case since the application crashes and then is auto-restarted (it's iPad/iPhone app), so all Intruments history is gone.

I do not see much point instantiating NSMutableURLRequest as autoreleased object since it needs to be retained for the duration of async call spanning multiple idle loop cycles. NSURLConnection may retain it internally or not, but keeping an extra reference for safety should not hurt.

The initialization code boils down to essentially the following:

rq->url_encoded = [url_encode(rq->url) retain];
rq->http_url = [NSURL URLWithString:rq->url_encoded];
rq->http_request = [[NSMutableURLRequest alloc] initWithURL:rq->http_url];
[rq->http_request setHTTPMethod:@"GET"];

NSArray* availableCookies = [rq->service.CookieJar cookiesForURL:rq->http_url];
NSDictionary* headers = [NSHTTPCookie requestHeaderFieldsWithCookies:availableCookies];
[rq->http_request setAllHTTPHeaderFields:headers];
rq->http_connection = [NSURLConnection alloc];

[rq->http_connection initWithRequest:rq->http_request delegate:rq startImmediately:YES];

The release procedure is that connectionDidFinishLoading calls [rq->http_connection cancel] and [rq autorelease], the latter eventually leading to:

// [http_request autorelease];
// delayedAutoRelease(http_request);
[http_connection autorelease];
[http_url autorelease];
[url release];
[url_encoded release];
[response release];

Note that [response release] just pairs previous [response retain] executed inside didReceiveResponse.

If I leave first two lines commented out, NSURLRequest leaks with reference count 1 per Instruments, and this is the only leak observed.
Whenever I attempt to autorelease http_request whichever way as described above, a crash occurs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜