static pointer as class member leading to valgrind still reachable

I have following code example. Class foo has a private member bar, which is a static string pointer. In the main function I am doing something completely not relevant with class foo.

In the end, valgrind gave me: still reachable: 32 bytes in 1 blocks
I have no idea how to handle that. Can you give me a hint?
In the reality, class foo is given in another file of a university project. I coded my assignment without any connection with class foo (until valgrind told me memory leak happend) Do I need to change the file containing class foo, which is not part of my assignment?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <string>

class foo {
  foo() = default;

  ~foo() = default;

  private:
   static std::string* bar;
};

std::string* foo::bar = new std::string("0");

int main () {
    // do some work without foo
    return 0;
}


Details:

Compile with:
$ g++ test.cc -o test

Valgrind command and output:
$ valgrind --leak-check=full --show-reachable=yes --show-leak-kinds=all ./test
==2424== Memcheck, a memory error detector
==2424== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2424== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==2424== Command: ./test
==2424==
==2424==
==2424== HEAP SUMMARY:
==2424== in use at exit: 32 bytes in 1 blocks
==2424== total heap usage: 2 allocs, 1 frees, 72,736 bytes allocated
==2424==
==2424== 32 bytes in 1 blocks are still reachable in loss record 1 of 1
==2424== at 0x4C3017F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2424== by 0x1089ED: __static_initialization_and_destruction_0(int, int) (in /tmp/test)
==2424== by 0x108A76: _GLOBAL__sub_I__ZN3foo3barB5cxx11E (in /tmp/test)
==2424== by 0x108ACC: __libc_csu_init (in /tmp/test)
==2424== by 0x53FEB27: (below main) (libc-start.c:266)
==2424==
==2424== LEAK SUMMARY:
==2424== definitely lost: 0 bytes in 0 blocks
==2424== indirectly lost: 0 bytes in 0 blocks
==2424== possibly lost: 0 bytes in 0 blocks
==2424== still reachable: 32 bytes in 1 blocks
==2424== suppressed: 0 bytes in 0 blocks
==2424==
==2424== For counts of detected and suppressed errors, rerun with: -v
==2424== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Last edited on
A "still reachable" leak is not usually a serious problem.
In your case you simply need to delete the object if you want, just before main ends.

 
delete foo::bar;

Or maybe use a std::unique_ptr.
Last edited on
Thank you dutch.

delete foo::bar; this command doesn't work, since the bar is private

Assume we want a approach without unique_ptr, how can we fix this?

The reason is that, the class foo is in foo.cc and I did my class assignment in assignment.cc. The assignment is like to implement a data structure without any main function.
(I feel somehow it is a desgin problem of the assignment. The assignment tester accepts only the assignmen.cc file and checks the "still reachable".)

Last edited on
Try:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <memory>

class foo {
public:
  foo() = default;
  ~foo() = default;
private:
   static std::unique_ptr<std::string> bar;
};

std::unique_ptr<std::string> foo::bar = std::make_unique<std::string>("0");

int main () {
    // do some work without foo
    return 0;
}

Topic archived. No new replies allowed.