开发者

Does Objective-C 2.0 garbage collection collect C structures?

开发者 https://www.devze.com 2022-12-27 16:00 出处:网络
What exactly does the Objective-C garbage collector collect? For example, if I\'m writing a program in Objective-C 2.0, and I use some plain C structs, does my code need t开发者_如何学Pythono worry ab

What exactly does the Objective-C garbage collector collect? For example, if I'm writing a program in Objective-C 2.0, and I use some plain C structs, does my code need t开发者_如何学Pythono worry about manually freeing that memory?


For heap memory, you're on your own by default. Unless you allocate heap memory with NSAllocateCollectable or NSReallocateCollectable, or explicitly hand a CoreFoundation object over to the garbage collector via an API like NSMakeCollectable, the GC has no idea about your memory, and won't manage it for you. Conversely, when GC is enabled, Objective-C objects created with +alloc/-init are managed by GC unless you explicitly tell it otherwise with a complementary API call.

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/GarbageCollection/


If you use malloc() to allocate a structure on the heap, then I really doubt it (the man page doesn't mention anything about it, but it's possible that Apple rewrote malloc for the ObjC2 runtime). If you use NSAllocateCollectable(), then yes.

If the structure was created on the stack, then there's no need for collection, since it will be destroyed as soon as the frame exits.

0

精彩评论

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

关注公众号