c++ - 程序参数超过一定阈值时出现段错误

标签 c++ segmentation-fault

我正在用 C++ 编写这个相当大的网络模拟器。我在开发它们时一直在定期测试各个部分,在将所有内容放在一起之后,只要我对模拟器施加的负载不太大(它是一个 P2P 内容分发模拟器,所以越不同“内容”我介绍了模拟器必须处理的更多数据传输)。任何超过被模拟的不同内容数量的特定阈值的任何东西都会在几分钟的平稳运行后导致突然的 SIGSEGV。我假设存在内存泄漏,最终变得太大并把事情搞砸了,但是参数低于阈值的 valgrind 运行完美终止。但是,如果我尝试使用 valgrind 运行程序,使用内容编号的临界值,在某个点之后,我开始在以前没有问题的函数中出现内存访问错误:

==5987== Invalid read of size 8
==5987==    at 0x40524E: Scheduler::advanceClock() (Scheduler.cpp:38)
==5987==    by 0x45BA73: TestRun::execute() (TestRun.cpp:73)
==5987==    by 0x45522B: main (CDSim.cpp:131)
==5987==  Address 0x2e63bc70 is 0 bytes inside a block of size 32 free'd
==5987==    at 0x4C2A4BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5987==    by 0x405487: Scheduler::advanceClock() (Scheduler.cpp:69)
==5987==    by 0x45BA73: TestRun::execute() (TestRun.cpp:73)
==5987==    by 0x45522B: main (CDSim.cpp:131)
==5987==
==5987== Invalid read of size 4
==5987==    at 0x40584E: Request::getSimTime() const (Event.hpp:45)
==5987==    by 0x40525C: Scheduler::advanceClock() (Scheduler.cpp:38)
==5987==    by 0x45BA73: TestRun::execute() (TestRun.cpp:73)
==5987==    by 0x45522B: main (CDSim.cpp:131)
==5987==  Address 0x2e63bc78 is 8 bytes inside a block of size 32 free'd
==5987==    at 0x4C2A4BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5987==    by 0x405487: Scheduler::advanceClock() (Scheduler.cpp:69)
==5987==    by 0x45BA73: TestRun::execute() (TestRun.cpp:73)
==5987==    by 0x45522B: main (CDSim.cpp:131)
==5987==

我知道在没有看到完整代码的情况下可能很难给出答案,但是是否有关于这里可能发生的事情的“高级”提示?我不明白为什么一个看似正常工作的功能突然开始出现异常。有什么明显的东西是我可能遗漏的吗?

之前的 valgrind 日志中涉及的行是 if (nextEvent->getSimTime() < this->getSimTime())在以下 block 中:

bool Scheduler::advanceClock() {
  if (pendingEvents.size() == 0) {
    std::cerr << "WARNING: Scheduler::advanceClock() - Empty event queue before "
        "reaching the termination event" << std::endl;
    return false;
  }
  const Event* nextEvent = pendingEvents.top();
  // Check that the event is not scheduled in the past
  if (nextEvent->getSimTime() < this->getSimTime()) {
    std::cerr << "Scheduler::advanceClock() - Event scheduled in the past!" << 
        std::endl;
    std::cerr << "Simulation time: " << this->getSimTime()
        << ", event time: " << nextEvent->getSimTime()
        << std::endl;
    exit(ERR_EVENT_IN_THE_PAST);
  }
  // Update the clock with the current event time (>= previous time)
  this->setSimTime(nextEvent->getSimTime());
  ...

其中 pendingEvents 是一个 boost::heap::binomial_heap。

最佳答案

我终于找到问题所在了。当事件完成并且需要将其从列表中删除时,我的代码是这样的:

...
// Data transfer completed, remove event from queue
// Notify the oracle, which will update the cache mapping and free resources
// in the topology
oracle->notifyCompletedFlow(nextEvent, this);
// Remove flow from top of the queue
pendingEvents.pop();
handleMap.erase(nextEvent);
delete nextEvent;
return true;

问题是 oracle->notifyCompletedFlow() 调用了调度器上的一些方法来动态更新调度事件的优先级(例如,对网络中可用带宽的变化使用react),因此,在某些情况下,当我使用 pendingEvents.pop() 删除队列顶部时,我弹出了一个不同的事件并将已删除的 nextEvent 留在了那里。通过在调用 oracle 之前弹出队列,问题自行解决。

我很抱歉遗漏了一些可能会导致更快回答的代码,我会尝试从我的错误中吸取教训 :) 感谢您为我指明了正确的方向。

关于c++ - 程序参数超过一定阈值时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11210535/

相关文章:

c++ - 是否保证在调用其成员函数之前初始化对象?

c++ - 与 CMake 2.8 一起安装 CMake 3.3 的具体细节是什么?

c++ - 重载运算符中的类型不匹配(编写管道)

c++ - C/C++ ASCII 字符到 HEX BYTE 数组

c++ - double 的 printf 舍入行为

c++ - 启用优化时出现 g++ 段错误

c - 实现广度优先搜索时的错误 (C)

c - C 中函数的指针返回

调用isalpha导致段错误

c++ - 无法解决 Segmentation Fault 核心转储错误