Ignore:
Timestamp:
12/07/21 12:00:23 (3 years ago)
Author:
ymipsl
Message:
  • Solve memory leak from remapper.
  • shared_ptr add add for manage nodes.

YM

File:
1 edited

Legend:

Unmodified
Added
Removed
  • XIOS/dev/dev_ym/XIOS_COUPLING/extern/remap/src/node.cpp

    r2198 r2269  
    7979/* On level `level` find the node in our subtree that is closest to `src` and return through argument `closest`. 
    8080   The return value is just for recursive calling */ 
    81 void Node::findClosest(int level, NodePtr src, double& minDist2, NodePtr &closest) 
     81void Node::findClosest(int level, NodePtr src, double& minDist2, NodePtr& closest) 
    8282{ 
    8383        double r2; 
     
    8989                { 
    9090                        minDist2 = r2; 
    91                         closest = this; 
     91                        closest = shared_from_this(); 
    9292                } 
    9393        } 
     
    221221}*/ 
    222222 
    223 bool find_in_tree1(Node* node) 
     223bool find_in_tree1(NodePtr node) 
    224224{ 
    225225        if (node == node->tree->root) return true; 
    226         if (node->parent == NULL) 
     226        if (node->parent.lock() == NULL) 
    227227        { 
    228228                cerr << "Cannot find!" << endl; 
    229229                return false; 
    230230        } 
    231         return find_in_tree1(node->parent); 
     231        return find_in_tree1(node->parent.lock()); 
    232232} 
    233233 
     
    264264        { 
    265265    node->child.push_back(thIs); 
    266                 thIs->parent = node; 
     266                thIs->parent = node ; 
    267267                if (node->child.size() > MAX_NODE_SZ &&  node->tree->canSplit() ) // with us as additional child `node` is now too large :( 
    268                         return (node->reinserted || node->parent == NULL) ? split(node) : reinsert(node); 
     268                        return (node->reinserted || node->parent.lock() == NULL) ? split(node) : reinsert(node); 
    269269        } 
    270270        else // la < lb - 1 
     
    299299   
    300300        /* transfere out children from us to a new node q which will be returned */ 
    301         NodePtr q = new Node; 
     301        NodePtr q = make_shared<Node>(); 
    302302        q->tree = thIs->tree; 
    303303        q->child.resize(out); 
     
    462462{ 
    463463        thIs->tree->increaseLevelSize(thIs->level); 
    464         NodePtr p = new Node; 
    465         NodePtr q = new Node; 
     464        NodePtr p = make_shared<Node>(); 
     465        NodePtr q = make_shared<Node>(); 
    466466        p->level = q->level = thIs->level; 
    467467        p->reinserted = q->reinserted = false; 
    468         p->parent = q->parent = thIs->parent; 
     468        p->parent = thIs->parent; 
     469        q->parent = thIs->parent; 
    469470        p->tree = q->tree = thIs->tree; 
    470471 
     
    476477        std::sort(thIs->child.begin(), thIs->child.end(), compareDist); 
    477478        for (int i = 0; i < MAX_NODE_SZ+1; i++) 
    478                 assert(thIs->child[i]->parent == thIs); 
     479                assert(thIs->child[i]->parent.lock() == thIs); 
    479480        for (int i = 0; i < MAX_NODE_SZ/2 + 1; i++) 
    480481                q->child[i] = thIs->child[i]; 
     
    496497        thIs->reinserted = p->reinserted; 
    497498        thIs->update(); 
    498         delete p; 
     499        p.reset(); 
    499500 
    500501        if (thIs == thIs->tree->root) // root split 
     
    593594   
    594595  for (int i = 0; i < childSize; i++) 
    595                 assert(child[i]->parent == this); 
     596                assert(child[i]->parent.lock() == shared_from_this()); 
    596597 
    597598  if (level>0) for (int i = 0; i < childSize; i++) child[i]->checkParent() ; 
     
    602603        cout << "level " << this->level << ", centre "; 
    603604        cout << "level " << this->level << ", centre " << this->centre << "\t r = " << this->radius << endl; 
    604         cout << this << " p: " << this->parent << endl; 
     605        cout << this << " p: " << this->parent.lock() << endl; 
    605606        int n = this->child.size(); 
    606607        for (int i=0; i<n; i++) 
     
    656657        double distMin2 = 0; // squared distance 
    657658        closest = NULL; 
    658         if (tree->root == this) 
     659        if (tree->root == shared_from_this()) 
    659660                findClosest(level, node, distMin2, closest); 
    660661 
    661         if (closest != NULL && tree->root == this) 
     662        if (closest != NULL && tree->root == shared_from_this()) 
    662663                /* When is this point reached? 
    663664                   if the preceeding findClosest was called and succesed to set closest 
     
    690691} 
    691692 
    692 void Node::routingIntersecting(vector<Node> *routingList, NodePtr node) 
     693void Node::routingIntersecting(vector<NodePtr> *routingList, NodePtr node) 
    693694{ 
    694695        if (level==0) 
    695696        { 
    696697                int rank = route; 
    697                 routingList[rank].push_back(*node); 
     698                routingList[rank].push_back(node); 
    698699        } 
    699700        else 
     
    712713                child[i]->free_descendants(); 
    713714                if (child[i]->level) // do not attempt to delete leafs, they are delete through leafs vector 
    714                         delete child[i]; 
     715                        child[i].reset(); 
    715716        } 
    716717} 
     
    718719void Node::getNodeLevel(int assignLevel, std::list<NodePtr>& NodeList) 
    719720{ 
    720   if (level==assignLevel) NodeList.push_back(this) ; 
     721  if (level==assignLevel) NodeList.push_back(shared_from_this()) ; 
    721722  else if (level>0) for (int i = 0; i < child.size(); i++) child[i]->getNodeLevel(assignLevel,NodeList) ; 
    722723  return ; 
     
    737738        for (int j = 0; j < child[i]->child.size(); j++) tree->push_back(child[i]->child[j]) ; 
    738739        tree->decreaseLevelSize(assignLevel) ; 
    739         delete child[i] ; 
     740        child[i].reset() ; 
    740741      } 
    741742      else newChild.push_back(child[i]) ; 
Note: See TracChangeset for help on using the changeset viewer.