Recently, I ran into problems with memory allocations made in one DLL (or * .so - portable code) and freeing done in another DLL. Errors that I have encountered so far:
- It just doesn't work - assert () fails when debugging.
- This does not work if one DLL was statically linked to the standard C library and another DLL dynamically linked to it.
- This does not work if one DLL performs the allocation, then the DLL is unloaded, and the other DLL tries to free this memory.
Basically, I decided that I should follow in order not to make selections in one DLL and not release it in another (and it is preferable to store it in one cpp file). Usually this also means that I should not make a selection in the header file, which can be used by several DLLs. This means I should not make selections in tempaltes (since they are all in the header), and this is a pretty big limitation.
When I need to create a new object in the template, now I select the cpp file for it and only then run it c'tor with the new placement operator.
class MyBase
{
public:
static void* allocate(std::size_t i_size);
};
template <typename T>
class MyClass: MyBase
{
public:
T* createT();
};
temlpate <typename T>
T* MyClass<T>::createT()
{
void* pMem = MyBase::allocate( sizeof(T) );
return new (pMem) T;
}
void* MyBase::allocate(std::size_t i_size)
{
return malloc( i_size );
}
While this works, it's a little ugly. This means writing template code without using a new one.
, , , const ( ) ( , const ). STL. , , , , ( HP-UX), , ' .
- , ?