Today, when I completed a functional module, I encountered a very difficult problem, probably like this:
A dll in the main module (exe) has a function FUNA (). This function needs to query the database and obtain the record set for processing. The database operations encapsulate and export a DLL separately, because the record set is longer (I don't know how many records there are), we use a vector object to transmit data. The general process is as follows:
Boolfuna_exe ()
{
Vector <t> vecret;
String strsql = "select * from .....";
Func_dll (strsql, vecret); // database operation DLL interface function, add the query result to vecret
// Perform operations on vecret
Return true;
}
Result: vecret in the function can obtain the query result, but the function crashes when it exits, as shown in:
Stack information:
From the stack information, it is a vector <> object structure error !!!
I searched for the cause repeatedly and did not find the problem. I finally searched the internet and found the cause. I recorded it here to facilitate future search:
The reason is: the vector <> will automatically analyze the structure, but because the content in the vector <> is the memory allocated in the DLL, the memory allocation method in DLL and exe is different (it seems that this is the case, but it is not verified). In the vector <> object analysis, the corresponding memory cannot be found, so an error occurs! For more information, see the network.
Solution: apply for the query results in the database DLL and release the memory, but pass the result pointer back. Who applies for release !!!
Boolfuna_exe ()
{
Vector <t> * pvecret = NULL;
String strsql = "select * from .....";
Func_dll (strsql, & pvecret); // database operation DLL interface function, add the query result to vecret
// Perform operations on vecret
Return true;
}
Note: Release the applied memory in the database DLL module.
Suggestion:
1. It is best to use some standard types in the DLL Interface
2. Do not use container objects or container references in the DLL interface.