Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memory leaks with garbage collection #45

Closed
mackuba opened this issue Jun 10, 2010 · 7 comments
Closed

memory leaks with garbage collection #45

mackuba opened this issue Jun 10, 2010 · 7 comments

Comments

@mackuba
Copy link

mackuba commented Jun 10, 2010

If I run 'build and analyze' on a garbage-collection-enabled project using ASIHTTPRequest, I get 10 warnings with the message "Potential leak (when using garbage collection) of an object..." (in ASIFormDataRequest.m and ASIHTTPRequest.m). Could you look into this? (It seems it's mostly related to objects allocated using CoreFoundation functions and deallocated using autorelease.)

@pokeb
Copy link
Owner

pokeb commented Jun 18, 2010

I don't have any experience with GC - does this mean these will actually leak, or do you think Clang is just confused?

I guess in some cases I could switch to using CFRelease(), but I prefer to use autorelease. Lots of methods return early, so it's easier to ensure I'm not forgetting to release something.

@mackuba
Copy link
Author

mackuba commented Jun 19, 2010

I don't have any experience with CoreFoundation, but it looks like this actually does cause memory leaks. (I'm thinking now that those reports by my app's users that it leaks like hell might have something to do with this... though I haven't investigated this at all so I'm just guessing here :)

There's a page in Apple's documentation that explains in detail how to use CoreFoundation with garbage collection: http://developer.apple.com/mac/library/documentation/cocoa/conceptual/GarbageCollection/Articles/gcCoreFoundation.html

It seems the solution is to call CFMakeCollectable or NSMakeCollectable on the CoreFoundation objects - if I understand correctly, in GC mode this lowers the reference count for CF objects to zero (making the object collectable when it goes out of scope, not releasing it immediately), and in non-GC mode it does nothing, so it's safe to use it even without GC. If I wrap a call to e.g. CFReadStreamCopyProperty in CFMakeCollectable, Clang stops complaining.

The leak is apparently caused by the fact that CF* creation methods increase the reference count even in GC mode, but Cocoa's [release] and [autorelease] are no-ops in GC mode so they don't lower it and it stays above 0.

@mackuba
Copy link
Author

mackuba commented Jun 19, 2010

I've looked at the memory allocation in my app and I'd say that probably the problem is somewhere within the UI (mainly with images), not with the http requests... but it would be nice to fix this anyway.

@pokeb
Copy link
Owner

pokeb commented Jun 20, 2010

Many thanks for the info! :)

I've wrapped all the calls the static analyser complains about in NSMakeCollectable, and added CFMakeCollectable calls in markAsFinished to free up the CF objects a request hangs on to (as dealloc won't be called). I've done a quick run with the leaks instrument, and it looks like the leaks should be gone, but please let me know if you're still seeing them!

Thanks again

Ben

@mackuba
Copy link
Author

mackuba commented Jun 20, 2010

Looks good, no more warnings, thanks :)

@MrZoidberg
Copy link

Guys, does this issue fixes problems for iOS or it is Mac only?

@mackuba
Copy link
Author

mackuba commented Jun 21, 2010

Definitely Mac only, this is only relevant to garbage collection and there's no garbage collection on iOS for now.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants