1. 程式人生 > >DEBUG與RELEASE版本不能交叉呼叫

DEBUG與RELEASE版本不能交叉呼叫

讓我們先看一段引文,再來進行心靈的探索:

If you have an EXE and a DLL.When your exe APP was built Debug Mode, your Dll must be Debug mode.When your exe APP was built Release Mode, your Dll must be Release mode.If exe is Release Mode and Dll is Debug Mode, Error.If exe is Debug Mode and Dll is Release Mode, Error.

-------------------------------- Why? --------------------------------

Looks like you have a problem with memory allocation.When an application is built in debug mode it does not allocate memory from the same heap as it does in release mode.(have a look to the definition of new and delete operator, and also to malloc (in afxmem.h ? (not sure just grep it))Thus, when you get some memory from your debug app and give this memory to your release dll for processing and FREEING,you get an error since the realase dll tries to free it from the wrong heap. So always use your app and dll in the same mode.At least, do not try to free memory allocation in one mode in the other mode.

現在,我們來概括一個結論:之所以,Dll與Exe之間進行Release與Debug的交叉呼叫會出現錯誤,一個非常重要的原因,可能是由於Debug下與Release下,模組的匯入地址是不一樣的,即使你取消所有的優化,也許也並不能改變這樣的結局。 從而對於一個在Exe(假如是Release版本)中生成的物件(比如一個靜態的全域性陣列),與一個在Dll中生成的物件可能會位於完全不同的記憶體區域,從而對那些藉助於對靜態指標的值進行判斷的類(比如CString)跨模組呼叫的時候,一旦出現判斷,將會出現嚴重的邏輯錯誤,從而使一塊本不該被釋放的記憶體被釋放,從而引發執行時的錯誤。解決交叉呼叫的最簡單,最合理的方法就是: debug呼叫debug版,release呼叫release版,這也許就是模組化中的一個瑕疵吧。

但是,有時候會發現:

release的dll在debug和release的主調程式中都能使用,而且也沒有斷言,就是不能除錯

釋出的時候所有的都要用release版本

求俠哥俠女科普: