marpaia
marpaia

Reputation: 29

memory leak with allocated struct containing a vector

I'm running into an issue where, when I have a std::vector in a struct, and I heap allocate that struct, when the struct gets free'd, the vector leaks. Any ideas on how to prevent this?

Here's the code:

#include <cstdlib>
#include <string>
#include <vector>

struct foo {
  std::vector<std::string> bar;
};

const std::vector<std::string> kSample = {"test", "1", "2", "3", "4", "5"};

int main(int argc, char *argv[]) {
  struct foo *allocated = new foo;
  for (const auto& i : kSample) {
    allocated->bar.push_back(i);
  }
  free(allocated);
  return 0;
}

Here's the valgrind results from running this code:

==18131== 192 bytes in 1 blocks are definitely lost in loss record 51 of 76
==18131==    at 0x6DFB: malloc (in /usr/local/Cellar/valgrind/3.9.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==18131==    by 0x4728D: operator new(unsigned long) (in /usr/lib/libc++.1.dylib)
==18131==    by 0x10000283D: std::__1::__split_buffer<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >&) (in ./build/tools/test)
==18131==    by 0x100001EEC: std::__1::__split_buffer<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >&) (in ./build/tools/test)
==18131==    by 0x100001D9B: void std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >::__push_back_slow_path<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) (in ./build/tools/test)
==18131==    by 0x100001278: main (in ./build/tools/test)
==18131==
==18131== LEAK SUMMARY:
==18131==    definitely lost: 192 bytes in 1 blocks
==18131==    indirectly lost: 0 bytes in 0 blocks
==18131==      possibly lost: 0 bytes in 0 blocks
==18131==    still reachable: 0 bytes in 0 blocks
==18131==         suppressed: 25,768 bytes in 377 blocks

Upvotes: 1

Views: 556

Answers (1)

Eric Z
Eric Z

Reputation: 14505

You should call delete allocated;. new and delete should be used in pair.

Upvotes: 5

Related Questions