Strange problems with a new / delete in an application using my dll

Sorry for the cryptic title of the question. I have a strange problem and I have no idea why this is happening. Fortunately, this code is pretty simple. But if we get to it, let me briefly describe my application. This is a multi-threaded application that serves a large amount of data. Something like an in-ram database. You can have several "databases" in it and load / unload them at runtime. Now the problem is freeing up memory. See below code (class names, etc. Changed, but it does not matter):

void SS::AllocTree( double*** pba, int i, int d, int b, int split ) { this->m_tree = new my_tree( pba, i, d, b, split ); } void SS::DeallocTree() { delete this->m_tree; this->m_tree = NULL; } 

Each time delete this->m_tree , the program crashes. The stack trace looks like this:

  mydll.dll!_free_base(void * pBlock=0x0000000008496f70) Line 109 + 0x14 bytes C mydll.dll!_free_dbg_nolock(void * pUserData=0x0000000008496fa0, int nBlockUse=0x00000001) Line 1428 C++ mydll.dll!_free_dbg(void * pUserData=0x0000000008496fa0, int nBlockUse=0x00000001) Line 1258 + 0xe bytes C++ mydll.dll!operator delete(void * pUserData=0x0000000008496fa0) Line 54 + 0x12 bytes C++ mydll.dll!my_tree::`vector deleting destructor'() + 0x94 bytes C++ myprog.exe!SS::DeallocTree() Line 57 + 0x34 bytes C++ myprog.exe!SSUnloader(void * arg=0x00000000084d6f80) Line 1038 C++ msvcr90d.dll!_callthreadstart() Line 295 C msvcr90d.dll!_threadstart(void * ptd=0x00000000084dad30) Line 277 C 

Here is the stack trace to select the tree:

  msvcr90d.dll!malloc(unsigned __int64 nSize=0x0000000000000058) Line 56 + 0x21 bytes C++ msvcr90d.dll!operator new(unsigned __int64 size=0x0000000000000058) Line 59 + 0xa bytes C++ myprog.exe!SS::AllocTree(double * * * pba=0x0000000008458ff0, int i=0x00000bde, int d=0x00000010, int b=0x00000008, int split=0x00000001) Line 52 + 0xa bytes C++ myprog.exe!SSLoader(void * arg=0x000000000843cf80) Line 932 C++ msvcr90d.dll!_callthreadstart() Line 295 C msvcr90d.dll!_threadstart(void * ptd=0x0000000008440d30) Line 277 C 

As you can see, loading / unloading is performed by a separate thread specially created for this task. Nothing that I use any fancy stuff, no heaps or nothing, no custom new / delete statements in my DLL or my program. I have no idea why the program enters my DLL and causes a deletion there, but with a new one, this does not happen. If I changed DeallocTree() to look like this:

 void SS::DeallocTree() { ::operator delete( this->m_tree ); this->m_tree = NULL; } 

Then everything works fine. However, I am not sure if this is correct. Should I do something similar for the new operator? And how can I be sure that this same problem does not occur anywhere? To achieve the goal, I also attach a stack trace for this version of DeallocTree ():

  msvcr90d.dll!operator delete(void * pUserData=0x00000000086f5fa0) Line 45 + 0xa bytes C++ myprog.exe!SS::DeallocTree() Line 58 C++ myprog.exe!SSUnloader(void * arg=0x0000000008735f80) Line 1038 C++ msvcr90d.dll!_callthreadstart() Line 295 C msvcr90d.dll!_threadstart(void * ptd=0x0000000008739d30) Line 277 C 

Can someone explain to me what is going on here?

EDIT:
To clarify:
my.dll loads dynamically - VS 2008 output: myprog.exe ': Loaded' C: * \ Debug \ mydll.dll ', the characters are loaded.
Note. I am correctly using the debug version of the dll with the debug version of my program and vice versa with the release.
** my_tree is declared as: my_tree * m_tree; // tree

+4
source share
1 answer

Well, the hint seems to be in the tables. In the β€œdelete” freeze frame, you will notice that it invokes various delete functions, etc. Directly in mydll.dll. In distribution, distribution is performed using msvcr90d.dll.

You have the flag / MDd (or / MD in the release) set on your exe and a / MTd (or / MT in the version) set in your dll. Install them both in / MDd (or / MD in the release), and your problems will disappear ... Basically, you will install both exe and dll for calls in the CRT DLL instead of trying to do this in 2 different ways ...

+6
source

All Articles