Member Avatar for liveshell

I have following peace of code, when i check it with valgrind it gives me "537 bytes in 19 blocks are possibly lost in loss record 12 of 12"

Following is the code:

CManager.h

class CManager
enum RespType_en
{
	eInvalidRes = 0,	
	eAuthRes,
	eActivateRes,
	eSetStateRes
}

map<RespType_en, string>		m_mRespTags;

in CManager.cpp

CManager::CManager()
{
    GenerateResponseTagMap();
}

bool CManager::GenerateResponseTagMap()
{
    m_mRespTags[eInvalidRes]               = "InvalidRes";
  
    m_mRespTags[eAuthRes]                = "AuthRes";
	
    m_mRespTags[eActivateRes]		    = "ActivateRes";
    
    m_mRespTags[eSetStateRes]            = "SetStateRes";
	
	return true;
}

and the valgrind reports :

==3152== 537 bytes in 19 blocks are possibly lost in loss record 12 of 12
==3152==    at 0x4020B65: operator new(unsigned) (vg_replace_malloc.c:163)
==3152==    by 0x453DB0A: std::string::_Rep::_S_create(unsigned, unsigned, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.8)
==3152==    by 0x453F837: std::string::_M_mutate(unsigned, unsigned, unsigned) (in /usr/lib/libstdc++.so.6.0.8)
==3152==    by 0x453F9D9: std::string::_M_replace_safe(unsigned, unsigned, char const*, unsigned) (in /usr/lib/libstdc++.so.6.0.8)
==3152==    by 0x453FA74: std::string::assign(char const*, unsigned) (in /usr/lib/libstdc++.so.6.0.8)
==3152==    by 0x453FBF4: std::string::operator=(char const*) (in /usr/lib/libstdc++.so.6.0.8)
==3152==    by 0x8099A73: CManager::GenerateResponseTagMap() (in /test/bin/Service)
==3152==    by 0x8099E26: CManager::CManager() (in /test/bin/Service)
==3152==    by 0x8099EE1: CManager::Instance(bool) (in /test/bin/Service)
==3152==    by 0x8069E27: main (in /test/bin/Service)

So is there any memory leak in
bool CManager::GenerateResponseTagMap() ??

So is there any memory leak in
bool CManager::GenerateResponseTagMap() ??

It looks like a perfectly valid way of doing what you are doing there. However, you may be leaking CManager object(s). So, how are you allocating those objects, also are you using static CManager objects?

Post an actual example!

My simple test has no issues.

#include <iostream>
#include <iomanip>
#include <string>
#include <map>
using namespace std;

class CManager {
  enum RespType_en
  {
    eInvalidRes = 0,
    eAuthRes,
    eActivateRes,
    eSetStateRes
  };
  map<RespType_en, string>    m_mRespTags;
  public:
    CManager();
    bool GenerateResponseTagMap();
};

CManager::CManager()
{
  GenerateResponseTagMap();
}

bool CManager::GenerateResponseTagMap()
{
  m_mRespTags[eInvalidRes]        = "InvalidRes";
  m_mRespTags[eAuthRes]           = "AuthRes";
  m_mRespTags[eActivateRes]       = "ActivateRes";
  m_mRespTags[eSetStateRes]       = "SetStateRes";
  return true;
}

int main ( ) {
  CManager  foo;
}


$ g++ foo.cpp
$ valgrind ./a.out 
==3471== Memcheck, a memory error detector.
==3471== Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al.
==3471== Using LibVEX rev 1884, a library for dynamic binary translation.
==3471== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP.
==3471== Using valgrind-3.4.1-Debian, a dynamic binary instrumentation framework.
==3471== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al.
==3471== For more details, rerun with: -v
==3471== 
==3471== 
==3471== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 17 from 1)
==3471== malloc/free: in use at exit: 0 bytes in 0 blocks.
==3471== malloc/free: 8 allocs, 8 frees, 187 bytes allocated.
==3471== For counts of detected errors, rerun with: -v
==3471== All heap blocks were freed -- no leaks are possible.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.