简体   繁体   English

模拟 Leach 遇到了问题。 以 omnet++ 中的错误结束

[英]Simulating Leach has encounter a problem. Finished with error in omnet++

When running the simulation in omnet++ 5.7 the execution stops suddenly and closes.在 omnet++ 5.7 中运行模拟时,执行突然停止并关闭。 This is the code that is being run in omnet这是在 omnet 中运行的代码

auto simulation = getSimulation();
for (i = 1; i <= simulation->getLastComponentId(); i++) {

    int x, y, id;

    //scan the simulation module vector
    mod = (cModule*)simulation->getModule(i);

    if (strcmp(mod->getName(), "node") == 0) {
        id = ((Node*)mod)->myId;
        x = ((Node*)mod)->xpos;
        y = ((Node*)mod)->ypos;
        nodePtr[id] = ((Node*)mod);

        if (id != this->myId) {
            cGate* g;
            char gName1[32], gName2[32];

            // make new gate here
            if (this->hasGate(gName1)) {
                this->gate(gName1)->disconnect();
                this->deleteGate(gName1);
            }

            this->addGate(gName1, cGate::OUTPUT, false);

            // make new gate at other side
            if (mod->hasGate(gName2)) {
                mod->gate(gName2)->disconnect();
                mod->deleteGate(gName2);
            }

            mod->addGate(gName2, omnetpp::cGate::INPUT, false);

            //CHANNEL
            cIdealChannel* ch = NULL;
            this->gate(gName1)->connectTo(mod->gate(gName2), ch);
            g = this->gate(gName1);
            g->setDisplayString(g->getDisplayString());
        }
    }
}

I assume that the last line g->setDisplayString(g->getDisplayString());我假设最后一行g->setDisplayString(g->getDisplayString()); is probably where the code breaks.可能是代码中断的地方。 The code repeats in the for loop with i<= simulation->getLastComponentId() .代码在 for 循环中使用i<= simulation->getLastComponentId()重复。 I'm new to Omnet++.我是 Omnet++ 的新手。 Any suggestion to fix this would be helpful.任何解决此问题的建议都会有所帮助。

Thanks.谢谢。

Several things in your code may be source of crashing:代码中的几件事可能是崩溃的根源:

  1. getModule(i) may return nullptr , see OMNeT++ Simulation API , so you should check in the code whether result is not nullptr . getModule(i)可能会返回nullptr ,请参阅OMNeT++ Simulation API ,因此您应该检查代码是否结果不是nullptr
  2. gName1 and gName2 are not set! gName1gName2未设置!

Other issues:其他事宜:

  • instead of (Node*)mod use dynamic_cast<Node*)>(mod) and check whether results is not nullptr .而不是(Node*)mod使用dynamic_cast<Node*)>(mod)并检查结果是否不是nullptr
  • instead of strcmp(mod->getName(), "node") == 0 I advice using mod->isName("node") - see OMNeT++ Simulation API而不是strcmp(mod->getName(), "node") == 0我建议使用mod->isName("node") - 请参阅OMNeT++ Simulation API
  • if you want to obtain a module whose name is "node", you do not need to manually check the name of every module - there is a useful method getModuleByPath() see OMNeT++ Simulation Manual如果你想获取一个名为“node”的模块,你不需要手动检查每个模块的名称 - 有一个有用的方法getModuleByPath()参见OMNeT++ Simulation Manual

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM