Valgrind抱怨数据可能丢失
Valgrind complains data possibly lost
我在c++中有一个回调函数,它做的是:
void MasterInterface::zsock_cb(struct ev_loop *loop, ev_zsock_t *wz, int revents) {
char iden[MAX_MSG_LEN];
char msg[MAX_MSG_LEN];
int numIDBytes = zmq_recv(wz->zsock, iden, sizeof(iden), 0);
int numBytes = zmq_recv(wz->zsock, msg, sizeof(msg), 0);
masterStruct* mStruct = (masterStruct*)wz->data;
std::string idenData = std::string(static_cast<char*>(iden), numIDBytes);
std::string msgData = std::string(static_cast<char*>(msg), numBytes);
cout << "[IN] " << msgData << endl;
if (!mStruct->synchronized) {
if (!IDRegistered(mStruct, idenData)) {
cout << "[IN] " << idenData << endl;
cout << "[IN] " << msgData << endl;
mStruct->slaveIDs[mStruct->counter++] = idenData;
zmq_send (wz->zsock, idenData.c_str(), idenData.size(), ZMQ_SNDMORE);
zmq_send (wz->zsock, msgData.c_str(), msgData.size(), 0);
}
if (mStruct->counter==SLAVES_EXPECTED) {
sendCommand(wz,"Master connected to slaves.");
mStruct->synchronized = true;
}
}
}
bool MasterInterface::IDRegistered(masterStruct* mStruct, const std::string& id) {
for (int i=0; i < SLAVES_EXPECTED; i++) {
if (mStruct->slaveIDs[i]=="") continue;
if (mStruct->slaveIDs[i].compare(id)==0)
return true;
}
return false;
}
我在程序上运行valgrind,它报告3块中的87字节可能丢失,表明这是数据"可能丢失"的行。std::string identidata = std::string(static_cast(identity), numIDBytes);
我不认为我做错了什么,为什么valgrind抱怨?这是假阳性吗?
谢谢。
这是mstruct。
struct masterStruct {
public:
int counter;
bool synchronized;
std::string slaveIDs[3];
};
编译没有优化。o0
下面是valgrind消息:
^C[Signal Caught] Closing connections...
Exited Master Process...
==6606==
==6606== HEAP SUMMARY:
==6606== in use at exit: 143 bytes in 4 blocks
==6606== total heap usage: 5,866 allocs, 5,862 frees, 533,480 bytes allocated
==6606==
==6606== Searching for pointers to 4 not-freed blocks
==6606== Checked 239,000 bytes
==6606==
==6606== 87 bytes in 3 blocks are possibly lost in loss record 2 of 2
==6606== at 0x4A074CC: operator new(unsigned long) (vg_replace_malloc.c:298)
==6606== by 0x3A7CC9C3C8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.13)
==6606== by 0x3A7CC9CDE4: ??? (in /usr/lib64/libstdc++.so.6.0.13)
==6606== by 0x3A7CC9CF6A: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, unsigned long, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.13)
==6606== by 0x402FD5: MasterInterface::zsock_cb(ev_loop*, ev_zsock_t*, int) (MasterInterface.cpp:74)
==6606== by 0x404C6B: s_check_cb (ev_zsock.c:61)
==6606== by 0x4E872BC: ev_invoke_pending (ev.c:3155)
==6606== by 0x4E8BAE1: ev_run (ev.c:3555)
==6606== by 0x403423: MasterInterface::init() (MasterInterface.cpp:118)
==6606== by 0x4046DB: main (PXSMaster.cpp:29)
==6606==
==6606== LEAK SUMMARY:
==6606== definitely lost: 0 bytes in 0 blocks
==6606== indirectly lost: 0 bytes in 0 blocks
==6606== possibly lost: 87 bytes in 3 blocks
==6606== still reachable: 56 bytes in 1 blocks
==6606== suppressed: 0 bytes in 0 blocks
==6606== Reachable blocks (those to which a pointer was found) are not shown.
==6606== To see them, rerun with: --leak-check=full --show-reachable=yes
==6606==
==6606== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)
--6606--
--6606-- used_suppression: 4 U1004-ARM-_dl_relocate_object
==6606==
==6606== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)
bash-4.1$
@Dark Falcon是正确的。
MasterInterface类被声明为一个全局变量。在这个类中,有struct masterStruct masStruct;
,其中有3个std::string数组。
这就是问题所在。当我按Ctrl-C时,valgrind可能没有考虑全局变量,因此这些字节可能会丢失。
将MasterInterface声明放在main()中解决了可能丢失的错误。
相关文章:
- 防止主数据类型C++的隐式转换
- valgrind-hellgrind与泄漏检查的结果不同
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 链表,反向函数,数据结构
- C++ mac OS X El Capitan上的openmp,Valgrind错误(数据竞赛)
- 如何故意用未初始化的数据初始化变量,以便valgrind将该变量视为未初始化
- Valgrind callgrind 是否使用总时间或"execution time"来创建数据
- Valgrind抱怨数据可能丢失
- Valgrind显示的数据在构造函数c++中肯定丢失了
- Valgrind是否有像Purify/Quantify这样的API,可以让您禁用数据记录