質問

globals are bad I know, but even statics local to a function appear to be bad.

I noticed that on linux, globals and statics inside a shared object (dynamic library) are initialised the first time you use dlopen. But when you dlclose, and then dlopen again, it doesn't initialise globals or statics the second time.

I used RTLD_NOLOAD with dlopen to test if dlclose actually removed the libary, and it said it has, so this isn't a reference count issue.

This isn't the same as windows: http://msdn.microsoft.com/en-us/library/988ye33t.aspx Which I've tested and does appear to re-initialise statics.

Is this true, or am I just messing something else up?

If this is true, wouldn't this make any kind of static variable dangerous/useless?

役に立ちましたか?

解決

But when you dlclose, and then dlopen again, it doesn't initialise globals or statics the second time.

This is most likely happening because the library was in fact not unloaded on dlclose.

From man dlclose

The function dlclose() decrements the reference count on the dynamic library
handle handle. If the reference count drops to zero and no other loaded
libraries use symbols in it, then the dynamic library is unloaded.

It is most likely that you didn't satisfy "no other loaded libraries use symbols in it" part.

You can run the binary with LD_DEBUG=symbols,bindings and watch for messages like this:

    binding file XXX to libYYY.so: normal symbol `ZZZ'

If you bind any symbols in libYYY.so to a file that is not unloaded, then libYYY.so can't be unloaded on dlclose.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top