最近, 某服务低概率core dump, 精定位, 发现core的地方是对象初始化的地方, 这就奇怪了。  而且, 在别的地方用得好好的, 唯独在这个服务中异常。为了便于叙述, 我把原问题简化为一个必现的问题:

#include <iostream>
using namespace std;

class A
{
public:
	A()
	{
		int *p = 0;
		*p = 0;
	}	
};

class B
{
public:
	A a;
	void test()
	{
		cout << "test" << endl;
	}
};


int main()
{
	B b;
	b.test();
	cout << "end!" << endl;
	return 0;
}
      由于在原问题中, A是一个比较基础的类, 要找源码似乎不太方便, 但要解决问题(反正原问题就是此处有低概率core, 其他用A得地方都不core), 怎么搞呢?  我用如下方法解决了问题, 并且从此不再core dump.
#include <iostream>
using namespace std;

class A
{
public:
	A()
	{
		int *p = 0;
		*p = 0;
	}	
};

class B
{
public:
	A a;
	static void test()
	{
		cout << "test" << endl;
	}
};


int main()
{
	B::test();
	cout << "end!" << endl;
	return 0;
}
       问题解决了, 爽爽哒!



本文转载:CSDN博客