Passing a vector object or pointer between DLL or EXE

Source: Internet
Author: User

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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.