Debug Assertion Failed! 求助

ai604233436 2015-10-13 10:26:22
程序运行一小会就出现了这个问题。代码如下:
Debug Assertion Failed!
Program: ****.exe
File: dbgheap.c
line: 1044

Expression: _CrtIsValidHeapPoint(pUserData)

For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts
调试堆栈是正在运行。
代码如下:
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTempDlg dialog

CTempDlg::CTempDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTempDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTempDlg)
m_strT1 = _T("");
m_strT10 = _T("");
m_strT2 = _T("");
m_strT3 = _T("");
m_strT4 = _T("");
m_strT5 = _T("");
m_strT6 = _T("");
m_strT7 = _T("");
m_strT8 = _T("");
m_strT9 = _T("");
m_strTime = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bAutoSend=FALSE;
m_bAutoTran=FALSE;
m_nCycleTime=1000;

}

void CTempDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTempDlg)
DDX_Control(pDX, IDC_BUTTON_Send, m_ctrButtonSend);
DDX_Control(pDX, IDC_EDIT_TIME, m_ctrTime);
DDX_Control(pDX, IDC_EDIT_T9, m_ctrT9);
DDX_Control(pDX, IDC_EDIT_T8, m_ctrT8);
DDX_Control(pDX, IDC_EDIT_T7, m_ctrT7);
DDX_Control(pDX, IDC_EDIT_T6, m_ctrT6);
DDX_Control(pDX, IDC_EDIT_T5, m_ctrT5);
DDX_Control(pDX, IDC_EDIT_T4, m_ctrT4);
DDX_Control(pDX, IDC_EDIT_T3, m_ctrT3);
DDX_Control(pDX, IDC_EDIT_T2, m_ctrT2);
DDX_Control(pDX, IDC_EDIT_T10, m_ctrT10);
DDX_Control(pDX, IDC_EDIT_T1, m_ctrT1);
DDX_Text(pDX, IDC_EDIT_T1, m_strT1);
DDX_Text(pDX, IDC_EDIT_T10, m_strT10);
DDX_Text(pDX, IDC_EDIT_T2, m_strT2);
DDX_Text(pDX, IDC_EDIT_T3, m_strT3);
DDX_Text(pDX, IDC_EDIT_T4, m_strT4);
DDX_Text(pDX, IDC_EDIT_T5, m_strT5);
DDX_Text(pDX, IDC_EDIT_T6, m_strT6);
DDX_Text(pDX, IDC_EDIT_T7, m_strT7);
DDX_Text(pDX, IDC_EDIT_T8, m_strT8);
DDX_Text(pDX, IDC_EDIT_T9, m_strT9);
DDX_Text(pDX, IDC_EDIT_TIME, m_strTime);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTempDlg, CDialog)
//{{AFX_MSG_MAP(CTempDlg)
ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_Send, OnBUTTONSend)
ON_WM_TIMER()
ON_EN_CHANGE(IDC_EDIT_TIME, OnChangeEditTime)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTempDlg message handlers

BOOL CTempDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here
m_nBaud=9600;//波特率
m_nCom=1;//串口号
m_cParity='N';//奇偶校验
m_nDatabits=8;//数据位
m_nStopbits=1;//停止位
m_dwCommEvents = EV_RXFLAG | EV_RXCHAR;
CString strStatus;
if (m_Port.InitPort(this, m_nCom, m_nBaud,m_cParity,m_nDatabits,m_nStopbits,m_dwCommEvents,512))
{
m_Port.StartMonitoring();
//"当前状态:串口打开,无奇偶校验,8数据位,1停止位");
}
else
{
AfxMessageBox("没有发现此串口");
}

CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT_TIME);
CString strText;
strText.Format("%d",m_nCycleTime);
pEdit->SetWindowText(strText);

return TRUE; // return TRUE unless you set the focus to a control
}

void CTempDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CTempDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTempDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
LONG CTempDlg::OnCommunication(WPARAM ch, LPARAM port)
{
CString str;
str.Format("%02X ",ch);
m_strRCV+=str;
unsigned char dat[512];
int a[512];
int len1=Str2Hex(m_strRCV,dat);
int i=0,k=0,j=0,n,l;
//数据处理
int f[512],d[512];
n=len1/25;
for (l=0;l<n;l++)
{
for(j=0;j<25;j++)
{
d[j]=dat[j+(n-1)*25];
}

for (i=0;i<20;i++)
{
a[i]=d[i+3];
}
}
//显示转换的数据
m_strT1.Empty();
m_strT2.Empty();
m_strT3.Empty();
m_strT4.Empty();
m_strT5.Empty();
m_strT6.Empty();
m_strT7.Empty();
m_strT8.Empty();
m_strT9.Empty();
m_strT9.Empty();
m_strT10.Empty();
UpdateData(FALSE);
for (k=0;k<10;k++)
{
f[k]=a[2*k]*256+a[2*k+1];
f[k]=78*(f[k]-1450)/366+22;
}
CString str1;
str1.Format("%2d ",f[0]);
int nLen1=m_ctrT1.GetWindowTextLength();
m_ctrT1.SetSel(nLen1, nLen1);
m_ctrT1.ReplaceSel(str1);
nLen1=str1.GetLength();
m_strT1=str1;
CString str2;
str2.Format("%2d ",f[1]);
int nLen2=m_ctrT2.GetWindowTextLength();
m_ctrT2.SetSel(nLen2, nLen2);
m_ctrT2.ReplaceSel(str2);
nLen2=str2.GetLength();
m_strT2=str2;
CString str3;
str3.Format("%2d ",f[2]);
int nLen3=m_ctrT3.GetWindowTextLength();
m_ctrT3.SetSel(nLen3, nLen3);
m_ctrT3.ReplaceSel(str3);
nLen3=str3.GetLength();
m_strT3=str3;
CString str4;
str4.Format("%2d ",f[3]);
int nLen4=m_ctrT4.GetWindowTextLength();
m_ctrT4.SetSel(nLen4, nLen4);
m_ctrT4.ReplaceSel(str4);
nLen4=str4.GetLength();
m_strT4=str4;
CString str5;
str5.Format("%2d ",f[4]);
int nLen5=m_ctrT5.GetWindowTextLength();
m_ctrT5.SetSel(nLen5, nLen5);
m_ctrT5.ReplaceSel(str5);
nLen5=str5.GetLength();
m_strT5=str5;
CString str6;
str6.Format("%2d ",f[5]);
int nLen6=m_ctrT6.GetWindowTextLength();
m_ctrT6.SetSel(nLen6, nLen6);
m_ctrT6.ReplaceSel(str6);
nLen6=str6.GetLength();
m_strT6=str6;
CString str7;
str7.Format("%2d ",f[6]);
int nLen7=m_ctrT7.GetWindowTextLength();
m_ctrT7.SetSel(nLen7, nLen7);
m_ctrT7.ReplaceSel(str7);
nLen7=str7.GetLength();
m_strT7=str7;
CString str8;
str8.Format("%2d ",f[7]);
int nLen8=m_ctrT8.GetWindowTextLength();
m_ctrT8.SetSel(nLen8, nLen8);
m_ctrT8.ReplaceSel(str8);
nLen8=str8.GetLength();
m_strT8=str8;
CString str9;
str9.Format("%2d ",f[8]);
int nLen9=m_ctrT9.GetWindowTextLength();
m_ctrT9.SetSel(nLen9, nLen9);
m_ctrT9.ReplaceSel(str9);
nLen9=str9.GetLength();
m_strT9=str9;
CString str10;
str10.Format("%2d ",f[9]);
int nLen10=m_ctrT10.GetWindowTextLength();
m_ctrT10.SetSel(nLen10, nLen10);
m_ctrT10.ReplaceSel(str10);
nLen10=str10.GetLength();
m_strT10=str10;
return 0;
}
unsigned char CTempDlg::HexChar(unsigned char c)
{
if((c>='0')&&(c<='9'))
return c-0x30;
else if((c>='A')&&(c<='F'))
return c-'A'+10;
else if((c>='a')&&(c<='f'))
return c-'a'+10;
else
return 0x10;
}
int CTempDlg::Str2Hex(CString str,unsigned char* data)
{
int t,t1;
int rlen=0,len=str.GetLength();
//data.SetSize(len/2);
for(int i=0;i<len;)
{
char l,h=str[i];
if(h==' ')
{
i++;
continue;
}
i++;
if(i>=len)
break;
l=str[i];
t=HexChar(h);
t1=HexChar(l);
if((t==16)||(t1==16))
break;
else
t=t*16+t1;
i++;
data[rlen]=(char)t;
rlen++;
}
return rlen;
}

void CTempDlg::OnBUTTONSend()
{
m_bAutoSend=!m_bAutoSend;//发送标志
if(m_bAutoSend)
m_ctrButtonSend.SetWindowText("发送");
else
m_ctrButtonSend.SetWindowText("停止发送");
if(m_bAutoSend)//判断是否自动发送
{
SetTimer(1,m_nCycleTime,NULL);
}
else
{
KillTimer(1);
}
}

void CTempDlg::send()
{
UpdateData(TRUE);

CString str;
str="01 04 00 00 00 0a 70 0d";//发送数据
unsigned char data[512];
int len=Str2Hex(str,data);
m_Port.WriteToPort(data,len);
}

void CTempDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
switch(nIDEvent)
{
case 1:
send();
break;

}
CDialog::OnTimer(nIDEvent);
}

void CTempDlg::OnChangeEditTime()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.

// TODO: Add your control notification handler code here
CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT_TIME);
CString strText;
pEdit->GetWindowText(strText);
m_nCycleTime=atoi(strText);

}
...全文
213 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-10-16
  • 打赏
  • 举报
回复
判断是否越界访问,可以在数组的最后一个元素之后对应的地址处设置数据读写断点。如果该地址对应其它变量干扰判断,可将数组多声明一个元素,并设置数据读写断点在该多出元素对应的地址上。
#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main() {
    int a,b[11];//本来是b[10],为判断哪句越界,故意声明为b[11]

    srand((unsigned int)time(NULL));//按两次F11,等黄色右箭头指向本行时,调试、新建断点、新建数据断点,地址:&b[10],字节计数:4,确定。
    while (1) {//按F5,会停在下面某句,此时a的值为10,b[10]已经被修改为对应0..4之一。
        b[(a=rand()%11)]=0;
        Sleep(100);
        b[(a=rand()%11)]=1;
        Sleep(100);
        b[(a=rand()%11)]=2;
        Sleep(100);
        b[(a=rand()%11)]=3;
        Sleep(100);
        b[(a=rand()%11)]=4;
        Sleep(100);
    }
    return 0;
}
ai604233436 2015-10-16
  • 打赏
  • 举报
回复
已找到问题所在,原因在于超出数组范围。
赵4老师 2015-10-14
  • 打赏
  • 举报
回复
有时不将“调用函数名字+各参数值,进入函数后各参数值,中间变量值,退出函数前准备返回的值,返回函数到调用处后函数名字+各参数值+返回值”这些信息写日志到文件中是无论如何也发现不了问题在哪里的,包括捕获各种异常、写日志到屏幕、单步或设断点或生成core文件、……这些方法都不行! 写日志到文件参考下面:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#else
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#define CRITICAL_SECTION pthread_mutex_t
#define _vsnprintf vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
struct tm *now;
struct timeb tb;

if (NULL==pszFmt||0==pszFmt[0]) return;
_vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
ftime(&tb);
now=localtime(&tb.time);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(mss,"%03d",tb.millitm);
printf("%s %s.%s %s",datestr,timestr,mss,logstr);
flog=fopen(logfilename1,"a");
if (NULL!=flog) {
fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
if (ftell(flog)>MAXLOGSIZE) {
fclose(flog);
if (rename(logfilename1,logfilename2)) {
remove(logfilename2);
rename(logfilename1,logfilename2);
}
} else {
fclose(flog);
}
}
}
void Log(const char *pszFmt,...) {
va_list argp;

Lock(&cs_log);
va_start(argp,pszFmt);
LogV(pszFmt,argp);
va_end(argp);
Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
int i;
#ifdef WIN32
InitializeCriticalSection(&cs_log);
#else
pthread_mutex_init(&cs_log,NULL);
#endif
for (i=0;i<10000;i++) {
Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
}
#ifdef WIN32
DeleteCriticalSection(&cs_log);
#else
pthread_mutex_destroy(&cs_log);
#endif
return 0;
}
//1-78行添加到你带main的.c或.cpp的那个文件的最前面
//81-85行添加到你的main函数开头
//89-93行添加到你的main函数结束前
//在要写LOG的地方仿照第87行的写法写LOG到文件MyLog1.log中
ai604233436 2015-10-14
  • 打赏
  • 举报
回复
引用 2 楼 pipi20151001 的回复:
诊断失败,你可以跟踪进去
怎么跟踪进去?
ai604233436 2015-10-14
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。
里面就写着一句话call stack unavailable while child is running
pipi20151001 2015-10-14
  • 打赏
  • 举报
回复
诊断失败,你可以跟踪进去
赵4老师 2015-10-14
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。
赵4老师 2015-10-14
  • 打赏
  • 举报
回复
你先在怀疑可能有问题的代码片断附近插入相应log语句,输出相关变量的值。 然后编译链接运行你的程序。 当你程序运行出现异常时,打开log文件看其中的内容,使用你自己的脑力挑出异常时其中异常的相关变量的值。 电脑还没聪明到或勤奋到能替你这个懒惰的主人找到出错位置和出错原因的程度。
赵4老师 2015-10-14
  • 打赏
  • 举报
回复
引用 14 楼 ai604233436 的回复:
[quote=引用 13 楼 zhao4zhong1 的回复:] 包括捕获各种异常、写日志到屏幕、单步或设断点或生成core文件、……这些方法都不行! 你按F10也就是我说的单步,已经被我定性了,你还用,只能赖你自己。
程序放进去了。还是没说哪出错了[/quote] 你先在怀疑有问题的代码片断的字里行间插入log语句输出相关的变量的值到log文件中,再运行你的程序,直到出现异常,然后打开日志文件看看在何时你的程序log出了什么信息,从这些信息中使用你的脑力提取出程序出现异常时相关的变量的值都有哪些异常。 电脑还没有聪明到或勤奋到帮你这个懒惰的主人找到代码或数据中隐藏很深的错误的。
ai604233436 2015-10-14
  • 打赏
  • 举报
回复
引用 13 楼 zhao4zhong1 的回复:
包括捕获各种异常、写日志到屏幕、单步或设断点或生成core文件、……这些方法都不行!
你按F10也就是我说的单步,已经被我定性了,你还用,只能赖你自己。
程序放进去了。还是没说哪出错了
赵4老师 2015-10-14
  • 打赏
  • 举报
回复
包括捕获各种异常、写日志到屏幕、单步或设断点或生成core文件、……这些方法都不行! 你按F10也就是我说的单步,已经被我定性了,你还用,只能赖你自己。
ai604233436 2015-10-14
  • 打赏
  • 举报
回复
引用 11 楼 zhao4zhong1 的回复:
不要偷懒,不要瞎猜,请楼主亲自动手将“调用函数名字+各参数值,进入函数后各参数值,中间变量值,退出函数前准备返回的值,返回函数到调用处后函数名字+各参数值+返回值”这些信息写日志到文件中自行分析。

代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
我都按了10分钟的F10都没停。
赵4老师 2015-10-14
  • 打赏
  • 举报
回复
不要偷懒,不要瞎猜,请楼主亲自动手将“调用函数名字+各参数值,进入函数后各参数值,中间变量值,退出函数前准备返回的值,返回函数到调用处后函数名字+各参数值+返回值”这些信息写日志到文件中自行分析。 代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
ai604233436 2015-10-14
  • 打赏
  • 举报
回复
有没有可能是数组大小不对,但是怎么查对不对呢? 我看起来感觉相对的,而且是程序运行大概半分钟就出现了这个问题,而不是一点运行就出现。是这种情况。大神们,这个问题大致出现在哪呢?应该怎么解决呢?
ai604233436 2015-10-14
  • 打赏
  • 举报
回复
引用 8 楼 zhao4zhong1 的回复:
81-85行添加到你类的构造函数的开头


析构函数是自己构造吧?我这只看到构造函数了
赵4老师 2015-10-14
  • 打赏
  • 举报
回复
1-78行添加到你的.c或.cpp的那个文件的最前面 81-95行添加到你类的构造函数的开头 89-93行添加到你类的析构函数结束前 在要写LOG的地方仿照第87行的写法写LOG到文件MyLog1.log中
赵4老师 2015-10-14
  • 打赏
  • 举报
回复
81-85行添加到你类的构造函数的开头
ai604233436 2015-10-14
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
有时不将“调用函数名字+各参数值,进入函数后各参数值,中间变量值,退出函数前准备返回的值,返回函数到调用处后函数名字+各参数值+返回值”这些信息写日志到文件中是无论如何也发现不了问题在哪里的,包括捕获各种异常、写日志到屏幕、单步或设断点或生成core文件、……这些方法都不行! 写日志到文件参考下面:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#include <io.h>
#else
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#define CRITICAL_SECTION pthread_mutex_t
#define _vsnprintf vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
struct tm *now;
struct timeb tb;

if (NULL==pszFmt||0==pszFmt[0]) return;
_vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
ftime(&tb);
now=localtime(&tb.time);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min ,now->tm_sec );
sprintf(mss,"%03d",tb.millitm);
printf("%s %s.%s %s",datestr,timestr,mss,logstr);
flog=fopen(logfilename1,"a");
if (NULL!=flog) {
fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
if (ftell(flog)>MAXLOGSIZE) {
fclose(flog);
if (rename(logfilename1,logfilename2)) {
remove(logfilename2);
rename(logfilename1,logfilename2);
}
} else {
fclose(flog);
}
}
}
void Log(const char *pszFmt,...) {
va_list argp;

Lock(&cs_log);
va_start(argp,pszFmt);
LogV(pszFmt,argp);
va_end(argp);
Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
int i;
#ifdef WIN32
InitializeCriticalSection(&cs_log);
#else
pthread_mutex_init(&cs_log,NULL);
#endif
for (i=0;i<10000;i++) {
Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
}
#ifdef WIN32
DeleteCriticalSection(&cs_log);
#else
pthread_mutex_destroy(&cs_log);
#endif
return 0;
}
//1-78行添加到你带main的.c或.cpp的那个文件的最前面
//81-85行添加到你的main函数开头
//89-93行添加到你的main函数结束前
//在要写LOG的地方仿照第87行的写法写LOG到文件MyLog1.log中
f非常感谢,但是我这个没有main函数,那写到哪?

16,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧