After reading the “possibly lost” block message with Valgrind, it seems like they are bad.
I get an error for a member of a static pointer class. I want to check there is nothing wrong with our code.
I get 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 belongs: I simplified the code to publish it here, but if necessary I can add more detailed information.
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 a static pointer correctly. However, the documentation says that this is usually a memory leak unless you do something funny with your pointers. I don’t think we are doing something funny.
, ?
, , ?