After reading about "possibly lost" block message with Valgrind it seems they are bad.
I am getting the error for a static pointer class member. I want to verify there is nothing wrong with our code.
I am getting this from Valgrind:
==27986== 76 bytes in 1 blocks are possibly lost in loss record 370 of 1,143
==27986== at 0x4C247F0: operator new(unsigned long) (vg_replace_malloc.c:319)
==27986== by 0x107CFEE8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (new_allocator.h:94)
==27986== by 0xDDCE21F: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (basic_string.tcc:140)
==27986== by 0x107D19B2: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (basic_string.h:1722)
==27986== by 0xD381F19: MyNamespace::MyClass::MyMethod() (MyClass.cpp:189)
==27986== by 0xD1A6E17: __static_initialization_and_destruction_0(int, int) (IProperty.cpp:520)
==27986== by 0xD1B2B97: _GLOBAL__sub_I_IProperty.cpp (IProperty.cpp:551)
==27986== by 0x400D4F2: call_init (in /lib64/ld-2.5.so)
==27986== by 0x400D5B4: _dl_init (in /lib64/ld-2.5.so)
==27986== by 0x4000AA9: ??? (in /lib64/ld-2.5.so)
My class (Simplified) to which this error is referring to: I have simplified the code to post it here, but I can add more detail if required.
MyClass.h
class MyClass
{
private:
double _p1, _p2, _p3, _p4;
std::string _p5, _p6, _p7;
public:
MyClass(double p1, double p2, double p3, double p4, std::string p5, std::string p6, std::string p7)
{
_p1 = p1;
_p2 = p2;
_p3 = p3;
_p4 = p4;
this->_p5 = p5;
this->_p6 = p6;
this->_p7 = p7;
}
static MyClass& MyMethod();
}
MyClass.cpp
static MyClass* _myPtr = NULL;
MyClass& MyClass::MyMethod()
{
if (!_myPtr )
{
_myPtr = new MyClass(1, 2.1, 3, 4, "xxxx", "yyyyy", "zzzzz");
}
return *_myPtr ;
}
I think we are using the static pointer properly. However, the documentation says that this normally is a memory leak unless you are doing something funny with your pointers. I don't think we are doing anything funny.
Is this error actually referring to the internal pointers of the string class that the constructor receives as parameters?
Should we worry about this possibly lost block error?