0

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 ?

Community
  • 1
  • 1
Thalia
  • 13,637
  • 22
  • 96
  • 190
  • 3
    Nope you don't. You only delete heap memory allocations. And functions are not it. – dtech Jan 04 '16 at 20:39
  • Youe have to delete everything you've created with the `create` Methode. – thomas Jan 04 '16 at 22:44
  • @tpr - I create QGraphicsItem* items that are added to a scene so that is not an issue. I have been very careful with my items. – Thalia Jan 05 '16 at 15:01
  • @ddriver It seems your comment would be an answer... (I hope it's right :-) already upvoted 3 times). So will you please put it as an answer ? Thank you. – Thalia Jan 05 '16 at 15:03
  • @Thalia - the comment is enough I think... – dtech Jan 05 '16 at 15:13

0 Answers0