Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor / Destructor Linking Error

I have a project (that creates a dll), say test.dll, where I am not exporting the constructor and destructor of a class, say TestClass, out in the dll. This is because I have some factory functions that are supposed to be invoked to create and destroy objects of TestClass. This design works perfectly fine in stand alone examples that I create where I try to use objects of TestClass from test.dll.

However when I link this test.dll (or the .lib in my case since I am using Visual Studio) to a project in our production module, I get weird linking errors pointing to the constructor and destructor that it could not find for TestClass. I know for a fact that I don't call new / delete, or create any stack instance of TestClass anywhere in the project. The production module uses C#/CLR/CLI along with C++ I think. The only way to get around is for me to export the constructor and destructor for TestClass. This is undesirable by design.

Is this situation familiar to anyone? Can some one point to what might be the issue?

This is the error that I get:

Error   5264    error LNK2028: unresolved token (0A000BA3) "public: virtual __thiscall BE::TestClass::~TestClass(void)" (??1TestClass@BE@@$$FUAE@XZ) referenced in function "public: virtual void * __thiscall BE::TestClass::`vector deleting destructor'(unsigned int)" (??_ETestClass@BE@@$$FUAEPAXI@Z)  AMBestDetailBridge.obj  BEBase

Error   5373    error LNK2001: unresolved external symbol "public: virtual __thiscall BE::TestClass::~TestClass(void)" (??1TestClass@BE@@$$FUAE@XZ) AMBestDetailBridge.obj  BEBase

Thanks!

like image 531
Goutham Avatar asked Nov 14 '22 00:11

Goutham


1 Answers

The link error says you are calling destructor through delete [], so it is most probably that in your production module, you have certain code:

TestClass* pTest = ...
delete pTest;
TestClass* pTestArray = ...
delete[] pTest;

Of course , it might be not that obvious, in case of smart pointers:

SmartPtr<TestClass> spTest = ...
// delete called automatically when out of scope.

Allocate in one module and delete in another will put yourself in potential risk - because the 2 modules may use different heap for memory allocation (say you redefine new operators), it is lucky that this problem is exposed in link time, not run time.

like image 127
Baiyan Huang Avatar answered Dec 02 '22 01:12

Baiyan Huang