NegatIV
NegatIV

Reputation: 47

shared_ptr is empty out of function

I try to dynamically create graphs which can have unreachable nodes. These nodes will be converted as new graphs on creation. Then I save all graphs in a map of <string,shared_ptr>.

A MaterialArch is a graph

A MatArchNode is a node

Creation of the main graph :

void Console::newMaterial(std::string name, int size){
    if(!isVariable(name)){
        if(!inMaterial(name)){
            std::shared_ptr<MaterialArch> mat = std::make_shared<MaterialArch>(m_matPop.get());
            mat->generate(size);
            m_matPop->add(name,mat);
        }
    }
    else{
        print("Error : Material name already used");
    }
}

The generate function where unreachable nodes are converted to graph :

void MaterialArch::generate(int nbNodes){
    std::vector<std::shared_ptr<MatArchNode>> nodeList;
    if(m_root == nullptr){
        m_root = std::make_shared<MatArchNode>();
        m_root->generate();
        nodeList.push_back(m_root);
        for (int i = 1; i < nbNodes; ++i) {
            std::shared_ptr<MatArchNode> nextNode = std::make_shared<MatArchNode>();
            nextNode->generate();
            nodeList.push_back(nextNode);
        }
        if(nodeList.size() < 2){
            return;
        }
        for (auto node : nodeList) {
            //linking
        }
    }
    std::vector<std::shared_ptr<MatArchNode>> newRoots;
    for (auto node : nodeList) {
        bool unreachable = true;
        if(!node->orpheline(m_root)){
                unreachable = false;
        }
        for (auto root : newRoots) {
            if(!node->orpheline(root)){
                unreachable = false;
            }
        }
        if(unreachable){
            std::cout << "@ORPH" << std::endl;
            std::shared_ptr<MaterialArch> orph = std::make_shared<MaterialArch>(m_matPop.get());
            orph->setRoot(node);
            m_pop->add(m_pop->orphName(),orph);
            newRoots.push_back(node);
        }
    }
}

The function where I put the graph in the map :

void MaterialPop::add(std::string name, std::shared_ptr<MaterialArch> mat){
    std::cout << "ADD : "<<name << std::endl;
    bool alreadyExist = false;
    for (auto var : m_varMaterials) {
        if(var.second == mat){
            alreadyExist = true;
        }
    }
    if(!alreadyExist){
        m_console->addMatName(name);
        m_varMaterials.emplace(name,std::move(mat)); 
        m_views.emplace(name, std::make_shared<MaterialArchView>(m_varMaterials.at(name)));
        m_visibility.emplace(name,true);
    }
}

My problem is as soon as I get out of generate(), every orph shared_ptr is empty. I thought it was because I try to access a deleted object but the main graph is created the same way, in a function, and is not empty out of it.

Thanks in advance for helping

Upvotes: 0

Views: 72

Answers (1)

NegatIV
NegatIV

Reputation: 47

Sorry I'm so bad, my setRoot() looked like :

if(!orpheline(node)){
   m_root = node;
}

so the root wasn't set because node was always unreachable

Upvotes: 1

Related Questions