Passing STL objects between multiple DLL (exe) through pointers or references

Source: Internet
Author: User
Operation failures may occur when multiple DLL (exe) objects are passed through pointers or references to STL objects. The complete solution is provided on msdn. Take it seriously when developing applications.

Symptom:

When operating on STL objects in another DLL or EXE through pointers or references in different DLL or EXE, serious program errors may occur, including data disorder or loss.

Cause:

Most classes in the Standard C ++ library directly or indirectly use static data members. Since these classes create instances through templates, each executable program (typically with a DLL or EXE extension) contains a copy of the static data member of the class. When methods in the STL class require the operation of static data members, the static data operated by this class is the data in the execution program where the method code is located. Because static member data cannot be synchronized in the executable program, the operations mentioned above may cause read failure or data confusion and loss.

Solution:

1. Output The read method (function) in the executable program that creates the STL object ). These functions encapsulate the features required by STL objects. In this way, STL objects can only be directly read in a single executable program. For example, suppose myprogram. exx needs to get mylibrary. next element in deque <myclass> In DLL, mylibrary. DLL: "myclass * dequenextitem (/*... */);". Myprogram. EXE can execute this method to obtain the next element of the queue.
2. Output a template instance from an executable program and introduce it to another executable program. For example, if mylibrary. dll returns the vector <myclass> pointer to a function in myprogram. EXE, The myclass class and vector <myclass> must be output in mylibrary. dll. Introduce them in myprogram. EXE. You can get a copy of the static data member in mylibrary. dll.

Example program:
//---------------------------------------------------------
// Avexe. cpp
// Compile options needed:/GX
# Pragma warning (Disable: 4786)
# Include <map>
# Include <string>
# Include <stdio. h>
_ Declspec (dllimport) STD: Map <int, STD: String> * givemeamap (int n );
_ Declspec (dllimport) void showmethemap (STD: Map <int, STD: String> * AMAP );
_ Declspec (dllexport) const char * mapitemx (STD: Map <int, STD: String> * m, int X );
Int main (){
// Create the map in the DLL
Int x = 6;
STD: Map <int, STD: String> * P = givemeamap (X );

// Display the contents of the map from the DLL
Printf ("showing contents from the DLL/N ");
Showmethemap (P );

// Display the contents of the map from the EXE
// Using the accessor function from the DLL so we
// Aren't directly accessing the map
Printf ("showing contents from the EXE using accessor/N ");
Int I = X;
While (I --){
Printf ("% d = % s/n", I, mapitemx (P, I ));
}

// Access violation when accessing the map that
// Was created in the DLL from the EXE
Printf ("showing contents from the EXE directly/N ");
While (X --){
Printf ("% d = % s/n", X, (* P) [x]. c_str ());
}

Return 0;
}

//---------------------------------------------------------
// Avdll. cpp
// Compile options needed/GX
# Pragma warning (Disable: 4786)
# Include <map>
# Include <string>
# Include <stdlib. h>

// Create the map here in the DLL
_ Declspec (dllexport) STD: Map <int, STD: String> * givemeamap (int n)
{
STD: Map <int, STD: String> * m = new STD: Map <int, STD: String>;
While (n --){
Char B [33];
ITOA (n, B, 2 );
(* M) [N] = STD: string (B );
}
Return m;
}

// We can access the map without error from the executable
// Image where the map was created
_ Declspec (dllexport) void showmethemap (STD: Map <int, STD: String> * P)
{
Int x = p-> size ();
While (X --){
Printf ("% d = % s/n", X, (* P) [x]. c_str ());
}
}

// An accessor method to return the associated C string
// For key X
_ Declspec (dllexport) const char * mapitemx (STD: Map <int, STD: String> * m, int X)
{
Return (* m) [x]. c_str ();
}
 

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.