C++指针地址突然改变[关闭]

问题描述 投票:0回答:0

这次重新询问了一些源代码。

我有一个看似随机更改指针内存地址的代码库。 所有的指针。包含代码的类的示例伪代码片段:

&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 上运行。

问题:

  1. 导致这种看似随机的指针行为的 SDK 调用中可能有什么行为?
  2. 那些非常低的内存地址有什么意义?那是某种特殊的内存区域吗?
  3. 任何事情(内存损坏、机器目标(核心 2)与运行时不同、错误代码、编译器错误等)表现出的什么行为会导致此类问题?
由于保密协议,我不能提供太多代码,但基本上是这样的:

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 (...) { } }
    
c++ pointers smart-pointers
© www.soinside.com 2019 - 2024. All rights reserved.