Re: Static variables reset inside CFPlugin code
Re: Static variables reset inside CFPlugin code
- Subject: Re: Static variables reset inside CFPlugin code
- From: Grandinetti Philip <email@hidden>
- Date: Sun, 01 Jan 2012 17:21:52 -0500
Thanks Jens. With more experimenting and reading around I've now got a better picture, and your reply confirmed that picture. I think my problem was even trickier, because I defined my static variable (actually a pointer to a array) in a static library linked to the main app, and the plugin is loaded by the static library, not the main app. Whew!
Your suggestions make sense, although...
option (a) doesn't work for me, since the static variables are being used before the plugin is loaded.
option (b) I tried changing the static variable into a global, and putting the global outside of main in the app, but this didn't work for me. Not sure what I did wrong here.
option (c) seems like the solution I need, but I couldn't find how to do this in Xcode.
option (d) I'm not sure I understand. If I link the host app against the plugin doesn't that defeat the purpose of making it a plugin?
Ultimately, a solution I put together was to add an interface function to the plugin that passed the pointer from the host app over to the plugin, and used that to set the plugin's copy of the static variable (pointer in this case).
If you know where I can read more about implementing option (c) or other suggestions I would be grateful to hear about it.
Thanks, and happy new year!
Philip
On Jan 1, 2012, at 3:42 PM, Jens Alfke wrote:
>
> On Dec 29, 2011, at 12:11 PM, Grandinetti Philip wrote:
>
>> I'm loading and creating a CFplugin using Core Foundation and running into a strange behavior where all the static variables defined throughout my code get reset to their initialization values when running code inside plugin.
>
> If you have two binaries — an application and a plugin — then each of those has independent global/static variables, even if those variables are declared in the same source files. The scope of globals is basically defined by the linker, and you've linked two independent binaries that have no magic connection to each other.
>
> If you want the two to share a variable, you'll need to:
> (a) define it in the plugin code, not the app
> (b) declare it as global not static, so it's visible outside that compilation unit
> (c) tell the linker to export that global from the plugin, e.g. by using an export file
> (d) make the app link against the plugin so it can access the variable
>
> —Jens
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden