上一篇是 https://v2ex.com/t/941007
结构体定义:
struct ModuleConfig
{
ModuleConfig()
{
printf("ModuleConfig::constructor\n");
}
~ModuleConfig()
{
printf("ModuleConfig::destructor\n");
}
uint32_t identity;
std::string pdoMapName;
uint32_t pdoMapInOffset;
uint32_t pdoMapOutOffset;
};
调用并崩溃的代码:
void ESI_SetModuleIdentities(int slaveId, std::vector<uint32_t>& moduleIdentities)
{
ModuleConfig* newModule = new ModuleConfig;
printf("line: %d\n", __LINE__); // 917
newModule->identity = 243423;
printf("EEEEE %d\n", newModule->identity);
delete newModule;
printf("line: %d\n", __LINE__); // 921
newModule = NULL;
printf("line: %d\n", __LINE__); // 923
SlaveFileConfig* config = database[slaveId];
config->SetModuleIdentities(moduleIdentities);
}
直接运行后的打印输出:
ModuleConfig::constructor
line: 917
EEEEE 243423
ModuleConfig::destructor
(只有这么多,打完这些就崩溃)
使用 valgrind 调试,相关信息如下:
ModuleConfig::constructor
line: 917
EEEEE 243423
ModuleConfig::destructor
==8820== Conditional jump or move depends on uninitialised value(s)
==8820== at 0x49D65CE: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==8820== by 0x170615: ModuleConfig::~ModuleConfig() (SlaveConfigParser.h:26)
==8820== by 0x17F992: ESI_SetModuleIdentities(int, std::vector<unsigned int, std::allocator<unsigned int> >&) (SlaveConfigParser.cpp:920)
==8820== by 0x161637: EcatAdapter::InitSlaveFileConfig() (EcatAdapter.cpp:1321)
==8820== by 0x15F583: EcatAdapter::ConfigSlaves() (EcatAdapter.cpp:762)
==8820== by 0x15DB78: EcatAdapter::Connect() (EcatAdapter.cpp:247)
==8820== by 0x15C4D4: AdapterManager::Connect() (AdapterManager.cpp:250)
==8820== by 0x15C0F6: AdapterManager::Run() (AdapterManager.cpp:151)
==8820== by 0x15CA7F: StartAdapter (CExport.cpp:22)
==8820== by 0x18F8CA: main (main.c:5)
==8820== Uninitialised value was created by a heap allocation
==8820== at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==8820== by 0x17F939: ESI_SetModuleIdentities(int, std::vector<unsigned int, std::allocator<unsigned int> >&) (SlaveConfigParser.cpp:916)
==8820== by 0x161637: EcatAdapter::InitSlaveFileConfig() (EcatAdapter.cpp:1321)
==8820== by 0x15F583: EcatAdapter::ConfigSlaves() (EcatAdapter.cpp:762)
==8820== by 0x15DB78: EcatAdapter::Connect() (EcatAdapter.cpp:247)
==8820== by 0x15C4D4: AdapterManager::Connect() (AdapterManager.cpp:250)
==8820== by 0x15C0F6: AdapterManager::Run() (AdapterManager.cpp:151)
==8820== by 0x15CA7F: StartAdapter (CExport.cpp:22)
==8820== by 0x18F8CA: main (main.c:5)
==8820==
==8820== Conditional jump or move depends on uninitialised value(s)
==8820== at 0x483CF75: operator delete(void*) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==8820== by 0x170615: ModuleConfig::~ModuleConfig() (SlaveConfigParser.h:26)
==8820== by 0x17F992: ESI_SetModuleIdentities(int, std::vector<unsigned int, std::allocator<unsigned int> >&) (SlaveConfigParser.cpp:920)
==8820== by 0x161637: EcatAdapter::InitSlaveFileConfig() (EcatAdapter.cpp:1321)
==8820== by 0x15F583: EcatAdapter::ConfigSlaves() (EcatAdapter.cpp:762)
==8820== by 0x15DB78: EcatAdapter::Connect() (EcatAdapter.cpp:247)
==8820== by 0x15C4D4: AdapterManager::Connect() (AdapterManager.cpp:250)
==8820== by 0x15C0F6: AdapterManager::Run() (AdapterManager.cpp:151)
==8820== by 0x15CA7F: StartAdapter (CExport.cpp:22)
==8820== by 0x18F8CA: main (main.c:5)
==8820== Uninitialised value was created by a heap allocation
==8820== at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==8820== by 0x17F939: ESI_SetModuleIdentities(int, std::vector<unsigned int, std::allocator<unsigned int> >&) (SlaveConfigParser.cpp:916)
==8820== by 0x161637: EcatAdapter::InitSlaveFileConfig() (EcatAdapter.cpp:1321)
==8820== by 0x15F583: EcatAdapter::ConfigSlaves() (EcatAdapter.cpp:762)
==8820== by 0x15DB78: EcatAdapter::Connect() (EcatAdapter.cpp:247)
==8820== by 0x15C4D4: AdapterManager::Connect() (AdapterManager.cpp:250)
==8820== by 0x15C0F6: AdapterManager::Run() (AdapterManager.cpp:151)
==8820== by 0x15CA7F: StartAdapter (CExport.cpp:22)
==8820== by 0x18F8CA: main (main.c:5)
==8820==
==8820== Invalid free() / delete / delete[] / realloc()
==8820== at 0x483CFBF: operator delete(void*) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==8820== by 0x170615: ModuleConfig::~ModuleConfig() (SlaveConfigParser.h:26)
==8820== by 0x17F992: ESI_SetModuleIdentities(int, std::vector<unsigned int, std::allocator<unsigned int> >&) (SlaveConfigParser.cpp:920)
==8820== by 0x161637: EcatAdapter::InitSlaveFileConfig() (EcatAdapter.cpp:1321)
==8820== by 0x15F583: EcatAdapter::ConfigSlaves() (EcatAdapter.cpp:762)
==8820== by 0x15DB78: EcatAdapter::Connect() (EcatAdapter.cpp:247)
==8820== by 0x15C4D4: AdapterManager::Connect() (AdapterManager.cpp:250)
==8820== by 0x15C0F6: AdapterManager::Run() (AdapterManager.cpp:151)
==8820== by 0x15CA7F: StartAdapter (CExport.cpp:22)
==8820== by 0x18F8CA: main (main.c:5)
==8820== Address 0x4ff42e800000000 is not stack'd, malloc'd or (recently) free'd
==8820==
line: 921
line: 923
环境:g++ 9.4.0 / Ubuntu 20.04 / c++11
把 std::string pdoMapName
改成 std::string pdoMapName{}
也不行
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.