Brief: In a "factory create" class I use a QMap
where the values are function pointers. Do I need to delete them on exit to avoid memory leak ? If yes, how ?
I am using a class factory (similar to this answer) to create items of different types.
Somewhere in my code there was a significant memory leak (which I have resolved), so now I'm running Valgrind to spot all memory leaks and other potential problems.
One issue that came up was, in creating items.
The stack trace...
# 0 replace_malloc [d:\drmemory_package\common\alloc_replace.c:2537]
# 1 QtCored4.dll!qMalloc [c:/n/qt-desktop/src/corelib/global/qmalloc.cpp:55]
# 2 QtCored4.dll!QMapData::node_create [c:/n/qt-desktop/src/corelib/tools/qmap.cpp:140]
# 3 MyDll.dll!QMap<>::node_create [c:/QtSDK/Desktop/Qt/4.7.3/mingw/include/QtCore/qmap.h:450]
# 4 MyDll.dll!QMap<>::operator[] [c:/QtSDK/Desktop/Qt/4.7.3/mingw/include/QtCore/qmap.h:531]
# 5 MyDll.dll!ClassFactory<>::registerType<> [C:\Users\folder/classfactory.h:31]
# 6 MyDll.dll!Caller::registerTypes [C:\Users\folder/caller.cpp:19]
The error (#5) on this line from linked code:
_createFuncs[name] = &createFunc<TDerived>;
I don't know if this may be a false positive or an actual memory leak that I should fix ?
I have tried to add a destructor that calls _createFuncs.qDeleteAll()
- but I got an error about being unable to delete function pointers.
So I researched that error and read that function pointers should not be deleted !
So... Do I have a memory leak in that code, and if yes, how do I fix it ?