Here is my code that has a list of structs. Each struct has id and vector. Essentially I have insert function that adds new node into keys list. Object is created inside the insert when the function is called. Once insertion is finished I want to avoid any leaks I hope for your help and suggestions!
struct Node {
vector<int> data; //actual int vector
int id; //node id
};
list<Node *> keys;
//method{
Node *temp;
if (temp->id != key){
temp = new Node;
temp->id = key;
temp->data.push_back(value);
keys.push_back(temp);
I have troubles in that part of code:
temp = nullptr;
delete(temp); //that's how I'm trying to fix leak.
}
//}
Valgrind:
==4199== HEAP SUMMARY:
==4199== in use at exit: 620 bytes in 20 blocks
==4199== total heap usage: 82 allocs, 62 frees, 75,452 bytes allocated
==4199==
==4199== 4 bytes in 1 blocks are indirectly lost in loss record 1 of 4
==4199== at 0x4C3017F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4199== by 0x10B621: __gnu_cxx::new_allocator<int>::allocate(unsigned long, void const*) (in /home/admin/CLionProjects/A2/a2)
==4199== by 0x10B0EA: std::allocator_traits<std::allocator<int> >::allocate(std::allocator<int>&, unsigned long) (in /home/admin/CLionProjects/A2/a2)
==4199== by 0x10AA05: std::_Vector_base<int, std::allocator<int> >::_M_allocate(unsigned long) (in /home/admin/CLionProjects/A2/a2)
==4199== by 0x10A07D: void std::vector<int, std::allocator<int> >::_M_realloc_insert<int const&>(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&) (in /home/admin/CLionProjects/A2/a2)
==4199== by 0x109A91: std::vector<int, std::allocator<int> >::push_back(int const&) (in /home/admin/CLionProjects/A2/a2)
==4199== by 0x1096F6: key_value_sequences::insert(int, int) (in /home/admin/CLionProjects/A2/a2)
==4199== by 0x108F93: main (in /home/admin/CLionProjects/A2/a2)
==4199==
==4199== 36 (32 direct, 4 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 4
==4199== at 0x4C3017F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4199== by 0x1096C7: key_value_sequences::insert(int, int) (in /home/admin/CLionProjects/A2/a2)
==4199== by 0x108F93: main (in /home/admin/CLionProjects/A2/a2)
==4199==
==4199== 296 bytes in 9 blocks are indirectly lost in loss record 3 of 4
==4199== at 0x4C3017F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4199== by 0x10B621: __gnu_cxx::new_allocator<int>::allocate(unsigned long, void const*) (in /home/admin/CLionProjects/A2/a2)
==4199== by 0x10B0EA: std::allocator_traits<std::allocator<int> >::allocate(std::allocator<int>&, unsigned long) (in /home/admin/CLionProjects/A2/a2)
==4199== by 0x10AA05: std::_Vector_base<int, std::allocator<int> >::_M_allocate(unsigned long) (in /home/admin/CLionProjects/A2/a2)
==4199== by 0x10A07D: void std::vector<int, std::allocator<int> >::_M_realloc_insert<int const&>(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&) (in /home/admin/CLionProjects/A2/a2)
==4199== by 0x109A91: std::vector<int, std::allocator<int> >::push_back(int const&) (in /home/admin/CLionProjects/A2/a2)
==4199== by 0x109627: key_value_sequences::insert(int, int) (in /home/admin/CLionProjects/A2/a2)
==4199== by 0x108F93: main (in /home/admin/CLionProjects/A2/a2)
==4199==
==4199== 584 (288 direct, 296 indirect) bytes in 9 blocks are definitely lost in loss record 4 of 4
==4199== at 0x4C3017F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4199== by 0x10964F: key_value_sequences::insert(int, int) (in /home/admin/CLionProjects/A2/a2)
==4199== by 0x108F93: main (in /home/admin/CLionProjects/A2/a2)
==4199==
==4199== LEAK SUMMARY:
==4199== definitely lost: 320 bytes in 10 blocks
==4199== indirectly lost: 300 bytes in 10 blocks
==4199== possibly lost: 0 bytes in 0 blocks
==4199== still reachable: 0 bytes in 0 blocks
==4199== suppressed: 0 bytes in 0 blocks
==4199==
==4199== For counts of detected and suppressed errors, rerun with: -v
==4199== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)