I have developed a MFC dll containing a function having this prototype:
//DLL code
long __declspec(dllexport) GetData(CString csIdentifier, CStringArray& arrOfData)
{
//based on the identifier I must add some strings inside the string array
arrOfData.Add("...");
arrOfData.Add("...");
/*.....................*/
return 1;
}
The problem that I have is after the function gets called (from the executable). The destructor of the arrData will be called and will try to release the memory but it will not succeed because the allocation of the arrOfData was done on another heap(inside the dll). Although I have compiled both applications (Exe and Dll) using the same enviroment settings, I still have the issue in both debug and both release mode. How can I solve the issue?
//Executable code
{
CStringArray arrData;
GetData("Identifier",arrData);
//data is accesible
}
heap violation occurs just before existing the code block
In order to share MFC objects like CStringArray across an exe/dll boundary, you'll need to make the DLL be an MFC Extension DLL. See: https://msdn.microsoft.com/en-us/library/h5f7ck28(v=vs.140).aspx
From the section on Memory Management:
It's also possible that your DLL function needs AFX_MANAGE_STATE(AfxGetStaticModuleState()) at the top to property set up the environment when called externally.