We need the simplest option on the reference counters for the following class tree:

struct AST { string val; vector<AST*> nest; void push(AST*); // nested tree elements virtual string dump(int depth=0); string pad(int); // dump tree }; struct Num:AST { Num(string); double val; string dump(int); }; struct Str:AST { Str(string); }; struct Op:AST { Op(string); }; struct Vector:AST { Vector(); } 

The problem is trying to implement a calculation on the tree:

 AST glob("environment","global"); AST* AST::eval(AST*E=&glob) { for (it = nest.begin(), e=nest.end(); it!=e ; it++) (*it) = (*it)->eval(E); // вот здесь водопадная утечка памяти return this; } 

Not sure that shared_ptr is able to work with global data structures - the parser builds a global tree or graph in memory, then the handler runs on it (or somewhat in parallel, but this is in perspective), which this graph rebuilds, indexes, or builds a new part of the network bypassing existing related items. This is something from the field of implementation of semantic networks or Minsky frames , for a start - just an interpreter on trees with calculated attributes.

  • 2
    And std :: shared_ptr and std :: weak_ptr will not work? - Unick
  • I can offer two options for "surveillance" (the simplest case). 1. refcount - when the object is not used, you lower the refcount, if enabled, increase. When refcount is 0 or -1, it clears the object. 2. you put a timestamp that you update if the object is reused, and under certain conditions (for example, if the number of objects> 100) delete the oldest one. in this case, you can even make an array of pre-initialized objects and not do new every time. - nick_n_a

0