Memory Leak with NSURLRequest?
Memory Leak with NSURLRequest?
- Subject: Memory Leak with NSURLRequest?
- From: Jason McInnes <email@hidden>
- Date: Sat, 15 Nov 2003 14:51:57 -0800 (PST)
I'm writing a batch application that makes repeated
calls to the USPS website using the following method
calls:
***snip***
request = [NSMutableURLRequest requestWithURL:aURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
***snip***
receivedData = [NSURLConnection
sendSynchronousRequest:request
returningResponse:&aResponse
error:&aError];
I was examining memory allocation in ObjectAlloc and
discovered (to my dismay) that I had URLRequests that
had not been deallocated. The requests corresponded to
the requests that I had posted.
Each had:
AllocationEvent
ObjectRetainedEvent
ObjectReleasedEvent
ObjectRetainedEvent
ObjectReleasedEvent
Other interesting observations:
- I tested with just 5 requests. According to
ObjectAlloc, there were 15 NSURLRequests total.
- 10 of the NSURL Requests were freed.
- 5 of the freed NSURLRequests had:
AllocationEvent
ObjectRetainedEvent
ObjectReleasedEvent
ObjectReleasedEvent
FreeEvent
- 5 of the freed NSURLRequests had:
AllocationEvent
ObjectReleasedEvent
FreeEvent
- the event inspector information for the remaining
allocated Requests:
Event: (#367979) AllocationEvent
Time Index: 90628504.409750
Category: NSURLRequest
Pointer: 0x2e949d0 Size: 14
Thread: 0x6d0b Extra Data: 0x0
Backtrace:
object_getIndexedIvars
NSAllocateObject
-[NSMutableURLRequest copyWithZone:]
-[NSURLConnection initWithRequest:delegate:]
-[NSSynchronousURLConnectionDelegate run:]
forkThreadForFunction
start
Event: (#368572) ObjectRetainedEvent
Time Index: 90628504.413922
Category: NSURLRequest
Pointer: 0x2e949d0 Size: 14
Thread: 0x3c03 Extra Data: 0x2
Backtrace:
NSIncrementExtraRefCount
-[NSObject retain]
-[NSURLProtocol
initWithRequest:cachedResponse:client:]
-[NSHTTPURLProtocol
initWithRequest:cachedResponse:client:]
-[NSSimpleHTTPURLProtocol
initWithRequest:cachedResponse:client:]
-[NSURLConnection(NSURLConnectionInternal)
_beginLoad]
_loadBegin
__CFRunLoopDoSources0
__CFRunLoopRun
CFRunLoopRunSpecific
-[NSRunLoop runMode:beforeDate:]
-[NSRunLoop run]
+[NSURLConnection(NSURLConnectionInternal)
_resourceLoadLoop:]
forkThreadForFunction
start
Event: (#397120) ObjectReleasedEvent
Time Index: 90628505.315979
Category: NSURLRequest
Pointer: 0x2e949d0 Size: 14
Thread: 0x3c03 Extra Data: 0x2
Backtrace:
NSDecrementExtraRefCountWasZero
-[NSObject release]
-[NSURLProtocolInternal dealloc]
-[NSURLProtocol dealloc]
-[NSHTTPURLProtocol dealloc]
-[NSSimpleHTTPURLProtocol dealloc]
NSPopAutoreleasePool
-[NSRunLoop runMode:beforeDate:]
-[NSRunLoop run]
+[NSURLConnection(NSURLConnectionInternal)
_resourceLoadLoop:]
forkThreadForFunction
start
Event: (#397216) ObjectRetainedEvent
Time Index: 90628505.317249
Category: NSURLRequest
Pointer: 0x2e949d0 Size: 14
Thread: 0x6d0b Extra Data: 0x2
Backtrace:
NSIncrementExtraRefCount
-[NSObject retain]
-[NSURLCacheNode initWithCachedResponse:request:key:]
-[NSURLCache storeCachedResponse:forRequest:]
-[NSURLConnection(NSURLConnectionInternal)
_sendCallbacks]
-[NSArray makeObjectsPerformSelector:withObject:]
_sendCallbacks
__CFRunLoopDoSources0
__CFRunLoopRun
CFRunLoopRunSpecific
CFRunLoopRun
-[NSSynchronousURLConnectionDelegate run:]
forkThreadForFunction
start
Event: (#417824) ObjectReleasedEvent
Time Index: 90628505.990522
Category: NSURLRequest
Pointer: 0x2e949d0 Size: 14
Thread: 0x603 Extra Data: 0x2
Backtrace:
NSDecrementExtraRefCountWasZero
-[NSObject release]
-[NSURLConnectionInternal dealloc]
-[NSURLConnection dealloc]
-[NSSynchronousURLConnectionDelegate dealloc]
NSPopAutoreleasePool
+[PFConversionDBManager preprocessPFAddressesCount:]
-[PFirstConversionWindowController
dbPreprocessPFClinicAddresses:]
-[NSApplication sendAction:to:from:]
-[NSControl sendAction:to:]
-[NSCell _sendActionFrom:]
-[NSCell trackMouse:inRect:ofView:untilMouseUp:]
-[NSButtonCell
trackMouse:inRect:ofView:untilMouseUp:]
-[NSControl mouseDown:]
-[NSWindow sendEvent:]
-[WebNSWindow sendEvent:]
-[NSApplication sendEvent:]
-[NSApplication run]
NSApplicationMain
main
_start
start
I'm sure it has something to do with threads (YIKES!).
I tried manually releasing the originally allocated
NSURLRequest, but my app crashed. Then I tried
autoreleasing it with the same result.
The method that makes use of the NSURLRequest is in a
while loop where each iteration through the loop has
its own NSAutoreleasePool. The last Release event of
the leaking NSURLRequests is due to the release of
this NSAutoreleasePool.
Is there something I am supposed to be doing here
because of the threads that are spawned by
sendSynchronousRequest? On the surface it looks like I
am within my rights to expect the NSURLRequest to be
properly autoreleased by the class method that gives
it to me (although I also noticed that in the event
list there is no autoreleased event...).
What am I missing?
jason
=====
Jason McInnes
2 Degrees
Cell: 206.849.3680
Email: email@hidden
__________________________________
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
http://antispam.yahoo.com/whatsnewfree
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.