我正在用 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())
以下块中:
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。