Commit 6705777f authored by Axel Kohlmeyer's avatar Axel Kohlmeyer
Browse files

fix memory issue in POEMS library, where auxiliary data was cast to a data...

fix memory issue in POEMS library, where auxiliary data was cast to a data structure of different (smaller) size when deleting.

this commit adds support to a callback function that allows to delete aux data in the Tree with its native size.
parent e3ddefb3
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -40,7 +40,9 @@ struct POEMSNode {
class SystemProcessor{
private:
	Tree nodes;
//	List<POEMSNode> forDeletion;
	static void POEMSNodeDelete_cb(void *node) {
		delete (POEMSNode *) node;
	}
	List<POEMSChain> headsOfSystems;
	List<List<int> > ringsInSystem;
	POEMSNode * findSingleLink(TreeNode * aNode);
@@ -65,6 +67,8 @@ public:
};

SystemProcessor::SystemProcessor(void){
  // register callback for deleting auxiliary data from tree nodes.
  nodes.SetDeleteAuxData(&POEMSNodeDelete_cb);
}

void SystemProcessor::processArray(int** links, int numLinks)
+24 −9
Original line number Diff line number Diff line
@@ -40,6 +40,9 @@ protected:
	// used by the copy constructor and assignment operator
	TreeNode *CopyTree(TreeNode *t);

    // callback function to delete aux data
    void (*DeleteAuxData)(void *);

	// used by insert and delete method to re-establish
	// the avl conditions after a node is added or deleted
	// from a subtree
@@ -72,7 +75,13 @@ public:

	// standard list handling methods
	void * Find(int& item);
	void * GetAuxData(int item) { return (void *)(FindNode(item, root)->GetAuxData());}
	void * GetAuxData(int item) {
      return (void *)(FindNode(item, root)->GetAuxData());
    }
    void SetDeleteAuxData(void (*callback)(void *)) {
      DeleteAuxData = callback;
    }

	void Insert(const int& item, const int& data, void * AuxData = NULL);
	void Delete(const int& item);
	void AVLInsert(TreeNode* &tree, TreeNode* newNode, int &reviseBalanceFactor);	
@@ -90,6 +99,7 @@ Tree::Tree(void)
	root = 0; 
	current = 0; 
	size = 0;
    DeleteAuxData = NULL;
}


@@ -569,12 +579,17 @@ TreeNode *Tree::CopyTree(TreeNode *t)
// the tree and delete each node as the vist operation
void Tree::DeleteTree(TreeNode *t)
{
	if (t != NULL)
	{
  if (t != NULL) {
    DeleteTree(t->Left());
    DeleteTree(t->Right());
		if (t->GetAuxData() != NULL)
                    delete (TreeNode *) t->GetAuxData();
    void *aux = t->GetAuxData();
    if (aux != NULL) {
      if (DeleteAuxData != NULL) {
        (*DeleteAuxData)(aux);
      } else {
        delete (TreeNode *) aux;
      }
    }
    FreeTreeNode(t);
  }
}