这次重新询问了一些源代码。
我有一个看似随机更改指针内存地址的代码库。 所有的指针。包含代码的类的示例伪代码片段:
&ptrA => 0xe4cfc8bc // Pointer relevant to the program
&ptrB => 0xe4cfc8dc // Pointer relevant to the program
&ptrC => 0xe4cfc8e8 // Random pointer just to see if this bug affects all pointers
3RD PARTY SDK CALL (supposedly a "read/getter", meaning it shouldn't be writing to disk or anything)
&ptrA => 0xe4cfc8bc
&ptrB => 0xe4cfc8dc
&ptrC => 0xe4cfc8e8
/*
Further down in the code, where NOTHING has referenced those pointers whatsoever
*/
&ptrA => 0xdc
&ptrB => 0xfc
&ptrC => 0x110
我已经确定调用第 3 方 SDK 是导致此行为的原因,因为注释掉该调用会导致其余代码按预期运行,并且指针永远不会失去其价值。
需要注意的重要一点是,新的内存值不会在 SDK 调用后立即发生。一段时间后。据我所知,SDK 调用不会产生新的线程或进程;不过我可能是错的。
SDK 很有可能是为 core2 机器编译的(编译器标志
-mcpu_type=core2 -mcpu_tune=core2
)。该代码在第 12 代 i3/i5 cpu 上运行。
问题:
App.h:
class MyApp {
public:
void Execute();
void PrintAddresses();
void Foo();
void Bar();
private:
TComPtr<Third_Party_TypeA> ptrA; // smart pointer
TComPtr<Third_Party_TypeB> ptrB; // smart pointer
char* ptrC = new char[25]; // not a smart pointer
}
App.cpp:
void MyApp::PrintAddresses() {
// print the following snprintf string:
(" ptrA:%p\n\r ptrB:%p\n\r ptrC:%p", &ptrA, &ptrB, &ptrC)
}
void MyApp::Execute() {
try {
// Setup things
PrintAddresses(); // normal
int rtn = SDKGetterCall();
PrintAddresses(); // normal
Foo(); // does not use any pointers
Bar(); // does not use any pointers
PrintAddresses(); // BROKEN
} catch (...) {
}
}