Desperately looking for a work-around to load and unload D shared libraries from C on OSX

ponce via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 18 02:43:41 PDT 2015


On Thursday, 17 September 2015 at 21:13:46 UTC, bitwise wrote:
> On Thursday, 17 September 2015 at 20:47:49 UTC, Jacob Carlborg 
> wrote:
>> On 2015-09-17 21:42, bitwise wrote:
>>
>>> Ok, but this kinda defeats the purpose, as the op wants to 
>>> unload the
>>> library ;)
>>
>> He said he doesn't want dlopen to crash, if it doesn't unload 
>> it fixes the problem ;)
>
> True. Looking at his bug report now, it seems his dylib is a 
> VST plugin. Had he just said that to begin with, this 
> conversation would have been a lot easier -_-
>
> The op shouldn't have to actually modify druntime in this case. 
> He shouldn't have to replace 
> "_dyld_register_func_for_add_image".
>
> He can simply create a second empty callback in VSTPluginMain 
> which will pin his library:
>
> static bool didInitRuntime = false;
>
> const char* ignoreImageLoad(
>     enum dyld_image_states state,
>     uint32_t infoCount,
>     const struct dyld_image_info info[])
> {
>     // ignore.
> }
>
> extern(C) void* VSTPluginMain(void* hostCallback)
> {
>     import core.runtime;
>
>     if(!didInitRuntime)
>     {
>         Runtime.initialize();
>         dyld_register_image_state_change_handler(
>             dyld_image_state_initialized,
>             &ignoreImageLoad);
>         didInitRuntime = true;
>     }
>
>     import std.stdio;
>     import core.stdc.stdio;
>     printf("Hello !\n");
>
>     // Runtime.terminate();
>     return null;
> }
>
>     Bit

Much success.
Not only did this work, it worked (around) right away!

Final patch here:
https://github.com/p0nce/dplug/commit/7dc6385ebb8147cc53cfe69bfd54e41f5341e158

Thanks to you all and I will for sure include your name in the 
release notes.
You removed a huge roadblock on my path to being relevant.




More information about the Digitalmars-d mailing list