?? memrecord.h
字號:
/**
* file : MemRecord.h
* comment :
*
*/
#ifndef MemRecord_h
#define MemRecord_h 1
#include <map>
#include <list>
#include <stack>
#include <pthread.h>
#include <sys/wait.h>
#include "Header.h"
#include "CommonMutex.h"
using namespace std;
#define SINGLE_NEW 0x00 // indicate alloc memory with new type
#define ARRAY_NEW 0x01 // indicate alloc memory with new[] type
#define SINGLE_DELETE 0x02 // indicate free memory with delete type
#define ARRAY_DELETE 0x03 // indicate free memory with delete[] type
#define FILENAME_LENGTH 32 // the filename length
#define MEMORY_INFO 0X12345678 // indicate the message type on the message queue
typedef struct
{
char Filename[ FILENAME_LENGTH ]; // new所在的源程序文件名
unsigned long LineNum; // new在源文件中的行號
size_t AllocSize; // 分配的內存大小
int OperationType;
void * pBuffer; // 分配后得到的內存指針
short errCode; // 0 - 沒有釋放, 1 - delete了new[]分配的內存
}MemOperation;
typedef struct
{
int Type; // message type, in this module must be MEMORY_INFO
MemOperation Data; // content of memory operation
} MsgBuffer;
#if defined( MEM_DEBUG )
class MemRecord
{
public:
/**
* 函數名稱: MemRecord
* 函數功能: 初始化類的數據成員(創建消息隊列)
* @param:
* @return:
* @see:
*/
MemRecord();
/**
* 函數名稱: ~MemRecord
* 函數功能: 打印m_mapMemory&m_listMemory中的內存
* 刪除消息隊列
* @param:
* @return:
* @see:
*/
virtual ~MemRecord();
/**
* 函數名稱: Insert
* 函數功能: 將一次內存操作插入到映射表(m_mapMemory)中
* @param: pBuffer - 分配成功的內存指針, 作為映射表的鍵值
* @param: pRecord - 內存操作的情況(包括文件名, 行號, 分配大小, 分配方式等)
* @return:
* @see: Erase
*/
void Insert( void *pBuffer, MemOperation *pRecord );
/**
* 函數名稱: Erase
* 函數功能: 對m_mapMemory中的相應記錄進行刪除
* @param: pBuffer - 所要刪除的內存指針
* @return: pRecord - delete的具體情況(包括文件名, 行號, 分配大小, 刪除方式等)
* @see: Insert
*/
int Erase( void *pBuffer, MemOperation *pRecord );
/**
* 函數名稱: GetMsgQueue
* 函數功能: 取得該類所創建的消息隊列號
* @param: 無
* @return: 消息隊列標識號
* @see:
*/
int GetMsgQueue() { return m_nMsgQueue; }
/**
* 函數名稱: GetMsgFilePath
* 函數功能: 取得建立消息隊列所用到的文件名
* @param: path - 存放所取得的文件名
* @return:
* @see:
*/
void GetMsgFilePath( char *path );
/**
* 函數名稱: GetMainProcessPid
* 函數功能: 取得被檢測進程pid
* @param: 無
* @return: 被檢測進程pid
* @see:
*/
pid_t GetMainProcessPid() {return m_pidMain;};
private:
/**
* Record the memory alloc, use the pointer from new operator success alloction as the key value of
* map data structor
*
* After exit the main() function, the result of memory leak will be output to a file named "leak.rec"
* that exist on the directory with execute file. if the file exist, it will truncate the length to zero.
*/
map<void *, MemOperation> m_mapMemory;
list<MemOperation> m_listMemory; // save the ErrorDelete(the way of new and delete is not matching) information.
/**
* gurantee that synchronized operation of the m_mapMemory and m_listMemory
*/
CCommonMutex m_mutexRecord;
/**
* indicate the message queue
*/
int m_nMsgQueue;
/**
*
*/
char m_szMsgPath[ 64 ];
pid_t m_pidMain;
};
/**
* 在delete operator發生遞歸調用的時候,對前一次的delete operator調用所
* 產生的文件名和行號進行現場保留。
*/
typedef struct
{
char Filename[ FILENAME_LENGTH ]; //
unsigned long LineNum; //
}DELINFOSTACK;
#define THIS_FILE __FILE__
void* operator new( size_t nSize, char* pszFileName, int nLineNum );
void* operator new[]( size_t nSize, char* pszFileName, int nLineNum );
void operator delete( void *ptr );
void operator delete[]( void *ptr );
void InterruptHandler( int signo );
#define DEBUG_NEW new( THIS_FILE, __LINE__ )
extern char DELETE_FILE[ FILENAME_LENGTH ];
extern int DELETE_LINE;
extern void BuildStack();
/**
* 原來用的是POSIX中的pthread_mutex_t, 當時pthread_mutex_t
* 在同一線程重入時會造成死鎖, 現在換成CCommonMutex類型(2003/05/13)
*/
extern CCommonMutex globalLock;
// if the DELETE_LINE is not 0, it means we need keep the older
// info in mind for subsequence call.
#define DEBUG_DELETE globalLock.Lock(); \
if (DELETE_LINE != 0) BuildStack();\
strncpy( DELETE_FILE, __FILE__,FILENAME_LENGTH - 1 ); \
DELETE_FILE[ FILENAME_LENGTH - 1 ]= '\0'; \
DELETE_LINE = __LINE__; \
delete
#else
class MemRecord {};
#endif // end defined( MEM_DEBUG )
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -