Mix / MD and / MT in a single dll

I have a dll project in Visual Studio 2012 that compiled with / MT (static multi-threaded runtime library). It also bundles a third-party static lib, also compiled with / MT (library A), without any problems.

The problem is related to another static lib (library B), which, unfortunately, is compiled with / MD. In my dll, I need to link both and there is no alternative to either of them (and I can not recompile them with the other option). I managed to successfully bind everything together, but now I have problems with allocating and deleting memory - sometimes it fails to delete the selected object, sometimes other strange errors occur. I believe this is caused by the mixed memory management functions used by different parts of my DLL - when new is called, the object is created in library B, but when delete is called, it tries to free memory with a different set of functions, but I may be wrong.

So my question is: is this really caused by mixed memory management functions? And if so, is there a way to combine this work?

The only solution I think of is to wrap the B library in another dll compiled with / MD, and then use it from the original dll to allow for the use of various memory management functions. I am not sure if this will help, and I would like to avoid it.

+4
source share
1 answer

You seem to understand the cause of the problems you see and described on MSDN , as shown below.

MSDN screenshot

If it is really impossible to link all linked libraries to the same CRT version, your only possible choice is to avoid passing CRT objects across the boundaries of these modules. Regardless of whether you can do this with your script, it depends on your application. The key point in this article is the following sentence:

If you create your DLL so that it passes CRT objects along the border or allocates memory and expects it to be freed outside the DLL, you restrict DLL users to the same copy of the CRT library as the DLL. A DLL and its users use the same copy of the CRT library only if both are associated with the same version of the CRT DLL.

I know that you stated that it is not possible to obtain or create compatible modules to communicate with your application, but I would recommend that you review it comprehensively and not to avoid mixing different CRT libraries at all costs.

+4
source

Source: https://habr.com/ru/post/1484076/


All Articles