[so] Re: valgrind
Alexandru Mosoi
brtzsnr at gmail.com
Thu Nov 1 13:06:26 EET 2007
cosmin are dreptate, iar pentru exemplificare sa luam programul:
#include <cstdio>
#include <cstdlib>
class A {
public:
~A() {
printf("pe aici nu se trece\n");
}
};
int main() {
A a;
exit(0);
return 0;
}
ai sa vezi, ca acest program comfirma istoria :) (nu e apelat
destructorul lui a). daca comentezi linia cu exit(0) destructorul e
apelat. in prima varianta e o scurgere de memorie... cauzata de faptul
ca exit(0) nu distruge variabilele locale (dar le distruge pe cele
globale/statice). scurgerea asta nu e importanta din moment ce... iesi
din program si memoria oricum e eliberata de SO. ar fi o problema
mare... daca exit() s-ar intoarce :).
On 11/1/07, cosminratiu at gmail.com <cosminratiu at gmail.com> wrote:
> Cred ca situatia este urmatoarea: return 0 da sansa bibliotecii standard C
> sa isi apeleze destructorul de la clasa string. exit(0) iese imediat, fara
> sa apeleze destructorul.
>
> E o non-problema asta pentru tine :).
>
>
> On 11/1/07, Andrei Ciuca <andrei.ciuca at gmail.com> wrote:
> >
> > Salut, ai dreptate.
> > In timp ce scoteam cod, am observat urmatoarea chestie :
> > Daca rulez codul asta :
> >
> > int main()
> > {
> >
> > cout << "\n> ";
> > string line;
> > getline(cin, line);
> >
> > command_t* root = NULL;
> > parse_line(line.c_str(), &root);
> >
> > free_parse_memory();
> >
> > exit(0);
> >
> > }
> >
> > Da outputul asta :
> >
> > ==2981== Memcheck, a memory error detector.
> > ==2981== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
> > ==2981== Using LibVEX rev 1606, a library for dynamic binary translation.
> > ==2981== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
> > ==2981== Using valgrind-3.2.0-Debian, a dynamic binary instrumentation
> framework.
> > ==2981== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
> > ==2981== For more details, rerun with: -v
> > ==2981==
> > ==2981== My PID = 2981, parent PID = 2259. Prog and args are:
> > ==2981== ./tema1
> > ==2981==
> > --2981-- DWARF2 CFI reader: unhandled CFI instruction 0:50
> > --2981-- DWARF2 CFI reader: unhandled CFI instruction 0:50
> > ==2981==
> > ==2981== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 17 from 1)
> > ==2981== malloc/free: in use at exit: 17 bytes in 1 blocks.
> > ==2981== malloc/free: 12 allocs, 11 frees, 209 bytes allocated.
> > ==2981== For counts of detected errors, rerun with: -v
> > ==2981== searching for pointers to 1 not-freed blocks.
> > ==2981== checked 108,104 bytes.
> > ==2981==
> > ==2981== 17 bytes in 1 blocks are possibly lost in loss record 1 of 1
> > ==2981== at 0x401B8C7: operator new(unsigned) (vg_replace_malloc.c:163)
> > ==2981== by 0x40B0C6A:
> std::string::_Rep::_S_create(unsigned, unsigned,
> std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.8)
> > ==2981== by 0x40B1BE9:
> std::string::_Rep::_M_clone(std::allocator<char> const&,
> unsigned) (in /usr/lib/libstdc++.so.6.0.8)
> > ==2981== by 0x40B35E7: std::string::reserve(unsigned) (in
> /usr/lib/libstdc++.so.6.0.8)
> > ==2981== by 0x4089126: std::basic_istream<char, std::char_traits<char>
> >& std::getline<char, std::char_traits<char>, std::allocator<char>
> >(std::basic_istream<char, std::char_traits<char> >&,
> std::basic_string<char, std::char_traits<char>, std::allocator<char> >&,
> char) (in /usr/lib/libstdc++.so.6.0.8)
> > ==2981== by 0x40A2178: std::basic_istream<char, std::char_traits<char>
> >& std::getline<char, std::char_traits<char>, std::allocator<char>
> >(std::basic_istream<char, std::char_traits<char> >&,
> std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (in
> /usr/lib/libstdc++.so.6.0.8)
> > ==2981== by 0x80493E9: main (tema1.cpp:875)
> > ==2981==
> > ==2981== LEAK SUMMARY:
> > ==2981== definitely lost: 0 bytes in 0 blocks.
> > ==2981== possibly lost: 17 bytes in 1 blocks.
> > ==2981== still reachable: 0 bytes in 0 blocks.
> > ==2981== suppressed: 0 bytes in 0 blocks.
> >
> >
> > Daca rulez codul asta (diferenta este ca in loc sa dau "exit(0)" dau
> "return 0" ):
> >
> > int main()
> > {
> >
> > cout << "\n> ";
> > string line;
> > getline(cin, line);
> >
> > command_t* root = NULL;
> > parse_line(line.c_str(), &root);
> >
> > free_parse_memory();
> >
> > return 0;
> > }
> >
> > atunci valgrind spune ca totul e ok - "no memory leaks possible".
> >
> > _______________________________________________
> > so mailing list
> > so at cursuri.cs.pub.ro
> > http://cursuri.cs.pub.ro/cgi-bin/mailman/listinfo/so
> >
> >
>
>
> _______________________________________________
> so mailing list
> so at cursuri.cs.pub.ro
> http://cursuri.cs.pub.ro/cgi-bin/mailman/listinfo/so
>
>
--
Alexandru Moşoi
http://alexandru.mosoi.googlepages.com
More information about the so
mailing list