PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : [Linux C++] g++ und valgrind / Speicher freigeben


Aqualon
2005-11-04, 15:14:57
H!

Ich spiel gerade ein wenig mit C++ und wollte mit valgrind rausfinden, ob ich auch den ganzen Speicher freigebe. Valgrind meint dann, dass 2 allocs gemacht wurden, aber nur 1 free, er zeigt mir allerdings nicht an, wo der Speicher allokiert wird, der nicht freigegeben wurde.

Hier erstmal der Code:
helloworld.cpp

#include <iostream>
#include "helloworld.h"

using namespace std;

helloworld::helloworld(string x) {
msg = x;
}

helloworld::~helloworld() {
cout << "this is the end..." << endl;
}

void helloworld::print() {
cout << msg << endl;
}

int main(int argc, char** argv) {

//helloworld
helloworld *myworld;

try {
myworld = new helloworld("hallo");
myworld->print();
delete myworld;
}
catch(bad_alloc) {
cerr << "kein Speicher verfuegbar" << endl; exit(1);
}
catch(...) {
cerr << "unbekannter Fehler" << endl; exit(1);
}
}

helloworld.h

#include <string>
using std::string;

class helloworld {

public:
helloworld(string x);
~helloworld();
void print();

private:
string msg;

};


Valgrind bringt dann einige Fehlermeldungen:

==13035== Memcheck, a memory error detector for x86-linux.
==13035== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al.
==13035== Using valgrind-2.4.0, a program supervision framework for x86-linux.
==13035== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
==13035== Valgrind library directory: /usr/lib/valgrind
==13035== Command line
==13035== ./helloworld
==13035== Startup, with flags:
==13035== --suppressions=/usr/lib/valgrind/debian-libc6-dbg.supp
==13035== --show-reachable=yes
==13035== -v
==13035== --
==13035== Contents of /proc/version:
==13035== Linux version 2.4.32-pre3 (root@mephisto) (gcc version 3.3.5 (Debian 1:3.3.5-13)) #1 SMP Mon Oct 24 14:11:51 CEST 2005
==13035== Reading syms from /home/cip/2004/sibresch/sos2/cpp/helloworld/helloworld (0x8048000)
==13035== Reading syms from /lib/ld-2.3.2.so (0x1B8E4000)
==13035== object doesn't have a symbol table
==13035== Reading debug info from /lib/ld-2.3.2.so...
==13035== ... CRC mismatch (computed E7117123 wanted 4ECF6D33)
==13035== object doesn't have any debug info
==13035== Reading syms from /usr/lib/valgrind/stage2 (0xB0000000)
==13035== Reading syms from /lib/ld-2.3.2.so (0xB1000000)
==13035== object doesn't have a symbol table
==13035== Reading debug info from /lib/ld-2.3.2.so...
==13035== ... CRC mismatch (computed E7117123 wanted 4ECF6D33)
==13035== object doesn't have any debug info
==13035== Reading syms from /lib/libdl-2.3.2.so (0xB1035000)
==13035== object doesn't have a symbol table
==13035== Reading debug info from /lib/libdl-2.3.2.so...
==13035== ... CRC mismatch (computed 6F61513E wanted 280D08E5)
==13035== object doesn't have any debug info
==13035== Reading syms from /lib/libc-2.3.2.so (0xB1038000)
==13035== object doesn't have a symbol table
==13035== Reading debug info from /lib/libc-2.3.2.so...
==13035== ... CRC mismatch (computed 76EC50B6 wanted 52619D67)
==13035== object doesn't have any debug info
==13035== Reading syms from /usr/lib/valgrind/vgskin_memcheck.so (0xB126C000)
==13035== Reading suppressions file: /usr/lib/valgrind/debian-libc6-dbg.supp
==13035== Reading suppressions file: /usr/lib/valgrind/default.supp
==13035==
==13035== Reading syms from /usr/lib/valgrind/vg_inject.so (0x1B8FE000)
==13035== Reading syms from /usr/lib/valgrind/vgpreload_memcheck.so (0x1B901000)
==13035== Reading syms from /usr/lib/libstdc++.so.5.0.7 (0x1B929000)
==13035== object doesn't have a symbol table
==13035== object doesn't have any debug info
==13035== Reading syms from /lib/libm-2.3.2.so (0x1B9E4000)
==13035== object doesn't have a symbol table
==13035== Reading debug info from /lib/libm-2.3.2.so...
==13035== ... CRC mismatch (computed 25C06BA7 wanted 42E315F7)
==13035== object doesn't have any debug info
==13035== Reading syms from /lib/libgcc_s.so.1 (0x1BA07000)
==13035== object doesn't have a symbol table
==13035== object doesn't have any debug info
==13035== Reading syms from /lib/libc-2.3.2.so (0x1BA11000)
==13035== object doesn't have a symbol table
==13035== Reading debug info from /lib/libc-2.3.2.so...
==13035== ... CRC mismatch (computed 76EC50B6 wanted 52619D67)
==13035== object doesn't have any debug info
==13035== Reading syms from /lib/libdl-2.3.2.so (0x1BB45000)
==13035== object doesn't have a symbol table
==13035== Reading debug info from /lib/libdl-2.3.2.so...
==13035== ... CRC mismatch (computed 6F61513E wanted 280D08E5)
==13035== object doesn't have any debug info
==13035== TRANSLATE: 0x1BA86D10 redirected to 0x1B9057B0
==13035== TRANSLATE: 0x1B9BB2D0 redirected to 0x1B90469A
==13035== TRANSLATE: 0x1B9B9D00 redirected to 0x1B904C26
==13035== TRANSLATE: 0x1BA820E0 redirected to 0x1B904A82
==13035==
==13035== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 19 from 1)
--13035--
--13035-- supp: 19 Ugly strchr error in /lib/ld-2.3.2.so
==13035== malloc/free: in use at exit: 960 bytes in 1 blocks.
==13035== malloc/free: 2 allocs, 1 frees, 964 bytes allocated.
==13035==
==13035== searching for pointers to 1 not-freed blocks.
==13035== checked 201664 bytes.
==13035==
==13035== LEAK SUMMARY:
==13035== definitely lost: 0 bytes in 0 blocks.
==13035== possibly lost: 0 bytes in 0 blocks.
==13035== still reachable: 960 bytes in 1 blocks.
==13035== suppressed: 0 bytes in 0 blocks.
--13035-- TT/TC: 0 tc sectors discarded.
--13035-- 3338 tt_fast misses.
--13035-- translate: new 3313 (53693 -> 726807; ratio 135:10)
--13035-- discard 0 (0 -> 0; ratio 0:10).
--13035-- chainings: 1994 chainings, 0 unchainings.
--13035-- dispatch: 308598 jumps (bb entries); of them 23262 (7%) unchained.
--13035-- 7/3488 major/minor sched events.
--13035-- reg-alloc: 579 t-req-spill, 129726+4343 orig+spill uis,
--13035-- 16967 total-reg-rank
--13035-- sanity: 8 cheap, 1 expensive checks.
--13035-- ccalls: 12722 C calls, 55% saves+restores avoided (41462 bytes)
--13035-- 17151 args, avg 0.86 setup instrs each (4494 bytes)
--13035-- 0% clear the stack (38166 bytes)
--13035-- 5124 retvals, 30% of reg-reg movs avoided (3070 bytes)

Compiliert wurde das Programm mit folgender Zeile:

g++ -g -Wall -pedantic helloworld.cpp -o helloworld

Warum fehlen ihm dann die debug Informationen und wo werden in dem kleinen Programm 960 Byte Speicher belegt?

Aqua

Edit: Die 960 Byte werden unabhaengig von der Anzahl der Objekte belegt, die ich erstelle. Ist das irgendeine Verwaltungsinfo, auf die ich keinen Zugriff habe?

Trap
2005-11-04, 18:30:29
Lies doch die Meldung mal genau:
-die Debuginformationen zu deinem Programm werden gefunden und geladen
-die 960 Byte werden zwar nicht freigegeben, sind aber auch kein leak (still reachable)

Wozu die 960 Byte benutzt werden weiß ich nicht. Vielleicht Puffer für Ein- und Ausgabe, vielleicht Verwaltungsdaten...

Aqualon
2005-11-04, 19:12:45
Sorry, kenn mich damit noch nicht so genau aus. Bin auch ein wenig mit den ganzen CRC mismatches durcheinandergekommen.

Also die Sachen die still reachable sind, stellen kein Problem dar, nur die Sachen die valgrind als lost kennzeichnet sind problematisch?

Aqua

Trap
2005-11-04, 20:09:15
http://valgrind.org/docs/FAQ/ Punkt 4.2

Aqualon
2005-11-04, 23:24:51
Danke sehr :)

Aqua