Hello, can someone tell me why this would be leaking one byte and how to stop it from doing so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <cstdlib>
#include <iostream>
usingnamespace std;
class Test
{
public:
Test()
{
}
};
int main()
{
Test *test = new Test();
return 0;
}
This is the valgrind report from it:
==6922== Memcheck, a memory error detector
==6922== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==6922== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==6922== Command: ./a.out -huff source.txt dest.txt
==6922==
==6922==
==6922== FILE DESCRIPTORS: 3 open at exit.
==6922== Open file descriptor 2: /dev/pts/0
==6922== <inherited from parent>
==6922==
==6922== Open file descriptor 1: /dev/pts/0
==6922== <inherited from parent>
==6922==
==6922== Open file descriptor 0: /dev/pts/0
==6922== <inherited from parent>
==6922==
==6922==
==6922== HEAP SUMMARY:
==6922== in use at exit: 1 bytes in 1 blocks
==6922== total heap usage: 1 allocs, 0 frees, 1 bytes allocated
==6922==
==6922== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
==6922== at 0x402569A: operator new(unsigned int) (vg_replace_malloc.c:255)
==6922== by 0x80485E9: main (in /home/ubuntu/Desktop/a.out)
==6922==
==6922== LEAK SUMMARY:
==6922== definitely lost: 1 bytes in 1 blocks
==6922== indirectly lost: 0 bytes in 0 blocks
==6922== possibly lost: 0 bytes in 0 blocks
==6922== still reachable: 0 bytes in 0 blocks
==6922== suppressed: 0 bytes in 0 blocks
==6922==
==6922== For counts of detected and suppressed errors, rerun with: -v
==6922== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 19 from 8)
Ah duh, I was thinking the destructor automatically deleted it, but I forgot that's only for data in the class itself. Thanks.
If you create a variable with new you must call the corresponding delete. That's never done (even not in the class) automatically. If you want that use smart pointer.