Inter-module call overload new and delete Problems

Source: Internet
Author: User
Inter-module call reload new and delete problem the call between modules reload new and delete problem: 1. Problem:
Run on our client Code New a class in the static library, and then delete it immediately,
An error occurred during the delete operation! The following code:
Kserver * psvr = new kserver;
Delete psvr; 2. cause:
Tracking found that the overloaded global operator new was introduced at the time of new (our client introduced the overload
The file header of the New and delete operators), but does not exist during the delete operation (the original Delete is called). 3. Why:
Not very clear, it may be a problem with the compiler. 4. solution:
[Method 1]:
Find the implementation code of the library and add the header files that reload the new and delete operators to solve the problem!
[Method 2]:
Find the implementation code of the library and write the constructor of the corresponding class to the header file. The problem is solved!
[Method 3]:
If there is no library implementation code (that is, only the library and header files are available ),
Add one in front of the delete statement: the indicator indicates that the global operator is called. The problem is solved! 5. Others:
In addition, after a test, we found that this problem would not occur if we changed the library to DLL. New and
Delete calls the global overload operator. 6. Appendix (test code of the problem ):
1. Dynamic library (DLL) project file:
//////////////////////////////////////// //////////////////////////////
// Svr1.h: interface for the svr1 class.
//
//////////////////////////////////////// //////////////////////////////
# Ifndef _ svr1_h
# DEFINE _ svr1_h // Note: Use the VC Wizard to generate a dynamic library project and define the macro: dll_exports in the project options,
// Add the svr1.h and svr1.cpp files.
# Ifdef dll_exports
# Define ksvr_api _ declspec (dllexport)
# Else
# Define ksvr_api _ declspec (dllimport)
# Endifclass ksvr_api svr1
{
Public:
Svr1 ();
Virtual ~ Svr1 ();
}; # Endif/_ svr1_h ///////////////////////////////// /////////////////////////////////////
// Svr1.cpp: Implementation of the svr1 class.
//
//////////////////////////////////////// //////////////////////////////
# Include "svr1.h" svr1: svr1 (){}
Svr1 ::~ Svr1 () {} II. Static library project file:
//////////////////////////////////////// //////////////////////////////
// Svr2.h: interface for the svr2 class.
//
//////////////////////////////////////// //////////////////////////////
# Ifndef _ svr2_h
# DEFINE _ svr2_h // Note: Use the VC Wizard to generate a static library project, and then add the svr2.h and svr2.cpp files. # DEFINE _ incpp // The call is not paired when new and delete objects are generated after this row is defined (New is an overloaded object, and delete is not)
Class svr2
{
Public:
# Ifdef _ incpp
Svr2 ();
# Else
Svr2 (){}
# Endif
Virtual ~ Svr2 ();
Void test ();
}; # Endif/_ svr2_h
//////////////////////////////////////// //////////////////////////////
// Svr2.cpp: Implementation of the svr2 class.
//
//////////////////////////////////////// //////////////////////////////
# Include "svr2.h" # ifdef _ incpp
Svr2: svr2 (){}
# Endifsvr2 ::~ Svr2 () {}3. test project file:
//////////////////////////////////////// //////////////////////////////
// Test. cpp: defines the entry point for the console application.
//
//////////////////////////////////////// //////////////////////////////
# DEFINE _ flip_mem_manager __# include "ktype. H" # include "DLL/svr1.h"
# Include "static/svr2.h" int main (INT argc, char * argv [])
{
// Test dll:
Svr1 * psvr1 = new svr1; // call overload operator new
Delete psvr1; // call overload operator Delete // test static:
Svr2 * psvr2 = new svr2; // call overload operator new
Delete psvr2; // whether call overload operator Delete depend on svr2's
// Construct function if implement in the CPP, if then not,
// Else call !!! // Below code will all call overload new and delete
Svr2 * Pok =: New svr2;
: Delete Pok; return 0;
} 4. Reload the header files of the New and delete operators.
//////////////////////////////////////// //////////////////////////////
// Ktype. h: defines the entry point for the console application.
//
//////////////////////////////////////// //////////////////////////////
# Ifndef _ type_h
# DEFINE _ type_h # include "windows. H" // note: this file is for testing only.
# Ifdef _ flip_mem_manager __
Static inline void * operator new (size_t size)
{
Void * P = malloc (size); // (_ file __, _ line __)
Outputdebugstring ("[here] overload new \ n ");
Return P;
}
Static inline void * operator new [] (size_t size)
{
Void * P = malloc (size); // (_ file __, _ line __)
Outputdebugstring ("[here] overload new [] \ n ");
Return P;
} Static inline void operator Delete (void * P)
{
Free (P );
Outputdebugstring ("[here] overload free \ n ");
}
Static inline void operator Delete [] (void * P)
{
Free (P );
Outputdebugstring ("[here] overload free [] \ n ");
}
# Endif/_ flip_mem_manager __# endif/_ type_h

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.