Pregunta

I have a exe built by vs2012, which can successfully run with linking to dll built by vs2012, while the following error appeared when linking to the same dll built by vc6. (Expression:_CrtIsValidHeapPointer(pUserData))

enter image description here

the only difference between the dll projects of vc6 and vs2012 is that the vc6 one fail to #include "SDKDDKVer.h"

Here is the code for export function of the dll

extern "C" _declspec(dllexport) RobotAI_Interface* Export()
{
   return (RobotAI_Interface*)new RobotAI();  
}
extern "C" _declspec(dllexport) void FreeRobotAIPointer(RobotAI_Interface* p)
{
   delete p;
}

The program breaks at this specific point when debuging (a system code):

extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(const void * pUserData)
{
    if (!pUserData)
        return FALSE;

    if (!_CrtIsValidPointer(pHdr(pUserData), sizeof(_CrtMemBlockHeader), FALSE))
        return FALSE;

    return HeapValidate( _crtheap, 0, pHdr(pUserData) );
->}

The arrow is where it breaks.

The following is my code where it breaks:

void CArmorClientDlg::AddToList(CString file_path_name)
{

pAIManager->Add(file_path_name);
string dll_name=pAIManager->GetAI_RobotName(pAIManager->GetAINum()-1);
CListBox* pList=static_cast<CListBox*>(GetDlgItem(IDC_LIST_AI));
pList->AddString((CString)dll_name.c_str());
->}

The arrow is where it breaks. All the operations related to dll are done in "Add(file_path_name)"

I know little about the dll things... How to solve the problem? Thank you!

TIP: the reason that I have to support vc6 dll is that the project is an AI competition, dll is the AI program that built by different contastents. The majority of the students in my school may only use vc6.

¿Fue útil?

Solución

To be clear, you are mixing an EXE built with VS2012 with a DLL built with VC6? If so you are mixing C runtime library versions. While this is possible, you have to be careful to not mix heap allocations. For example, if the DLL allocates memory and returns a pointer, and the EXE tries to free the pointer, that could cause the error you see.

Either compile both the DLL and EXE with the same compiler, or write a function in the DLL that frees the pointer and use that function to pass the allocated pointer back to the DLL to be freed by the C runtime that allocated it.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top