今天在别人写的程序上做新功能,析构的时候发现:CRT detected that the application wrote to memory 内存写错误,经过跟踪调试,发现在遍历delete A->listB 的时候 listB里又循环嵌套了A, 也就是说已经把A deleted 了,但是没设为NULL, 于是之后再次delete A 变出现错误, 也就是所谓的"野指针", ,他们居然不写个safe_delete宏, 害我跟了半天.
以后凡是报此类问题,皆是操作了未new的内存,
1.数组越界操作
2.DELETE后没有设NULL, 野指针
Reference:http://hi.baidu.com/kidcdf/blog/item/c886b34bd123b3fb83025c64.html
但是还有一种情况,根据我猜测和测试 由于宏而引起的,这种宏主要存在于头文件中才会导致内存写错误,举个例子吧:
去掉头文件中的宏,就不会出现内存写错误,具体为什么 还在研究中
#pragma once
#ifndef USE_EMULATION
class CEmulation;
#endif
class Test
{
public:
Test();
~Test();
......
private:
......
#ifdef USE_EMULATION
CEmulation* m_pEmulation;
#endif
};
....
Test::Test()
{
#ifdef USE_MULATION
m_pEmulation = new CEmulation();
#endif
}
~Test::Test()
{
#ifdef USE_MULATION
delete m_pEmulation;
m_pEmulation = NULL;
#endif
}
.....