程序在一段时间后终止"std::bad_alloc"
Programm terminating "std::bad_alloc" after some time
我对此进行了思考,但找不到错误。谁能帮助我在哪里做不好的编程。
一个boost::thread
通过套接字接收strings
,将它们拆分为vector<string>
并将它们分类到shared class
内的正确变量中。其他线程从那里读取。我试图通过互斥锁使其线程安全,如下所示。
我感谢任何帮助,即使是小提示:)
程序终止的方式如下:
Looping...<enter to exit>
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
这是相应的文件。它与ROS纠缠在一起,但这部分不应该是判决。
class shared
{
public:
shared() : count(0) {/*emp. body*/ } //constructor
void setvec(vector<string> &strVec, int type){
boost::upgrade_lock<boost::shared_mutex> lock(mtx);
boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
switch(type) {
case 1: typ1acc = strVec; setsensor[0] = true; break;
case 2: typ2mag = strVec; setsensor[1] = true; break;
case 3: typ3 = strVec; setsensor[2] = true; break;
}/**/
}
vector<string> getvec(int type) {
boost::shared_lock<boost::shared_mutex> lock(mtx);
switch(type) {
case 1: tmp = typ1acc; break;
case 2: tmp = typ2mag; break;
case 3: tmp = typ3; break;
}
return tmp;
}
private:
boost::shared_mutex mtx;
vector<string> tmp;
vector<string> typ1acc;
vector<string> typ2mag;
vector<string> typ3;
};
shared c; //class object
类是从多个 boost::线程调用的:
//socket function which sorts vectors into shared class
//this happens from one boost::thread
int type; //key sort by sensor type
vector<string> strVec;
c.setvec(strVec,type);
//multiple boost::threads call this to read the vectors
//this happens from multiple boost::thread
strVec = c.getvec(type);
我认为在
函数外部使用tmp
的问题在于,互斥析构函数将(或可以(在从tmp
到永久变量的复制操作之前运行,从而导致一个小窗口,其中tmp
可以被覆盖并导致潜在的数据竞争。
这些类显示每个互斥锁/字符串类何时运行,则可以看到这一点。末尾的代码为我输出以下内容 (VC++ 2015(:
CSimpleString Raw Constructor (g_tmp)
CSimpleString Raw Constructor (result)
CFakeMutex Constructor
CSimpleString Copy Raw Operator (TestFunction)
CSimpleString Copy Constructor (TestFunction)
CFakeMutex Destructor
CSimpleString Copy Operator (TestFunction)
CSimpleString Destructor (TestFunction)
Result = TestFunction
重要行缩进表示您的互斥锁在重要副本发生之前被销毁/释放。如果您将tmp
放入函数中,则操作顺序似乎不会改变,但由于tmp
是一个局部变量,因此不会发生潜在的数据争用。
用于测试这一点的非常基本的代码如下。
#include <string.h>
#include <vector>
class CFakeMutex
{
public:
CFakeMutex()
{
printf("CFakeMutex Constructorn");
}
~CFakeMutex()
{
printf("CFakeMutex Destructorn");
}
};
class CSimpleString
{
public:
CSimpleString() {
printf("CSimpleString Empty Constructorn");
}
CSimpleString(const char* pString) : m_String(pString) {
printf("CSimpleString Raw Constructor (%s)n", pString);
}
CSimpleString(const CSimpleString& String) : m_String(String.m_String) {
printf("CSimpleString Copy Constructor (%s)n", String.m_String.c_str());
}
~CSimpleString()
{
printf("CSimpleString Destructor (%s)n", m_String.c_str());
}
CSimpleString& operator=(const CSimpleString& Src)
{
if (&Src == this) return *this;
printf("CSimpleString Copy Operator (%s)n", Src.m_String.c_str());
m_String = Src.m_String;
return *this;
}
CSimpleString& operator=(const char* pString)
{
printf("CSimpleString Copy Raw Operator (%s)n", pString);
m_String = pString;
return *this;
}
std::string m_String;
};
CSimpleString g_tmp("g_tmp");
CSimpleString TestFunction()
{
CFakeMutex Mutex;
CSimpleString local_tmp("local_tmp");
//local_tmp = "TestFunction";
//return local_tmp;
g_tmp = "TestFunction";
return g_tmp;
}
int main()
{
CSimpleString result("result");
result = TestFunction();
printf("Result = %sn", result.m_String.c_str());
return 0;
}
Bo
Persson为我指出了正确的方向。我将vector<string> tmp
放入使用它的函数中,并且运行稳定。
vector<string> getvec(int type) {
boost::shared_lock<boost::shared_mutex> lock(mtx);
vector<string> tmp; //moved into the function
switch(type) { ...}}
我无法解释为什么,但它运行稳定。如果有人可以对此进行扩展,那就太好了。
相关文章:
- 使用来自 Excel VBA 的 C++ dll 时"Bad DLL calling convention" - 如何解决?
- C++17 编解码器在将标准::字符串转换为标准::字符串时抛出"bad conversion"
- 对于代理容器上的迭代器来说,"least bad implementation"可能是什么?
- 组件对象模型 (COM):IMalloc::Alloc 在哪里分配内存?
- 获取错误:在抛出"std::bad::alloc"的实例后终止调用 what(): std::bad_alloc
- 为什么 GetSystemMetrics (SM_CXVIRTUALSCREEN) 返回'bad'值?
- 将 lambda 传递给 STL 集时出错"bad function call"
- 新的 C++14 位分隔符处"Bad Character" Eclipse Oxygen 错误
- 使用 #define 被认为是"bad practice"吗?
- "400 Bad request"使用 OpenSSL BIO 进行请求时
- CGAL:Hausdorff距离不良Alloc
- 解密文件AES_256_CBC返回"bad decrypt"错误
- 在C++流中,eof(),fail(),bad()和good()有什么区别
- C STD ::初始化类对象中的Alloc错误错误
- make / gcc:"bad build"的可能原因是什么?
- 什么是地址0xbaddc0dedeadbead "Bad decode dead bead"
- 在多线程环境中,什么可能导致"bad file descriptor"?
- 尝试解决HackerBank上的BFS挑战时出现错误的alloc异常
- "std::vector"在调整大小时引发"bad allocation"异常
- 尝试使用 execp、dup2 和管道实现 shell,挂起或"bad file descriptor"