clearing a vector of pointers [duplicate]

Yes, the code has a memory leak unless you delete the pointers. If the foo class owns the pointers, it is its responsibility to delete them. You should do this before clearing the vector, otherwise you lose the handle to the memory you need to de-allocate. You could avoid the memory management issue altogether by … Read more

Valgrind: invalid read of size 4 -> sigsegv, works fine without valgrind and in visual studio

I’ll explain the first error to you. At line 331, you’re probably reading an (unsigned) int, in a part of the memory you haven’t allocated for your own program. This part gives more information about the part of memory you tried to read. It says you’ve already used the memory, but reallox freed it. That … Read more

Incorrect checksum for freed object on malloc

In read_response, you are probably overwriting the end of the buffer pointed to by buf. The problem is that buf is a pointer, so sizeof(buf) will return the size of a pointer (probably 4 or 8 depending on your CPU). You are using sizeof as if buf were an array, which is not really the same thing as a pointer in C although … Read more

Memory Clobbering Error

You are incrementing ptr, therefore changing the address that it points to. You can’t do that. In your case, have a separate pointer, let’s say char * p = ptr and do your operations with p leaving ptr intact so you can free(ptr) later. EDIT Taking a second look at your code, I found that … Read more

How to find memory leak in a C++ code/project?

Instructions Things You’ll Need Proficiency in C++ C++ compiler Debugger and other investigative software tools 1 Understand the operator basics. The C++ operator new allocates heap memory. The delete operator frees heap memory. For every new, you should use a delete so that you free the same memory you allocated: 2 Reallocate memory only if you’ve deleted. In the code below, str acquires a … Read more