开发者

What are the pitfalls of having a two dlls share a static library?

Say you have two dlls

Dll A and Dll B

They both statically link to a static library (ie a .lib file). We'll call that library L.

I know that L fully links into A and B separately effectively creating LA and LB. However, what happens when object O from LA gets pass开发者_JAVA技巧ed from A to B? I assume in A LA code gets executed while in B LB code gets executed because thats how linking happened. What bad things can happen if you create O in A, unload A, then use O in B?


A static library is just a collection of object files. When you link with a static library, everything happens as if the code of the static library had been included into yours.

So if there are global variables in the static library, each DLL will get its own copy, which may or may not be what you want.


However, what happens when object O from LA gets passed from A to B?

It really depends on what object O is and what the library L does with it. If O is an object that is entirely dependent on state stored within that object, then everything will probably be fine. However, if O is not, if O depends on global state or the state of some other object or something, you could have problems.

The best way to avoid problems is to have proper layers of insulation between two DLLs. That is, you shouldn't be passing O between the two at all, since they're not talking about the same O. In general, if L is big enough to require you to pass its objects between two users, then it is big enough that you probably should consider dynamically linking to it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜