views:

136

answers:

7

I'm quite new to C++ but have some basic C knowledge. In my past C (university) projects, I used Valgrind to check for memleaks.

Now, with C++, is Valgrind a valid tool? Does C++ suffer the same problems concerning memleaks like C? Or are there even better tools to use in conjunction with C++?

+4  A: 

Valgrind can be used to check memleaks in c++ also

valgrind has so many options which will give you info and you can explore callgrind also.

--Cheers

Koteswara sarma
+2  A: 

Memory leaks are a concern to myself as a C++ developer. I assume they are a concern to other developers as well, though I cannot speak for everyone. Valgrind is a fantastic tool in this space and one I really could not live without.

Sam Miller
+7  A: 

I never use new and delete (or other forms of manual memory management) and I very rarely even use pointers. And I still have to wrestle with memory leaks invalid memory accesses.1 Valgrind is an indispensable tool for me. Even more important than gdb.


1 As Viktor pointed out in a comment, producing memory leaks without manual memory management would be pretty weird (discounting circular references and other special cases).

Konrad Rudolph
Then I suggest you use different libraries.
Viktor Sehr
@Viktor: admittedly, this *is* largely a fault of the library. But even modern STL implementations happily accept an out-of-range access on `operator []` without uttering so much as a warning, even in debug built (GCC …).
Konrad Rudolph
@Konrad Rudolph; But thats not a memory leak? I don't wanna be cocky, but if you never writer "= new" in your code (note the "="), you dont get memory leaks (yes, shared_ptrs can cross-reference each other, but that occurs very rarely)
Viktor Sehr
@Viktor: right, bad example. Still, it’s a missing diagnostic on the part of the standard library implementation which Valgrind spots. Now that I think of it, actual leaks I rarely produce any more. But invalid memory reads are another big problem. I’ll correct my answer. And thanks for spotting this. It *is* a mistake.
Konrad Rudolph
+1  A: 

Valgrind is the best tool out there for dealing with memory errors (but do check the other modules beside memcheck).

C-style programming is a valid (and widely used) programming approach in C++, therefore yes, memory problems are still an issue.

Let_Me_Be
Have only used memcheck yet. What other modules can you recommend?
Helper Method
+4  A: 

While C++ has much better memory handling than C, it's still quite possible to mess up. Smart pointers are great, but it's possible to make mistakes with them. That's what valgrind is for.

David Thornley
+1  A: 

yes, it is.

i use dynamic allocation by default in unit tests (with auto pointers, or an idiomatic equivalent), to explicitly check additional for memory errors which valgrind may detect. valgrind, guardmalloc, leaks, etc. can catch many errors before they enter production code.

Justin
+1  A: 

remember to tell gcc runtime to not use its own private memory pool otherwise you will confuse valgrind

GLIBCPP_FORCE_NEW=1

pm100
Apparently it's `GLIBCXX_FORCE_NEW` from gcc 3.4 onwards. you might want to link to [Valgrind's FAQ](http://valgrind.org/docs/manual/faq.html#faq.reports) about this.
Hasturkun