overloading new/delete operator
I have overloaded new and delete (here is the full post):
Code Block 1
<code>
inline void* operator new(size_t size) throw() {
return TC::MemoryPoolManager::getInstance().allocateMemory(size);
}
inline void operator delete(void* deletable, size_t size) throw() {
TC::MemoryPoolManager::getInstance().releaseMemory(deletable,
size);
}
inline void* operator new [](size_t size) throw() {
return TC::MemoryPoolManager::getInstance().allocateMemory(size);
}
inline void operator delete[](void * deletable, size_t size) throw() {
TC::MemoryPoolManager::getInstance().releaseMemory(deletable,
size);
}
</code>
new operator is called without any problems, eg.:
Code Block 2
<code>
template<typename T>
T*** NMap<T>::alloc3DArray(int c, int sx, int sy) {
T*** data = new T**[c];
mem_begin_ = new T[c*sx*sy];
T* tmp;
for (int i = 0; i < c; i++) {
tmp = (mem_begin_ + i*sx*sy);
data[i] = new T*[sy];
for (int y = 0; y < sy; y++) {
data[i][y] = (tmp + y*sx);
}
}
allocated++;
return data;
}
</code>
but the delete operator is called from somewhere else (it is not
called the one that is defined by me [Code block 1]):
Code Block
<code>
template<typename T>
void NMap<T>::free3DArray(T*** data, int c) {
for (int i = 0; i < c; i++) {
T** d = data[i]; //some external delete
delete[] d;
}
delete[] data;
delete[] mem_begin_;
deallocated++;
}
</code>
where is the problem ???
I use in my project some static libraries and one of them contains
definitions of new/delete operators:
Code Block
<code>
// Custom memory allocator
namespace nv {
namespace mem {
NVCORE_API void * malloc(size_t size);
NVCORE_API void * malloc(size_t size, const char * file, int
line);
NVCORE_API void free(const void * ptr);
NVCORE_API void * realloc(void * ptr, size_t size);
} // mem namespace
} // nv namespace
// Override new/delete
inline void * operator new (size_t size) throw() {
return nv::mem::malloc(size);
}
inline void operator delete (void *p) throw() {
nv::mem::free(p);
}
inline void * operator new [] (size_t size) throw() {
return nv::mem::malloc(size);
}
inline void operator delete [] (void * p) throw() {
nv::mem::free(p);
}
</code>
When I put in my code non-inline version of new and delete:
Code Block
<code>
void* operator new(size_t size) throw() {
return TC::MemoryPoolManager::getInstance().allocateMemory(size);
}
void operator delete(void* deletable, size_t size) throw() {
TC::MemoryPoolManager::getInstance().releaseMemory(deletable,
size);
}
void* operator new [](size_t size) throw() {
return TC::MemoryPoolManager::getInstance().allocateMemory(size);
}
void operator delete[](void * deletable, size_t size) throw() {
TC::MemoryPoolManager::getInstance().releaseMemory(deletable,
size);
}
</code>
I got following linker errors (they don't appear if I precede my new/
delete definitions by inline keyword - but it still does not work):
------ Build started: Project: BTC_TC, Configuration: Debug Win32
------
Compiling...
Config.cpp
main.cpp
Generating Code...
Compiling manifest to resources...
Linking...
LINK : C:\Documents and Settings\indy\Moje dokumenty\Visual Studio
2005\Projects\NMHaar\Debug\BTC_TC.exe not found or not built by the
last incremental link; performing full link
nvcore.lib(StrLib.obj) : error LNK2005: "void * __cdecl operator
new(unsigned int)" (??2@YAPAXI@Z) already defined in main.obj
nvcore.lib(Memory.obj) : error LNK2005: "void * __cdecl operator
new(unsigned int)" (??2@YAPAXI@Z) already defined in main.obj
nvcore.lib(Debug.obj) : error LNK2005: "void * __cdecl operator
new(unsigned int)" (??2@YAPAXI@Z) already defined in main.obj
nvimage.lib(Quantize.obj) : error LNK2005: "void * __cdecl operator
new(unsigned int)" (??2@YAPAXI@Z) already defined in main.obj
nvimage.lib(Quantize.obj) : error LNK2005: "void * __cdecl operator
new[](unsigned int)" (??_U@YAPAXI@Z) already defined in main.obj
nvimage.lib(Image.obj) : error LNK2005: "void * __cdecl operator
new(unsigned int)" (??2@YAPAXI@Z) already defined in main.obj
nvimage.lib(ImageIO.obj) : error LNK2005: "void * __cdecl operator
new(unsigned int)" (??2@YAPAXI@Z) already defined in main.obj
nvimage.lib(ImageIO.obj) : error LNK2005: "void * __cdecl operator
new[](unsigned int)" (??_U@YAPAXI@Z) already defined in main.obj
Creating library C:\Documents and Settings\indy\Moje dokumenty
\Visual Studio 2005\Projects\NMHaar\Debug\BTC_TC.lib and object C:
\Documents and Settings\indy\Moje dokumenty\Visual Studio 2005\Projects
\NMHaar\Debug\BTC_TC.exp
C:\Documents and Settings\indy\Moje dokumenty\Visual Studio
2005\Projects\NMHaar\Debug\BTC_TC.exe : fatal error LNK1169: one or
more multiply defined symbols found
Build log was saved at "file://c:\Documents and Settings\indy\Moje
dokumenty\Visual Studio 2005\Projects\NMHaar\NMHaar\Debug
\BuildLog.htm"
BTC_TC - 9 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========
How can I disable the new/delete operators from static libraries ??