000
01.08.2009, 12:05 Uhr
deer
|
Ich habe eine polymorphe Klassenhierarchie. Durch den Gebrauch von valgrind habe ich entdeckt, dass ich durch (1) ein Memoryleak kriege.
Ich habe das Gefühl, dass ich etwas Grundlegendes übersehen habe!? Any ideas?
Vielen Dank!
C++: |
#include <boost/shared_ptr.hpp> #include <unordered_map> #include <string> #include <stdio.h> using std::string; using boost::shared_ptr;
class Symbol { public: shared_ptr<Symbol> type; string name; Symbol() {} Symbol(string s, shared_ptr< Symbol > t) : name(s), type(t) {} virtual ~Symbol() { } };
class VarSym: public Symbol { public: VarSym(string s, shared_ptr< Symbol > type) :Symbol(s, type) {} }; typedef std::unordered_map<string, shared_ptr<Symbol>> symMap; typedef shared_ptr<Symbol> SymPtr;
class Scope { public: Scope *parent; Scope() : parent(0){} Scope(Scope* p) : parent(p) {} virtual ~Scope() {} virtual void define(string name, shared_ptr<Symbol> s) = 0; };
class SymbolWithScope : public Symbol , public Scope{ public: symMap symbols;
SymbolWithScope(Scope* p) : Scope(p) {} SymbolWithScope(string s, shared_ptr< Symbol > type) : Symbol(s, type) {} virtual ~SymbolWithScope() { symbols.clear(); } virtual void define(string name, shared_ptr<Symbol> s) { symbols.insert(std::make_pair(name, s)); } };
class LocalScope : public SymbolWithScope { public: LocalScope(Scope* p) : SymbolWithScope(p) {} ~LocalScope() {} };
// global scope Scope *scp = new LocalScope(0);
template <typename T> void storeNewSymbol(string ident, shared_ptr<T> symbol) { scp->define(ident, symbol); }
int main() { printf("scp: %p\n", scp); string identifier("ident1"); shared_ptr<SymbolWithScope> currentScope(new LocalScope(scp)); storeNewSymbol(identifier, currentScope); scp = currentScope.get(); // (1) mem. leak delete scp; }
|
|