mercredi 29 mai 2019

Does my polymorphism process is causing memory leak

I have a TreeItem class which has a data member with the name Container

class TreeItem
{
 public:    
 void setContainer(const Container &data);

 private:
    QList<TreeItem*> childItems;
    Container itemData;
     TreeItem* parentItem;
 };

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Defination for Set Container

void TreeItem::setContainer(const Container &cont)
 {
   itemData = cont; 
 }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Defination for Container Class

class Container
 {
  private:
  std::string stdstrContainerName;
  std::string stdstrPluginType;
  Geometry* Geom;

public:
 Container();
 Container(std::string, std::string, Geometry* geometry );
 Container(const  Container& obj);
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Container::Container( std::string strName, std::string strType,  
Geometry* geometry) : Geom(geometry)
{   
  stdstrContainerName = strName;
  stdstrPluginType = strType;
  qDebug() << geometry->isValid();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// I use my TreeModel class to set the value of Container in the TreeItem object

void  TreeModel::SetContainer(const QModelIndex &index,  Container* Cont)
 {
   TreeItem *item = getItem(index);
   item->setContainer(*Cont);

  }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// The geometry which is data type to Container class has a virtual function with the name draw

Class Geometry
{
 public:    
     std::string stdstrType;
     bool bValid;

 public:
// Other Functions
 virtual void draw();

}; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// There are other clases which are derived from the Geometry class

 class Sph : public Geometry
  {
   public:
   Sph( float radius , float segments );
  ~Sph();
  void init();
  void CleanUp();
  void draw();

 private:
   float fRadius, fSegments;
   bool isInited;
   unsigned int m_VAO, m_VBO;
   int iNumsToDraw;
   SumShader shader;
   bool isChanged;
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// This is the process i am using to set the Container to TreeItem class.

 void WavefrontRenderer::AddSphere()
  {
    QModelIndex index = treeView->selectionModel()->currentIndex();
    QAbstractItemModel *model = treeView->model();
    TreeModel *myModel = qobject_cast<TreeModel*>(model);

    Sph* sphere = new Sph( 0.1 , 32);
   sphere->setValidState(true);
   sphere->stdstrType = "Sphere";
   if (!model->insertRow(index.row() + 1, index.parent()))
      return;

   Container cont("Sphere" , "SPHERE" , sphere );
   QModelIndex child = model->index(index.row() + 1, 0, index.parent());
   model->setData(child, QVariant("Container"), Qt::EditRole);
   myModel->SetContainer(child, &cont);
   delete sphere // This line gives error

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Everthing works as expected but i have few questions.

1) Since i am creating a new Object "Sph* sphere = new Sph( 0.1 , 32);" but not deleting it does it cause memory leakage.

2) When i tried to delete it after setting it on TreeItem i got Memory error.

Aucun commentaire:

Enregistrer un commentaire