Freeing memory allocated in a different DLL

后端 未结 4 1151
梦如初夏
梦如初夏 2020-12-06 11:37

I have an EXE file using a DLL file which is using another DLL file. This situation has arisen:

In DLL file 1:

class abc
{
    static bool FindSubFol         


        
4条回答
  •  情话喂你
    2020-12-06 11:55

    As other says, the problem can be solved by making sure that the CRT is shared between the two modules. But there are common scenarios where this contract is hard to enforce.

    The reason is that making sure to link against a shared CRT will not work if the EXE and DLL do not link against the same CRT version (as in 6.0, 7.0, 8.0). For example if you take a DLL that has been built in VC6.0 and try to use it with an EXE build in VS2010 you will get the same issue as before. The two versions of CRT will be loaded in your process and will each use their own heap for allocation, regardless if your EXE and DLL use 'shared' CRT, they will not be the same.

    A better practice for an API would be to make sure that objects allocated in one side are also destroyed on the same side. I know this sounds ugly but it is the only way to ensure that a DLL remains binary compatible.

提交回复
热议问题