سؤال

as a newbie valgrind user I can't figure out the reason why it outputs the following message

40 bytes in 1 blocks are definitely lost in loss

the offending code lines are the following:

void KukaDevice::_init()
{
    m_ops.insert(KukaDeviceSpace::OFF , &KukaDevice::_doNothing);
    m_ops.insert(KukaDeviceSpace::INITIALIZING ,&KukaDevice::_doInitialization);
    m_ops.insert(KukaDeviceSpace::STARTING ,&KukaDevice::_doStarting);
    m_ops.insert(KukaDeviceSpace::MONITORING ,&KukaDevice::_doMonitoring);
    m_ops.insert(KukaDeviceSpace::WORKING ,&KukaDevice::_doWorking);
    m_ops.insert(KukaDeviceSpace::STOPPING ,&KukaDevice::_doStop);
    m_ops.insert(KukaDeviceSpace::SHUTTINGDOWN ,&KukaDevice::_doShutdown);
}

where the variables are defined as:

#ifndef KukaDevice_H
#define KukaDevice_H

#include <QMap>

class KukaDevice : public QObject
{
    Q_OBJECT
/// High Level Operations
void _doNothing(); /// waits 10 ms
void _doInitialization();
void _doStarting(); 
void _doMonitoring();
void _doWorking();
void _doStop();
void _doShutdown();

/// Initialization
void _init(); 

typedef void (KukaDevice::*doFunc)();
typedef QMap<int,doFunc> OpStack;
OpStack m_ops;
};

#endif // KukaDevice_H

Any hint? Is it possible that QMap is generating the leak? Am I using QMap in a wrong way? Thanks!

Edit: Maybe this is relevant information: the message is displayed only when I terminate the application.

هل كانت مفيدة؟

المحلول

What Valgrind is saying is when you exit your application there is still allocated memory in the QMap. Indeed this would be true of any container implementation where you add a bunch of references you never clean-up.

If the object is a long-lived object that lives for the lifetime of the application then it's not really a leak. However if if you ever destroy the object of class KukaDevice you need to ensure you have cleared out the QMap as the stored items will be living in the heap.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top