这段代码有病吗?
我的整个project里面的其中2个文件:
MyNode.cpp
#include "MyNode.h"
#define _LookForBug 1
template <class T>
C_MemData<T>::C_MemData()
{
}
template <class T>
C_MemData<T>::~C_MemData()
{
#if _LookForBug
delete pz;
#endif
}
template <class T>
void C_MemData<T>::CreateMem(int xs, int ys)
{
#if _LookForBug
xa.AxisNumber = xs;
ya.AxisNumber = ys;
size = xs + ys + 2 + xs*ys;
pz = new T(size);
memset(pz, 0, size);
#endif
}
/*
C_UN_CC_TYPE<T>
*/
template <class T>
void C_UN_CC_TYPE<T>::SetPara(CHRCTRSTC_TYPE _cc_type, int xsize, int ysize)
{
#if _LookForBug
cc_type = _cc_type;
if(_cc_type == VALUE )
{
cc_value = new CC_VALUE<T>;
}
else if(_cc_type == MAP)
{
p_MemData = new C_MemData<T>;
p_MemData->CreateMem(xsize, ysize);
}
#endif
}
template <class T>
C_UN_CC_TYPE<T>::C_UN_CC_TYPE()
{
}
template <class T>
C_UN_CC_TYPE<T>::~C_UN_CC_TYPE()
{
#if _LookForBug
if( cc_type == VALUE )
{
delete cc_value;
}
else if(cc_type == MAP)
{
delete p_MemData;
}
#endif
}
/******************************************************************************
C_T_MEM
*******************************************************************************/
C_T_MEM::C_T_MEM()
{}
void C_T_MEM::SetPara(DATATYPE dt, CHRCTRSTC_TYPE cc_type, int xsize, int ysize)
{
#if _LookForBug
my_dt = dt;
if(dt == _UBYTE)
{
p_ut_byte = new C_UN_CC_TYPE<BYTE>;
p_ut_byte->SetPara(cc_type,xsize,ysize);
}
else if(dt == _UWORD)
{
p_ut_word = new C_UN_CC_TYPE<WORD>;
p_ut_word->SetPara(cc_type,xsize,ysize);
}
else if(dt == _SWORD)
{
p_ut_sword = new C_UN_CC_TYPE<SWORD>;
p_ut_sword->SetPara(cc_type, xsize, ysize);
}
#endif
}
C_T_MEM::~C_T_MEM()
{
#if _LookForBug
if(p_ut_byte == NULL)
{
return;
}
if(my_dt == _UBYTE)
delete p_ut_byte;//union,delete one is okay.
else if(my_dt == _UWORD)
delete p_ut_word;
else if(my_dt == _SWORD)
delete p_ut_sword;
#endif
}
MyNode::~MyNode()
{
if(Block_IF_DATA->Asap1b_Ccp)
{
delete Block_IF_DATA->Asap1b_Ccp;
}
if(Block_IF_DATA)
{
delete Block_IF_DATA;
}
};
/******************************************************************************
CMyNodeList
******************************************************************************/
CMyNodeList::CMyNodeList()
{
TotalNum = 0;
pHead = pTail = NULL;
}
CMyNodeList::~CMyNodeList()
{
MyNeed* pCur;
MyNeed* pNext;
pCur = pHead;
pHead = pTail = NULL;
while(pCur != NULL)
{
pNext = pCur->pNext;
delete(pCur);
pCur = pNext;
}
TotalNum = 0;
}
MyNode* CMyNodeList::Find(AnsiString Name_Str)
{
MyNode* p_C = pHead;
for(; p_C != NULL; p_C = p_C->pNext)
{
if(p_C->Name_Str == Name_Str)
{
return p_C;
}
}
return NULL;
}
void CMyNodeList::Append(MyNode* pc)
{
TotalNum++;
if(pHead == NULL)
{
pHead = pc;
pTail = pc;
pHead->pNext = NULL;
pTail->pNext = NULL;
}
else
{
pTail->pNext = pc;
pTail = pc;
pTail->pNext = NULL;
}
}
头文件
MyNode.h
#ifndef __MyNeed_H
#define __MyNeed_H
#include <vcl.h>
#include "DataType.h"
#include <vector>
#include <list>
using namespace std;
#define CHRCTRSTC_NUM 6
typedef enum
{AA, BB, CC}CHRCTRSTC_TYPE;
const AnsiString CHRCTRSTC_Table[CHRCTRSTC_NUM] =
{
"AA", "BB", "CC"
};
typedef enum
{AA_AXIS, BB_AXIS, CC_AXIS}AXIS_TYPE;
template <typename T>
struct CC_AA
{
T x;
};
typedef struct _Axis
{
BYTE AxisNumber;
float StartValue;
float Difference;
}Axis;
template <class T>
class C_MemData
{
public:
Axis xa;
Axis ya;
int size;
T *pz;
public:
C_MemData();
void CreateMem(int xs, int ys);
~C_MemData();
};
template <class T>
class C_UN_CC_TYPE
{
public:
CHRCTRSTC_TYPE cc_type;
union
{
CC_VALUE<T> *cc_value;
C_MemData<T> * p_MemData;
};
public:
void SetPara(CHRCTRSTC_TYPE _cc_type, int xsize, int ysize);
C_UN_CC_TYPE();
~C_UN_CC_TYPE();
C_UN_CC_TYPE & operator=(const int &t1)
{
cc_value = t1;
p_MemData = t1;
cc_type = t1;
return *this;
}
};
class C_T_MEM
{
public:
union //UPTM
{
C_UN_CC_TYPE<BYTE>* p_ut_byte;
C_UN_CC_TYPE<WORD>* p_ut_word;
C_UN_CC_TYPE<SWORD>* p_ut_sword;
};
DATATYPE my_dt;
public:
C_T_MEM();
~C_T_MEM();
void SetPara(DATATYPE dt, CHRCTRSTC_TYPE cc_type, int xsize, int ysize);
public:
C_T_MEM & operator=(const int &t1)
{
// v = t1;
// p_ut_byte = t1;
p_ut_word = NULL;
my_dt = t1;
return *this;
}
};
//template <typename T>
class CMyNode: public NODETYPE
{
public:
struct CMyNode *pNext;
CHRCTRSTC_TYPE cc_type;
C_T_MEM CTM;
public:
CMyNode()
{};
~CMyNode();
CMyNode(CHRCTRSTC_TYPE _cc_type)
{
cc_type = _cc_type;
};
};
class CMyNodeList
{
public:
int TotalNum;
public:
MyNode *pHead;
MyNode *pTail;
public:
CMyNodeList();
~CMyNodeList();
MyNode* Find(AnsiString Name_Str);
void Append(MyNode *pc);
void Remove( );
void Remove(MyNode *pc);
};
#endif
在project里的其他文件是这么调用的:
MyNode *mn;
switch(mn->cc_type)
{
case AA:
mn->CTM.SetPara(mn->DataType, mn->cc_type, 0, 0);
break;
case CC:
mn->CTM.SetPara(mn->DataType, mn->cc_type, 7, 5);
break;
}
当我把#define _LookForBug 1
改成
#define _LookForBug 0时
整个project没有一点儿问题。
但是当我使用#define _LookForBug 1时,就会出现下面的问题:
---------------------------
Information
---------------------------
Project E:\freescale_work\MyApp.exe raised too many consecutive exceptions: 'access violation at 0x00578f77: read of address 0x00000010'. Process Stopped. Use Step or Run to continue.
---------------------------
OK Help
---------------------------
帮忙,多谢。
[解决办法]
把你代码里面的#if换成 #ifdef