|
From: Roberto D. <rdi...@gm...> - 2009-04-01 06:31:03
|
Hello everybody.
Well I am trying to prepare a presentation about valgrind so I am making
some simple examples to show some common mistakes catched by valgrind..
The fact is that for this code I am not able to see valgrind reporting any
mistake:
#include <iostream>
class A
{
public:
int a () {return 0;}
};
int
main ()
{
A *a = new A;
std::cerr << "a: " << a << std::endl;
delete a;
// access to deleted memory
std::cerr << "rst: " << a->a () << std::endl;
}
If I compile this with g++ -ggdb3 -o prueba1 prueba1.cc and run it with
valgrind:
src/valgrind> valgrind ./prueba1
==14001== Memcheck, a memory error detector.
==14001== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==14001== Using LibVEX rev 1732, a library for dynamic binary translation.
==14001== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==14001== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
==14001== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==14001== For more details, rerun with: -v
==14001==
a: 0x4288028
rst: 0
==14001==
==14001== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 3 from 1)
==14001== malloc/free: in use at exit: 0 bytes in 0 blocks.
==14001== malloc/free: 1 allocs, 1 frees, 1 bytes allocated.
==14001== For counts of detected errors, rerun with: -v
==14001== All heap blocks were freed -- no leaks are possible.
src/valgrind>
I dont get mistakes.. am I missing something? shouldn't I receive an invalid
read message or something like that?
Thank you very much for your help.
Regards
Roberto
|
|
From: Konstantin S. <kon...@gm...> - 2009-04-01 06:57:17
|
On Wed, Apr 1, 2009 at 10:30 AM, Roberto Diaz <rdi...@gm...> wrote:
> Hello everybody.
>
> Well I am trying to prepare a presentation about valgrind so I am making
> some simple examples to show some common mistakes catched by valgrind..
>
> The fact is that for this code I am not able to see valgrind reporting any
> mistake:
>
>
> #include <iostream>
>
> class A
> {
> public:
> int a () {return 0;}
> };
>
>
> int
> main ()
> {
> A *a = new A;
> std::cerr << "a: " << a << std::endl;
> delete a;
>
> // access to deleted memory
>
> std::cerr << "rst: " << a->a () << std::endl;
I am not sure what C++ standard says about such code, but valgrind can
not see any problem here, because there is nothing wrong in the
executable binary.
The program calls A::a(a) which simply returns 0.
Change the code like this and you will get reports:
class A {
public:
A() : x_(0) {}
int a () {return x_;}
private:
int x_;
};
--kcc
> }
>
>
> If I compile this with g++ -ggdb3 -o prueba1 prueba1.cc and run it with
> valgrind:
>
> src/valgrind> valgrind ./prueba1
> ==14001== Memcheck, a memory error detector.
> ==14001== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
> ==14001== Using LibVEX rev 1732, a library for dynamic binary translation.
> ==14001== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
> ==14001== Using valgrind-3.2.3, a dynamic binary instrumentation framework.
> ==14001== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
> ==14001== For more details, rerun with: -v
> ==14001==
> a: 0x4288028
> rst: 0
> ==14001==
> ==14001== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 3 from 1)
> ==14001== malloc/free: in use at exit: 0 bytes in 0 blocks.
> ==14001== malloc/free: 1 allocs, 1 frees, 1 bytes allocated.
> ==14001== For counts of detected errors, rerun with: -v
> ==14001== All heap blocks were freed -- no leaks are possible.
> src/valgrind>
>
> I dont get mistakes.. am I missing something? shouldn't I receive an invalid
> read message or something like that?
>
>
> Thank you very much for your help.
>
>
>
> Regards
>
> Roberto
>
>
>
>
> ------------------------------------------------------------------------------
>
> _______________________________________________
> Valgrind-users mailing list
> Val...@li...
> https://lists.sourceforge.net/lists/listinfo/valgrind-users
>
>
|
|
From: Florian K. <br...@ac...> - 2009-04-01 12:02:47
|
On Wednesday 01 April 2009 2:30:54 am Roberto Diaz wrote:
>
> The fact is that for this code I am not able to see valgrind reporting any
> mistake:
>
>
> #include <iostream>
>
> class A
> {
> public:
> int a () {return 0;}
> };
>
>
> int
> main ()
> {
> A *a = new A;
> std::cerr << "a: " << a << std::endl;
> delete a;
>
> // access to deleted memory
>
> std::cerr << "rst: " << a->a () << std::endl;
> }
>
>
> If I compile this with g++ -ggdb3 -o prueba1 prueba1.cc and run it with
> valgrind:
>
[snip]
>
> I dont get mistakes.. am I missing something? shouldn't I receive an invalid
> read message or something like that?
>
The memory pointed to by "a" is not accessed.
a->a() gets converted to something like method_a_of_class_A(a)
and
int method_a_of_class_A(A *this)
{
return 0;
}
You're passing a pointer to some deallocated memory but it's not being dereferenced.
Hence no complaint.
Florian
|