mercredi 19 avril 2017

Memory management for 2 "interconnected" C++ classes

I have a class Branch and a class Tree. The declaration of the classes looks like:

class Branch{


  map<string, double> properties;

  vector<Branch *> parent;
  vector<Branch *> children;

  int hierarchy;



class Tree{


  /* Tree is a vector of pointers to branches. */
  vector<Branch*> tree_branches;

  map<int,int> Hierarchy_distribution;



1) If I understood well, the fact that the only attributes of Tree class are a vector and a map, makes it unnecessary to declare a destructor, a copy-assignement operator and a copy constructor, because the memory is managed "inside" the vector and map templates.

2) I use this classes from a python code (I used cython to interface between C++ an python) and all the operations I make are performed through a tree object. I thought that because the branches I "use" are contained in a tree object (with a good memory management) I didn't need to declare a destructor, a copy constructor,and a copy assignment operator for the branch class. However I'm experiencing some problems and I think I have a memory leak.

Can someone confirm me if this could be causing a memory leak? If it is would stocking the int hierarchy inside a vector<int> avoid declaring a destructor and company?


0 commentaires:

Enregistrer un commentaire