Can I unload a bundle in a Cocoa application?

According to Guides and sample codes ...

1. Cocoa loading Programming Topics > Multi-Bundle Application

Additionally, Cocoa applications cannot unload bundles. If you want to be able to load and unload large separately built application components, you can build your application as a number of smaller executable components, and the main application can fork child processes. You can enable communication between the processes with the Cocoa distributed objects architecture. For more information, see Distributed Objects.


2. Cocoa loading Programming Topics > Document Revision History

Removed limitations item claiming that Cocoa bundles cannot be unloaded.


Which is right?


I want to unload the dynamic bundle in a cocoa application. The bundle uses the NSWindowController to pop up and exit the Window.

However, when I unload the bundle, an EXC_BAD_ACCESS (objc_loadweakretained) error occurs.


If not, please give me another way to do the same thing.


Thank you.

Accepted Reply

There are limited circumstances under which a bundle can be unloaded but unloading a bunch containing general-purpose Objective-C code is not safe.

My general advice on this front is to not worry about unloading your bundles. A quiescent bundle should not have a big impact on your app. It’s only significant resource is its code, and that is paged in by the VM system and thus the memory can be recovered by the VM system.

Other than recovering resources, is there some other reason you need to unload this bundle?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

There are limited circumstances under which a bundle can be unloaded but unloading a bunch containing general-purpose Objective-C code is not safe.

My general advice on this front is to not worry about unloading your bundles. A quiescent bundle should not have a big impact on your app. It’s only significant resource is its code, and that is paged in by the VM system and thus the memory can be recovered by the VM system.

Other than recovering resources, is there some other reason you need to unload this bundle?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Quinn “The Eskimo!” states...
"There are limited circumstances under which a bundle can be unloaded"

Where does Apple document these 'limited circumstances'?

Where does Apple document these 'limited circumstances'?

I’m not aware of any recent documentation on this front. Historically, it was safe to unload a bundle that a) only contained C code, and b) was built with a magic flag that prevents the compiler from laying out CFString constants as statics. Honestly, I’ve no idea whether that still works.

Realistically, current systems have no reliable way to unload bundles and you should structure your code accordingly.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"

Historically, it was safe to unload a bundle that a) only contained C
code, and b) was built with a magic flag that prevents the compiler
from laying out CFString constants as statics.

Oh, and c) didn’t go out of its way to break this. A classic example of such breakage would be an atexit callback. Registering one of those from a bundle makes unloading the bundle unsafe.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"