我写了一个程序,用到了数据库、socket、线程,程序运行一段时间后(时间不定,有时一下午,有是两三天)会出现runtime error,点确定后退出,这个问题很严重。请问各位遇到过么?怎么解决?
我曾经看过用try except忽略的说明,不过我在数据库部分使用了trycatch,好像try之间不能嵌套,请各位指教。

解决方案 »

  1.   

    这是一个事后调试的情况,可以使用windows的Dr.Watson日志文件,能比较快速找到问题所在
      

  2.   

    先用win2k的任务管理器看你的程序有没有资源泄漏
    内存,句柄,GDI等资源---------
      ++C++
    ---------
      

  3.   

    to flyelf:
       windows的dr.watson日志文件是不是就是事件查看器中查看的文件?
    还有Try except怎么用啊
      

  4.   

    #include "stdafx.h"
    #include "ShortMsg.h"
    #include "ShortMsgDlg.h"#include "NkiDll/NkiDll.h"// 初始化网络
    #include <winsock2.h>
    #include "info.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif// ******* Define Global var  ********* 
    //
    const int iPort = 5151;
    const int lPort = 5150;
    BOOL g_DCheck = false;int SendMsg(const alarmInfo& info);
    // *********** InitSocket **************
    //
    int InitSocket()
    {
    WSADATA wsd;

    // 初始化网络
    if ( WSAStartup( MAKEWORD(2, 2), &wsd) != 0)
    {
    AfxMessageBox("网络初始化失败!");
    } return 0;
    }// ************** InitDataBase **********
    //
    int InitDbConn(_bstr_t & conn )
    {
    ::CoInitialize(NULL);
    IRiseAccObjPtr RiseObj;
    HRESULT hr;
    hr=RiseObj.CreateInstance(__uuidof(RiseAccObj));
    if (FAILED(hr))
    {
    AfxMessageBox("连接串出错!");
    ::CoUninitialize();
    return false;
    }

    RiseObj->GetSidData("RiseOffice");
    conn=RiseObj->GetconnStr();
    ::CoUninitialize();
    return 0;
    }// *********** GetlocalIp **************
    //
    int GetlocalIp(char m_I[16])
    {
    // 定义、初始化变量
    char name[255];  
    PHOSTENT hostinfo;
    char * m_Ip =(char *)malloc(sizeof(char) * 16); // 取得ip地址
    if( gethostname ( name, sizeof(name)) == 0)
    {
    if((hostinfo = gethostbyname(name)) != NULL)
    {
    m_Ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
    }
    } strcpy(m_I, m_Ip); // 设置输出变量
    free(m_Ip);
    m_Ip = NULL;
    return 0;
    }
    // ************ UserRegister ***************
    //
    int UserRegister(const ClientInfo & uRegInfo)
    {
    ::CoInitialize(NULL); _ConnectionPtr d_Connection;
    _RecordsetPtr d_Recordset;
    _variant_t RecordsAffected;
    _bstr_t connStr;
    _bstr_t query;
    HRESULT hr;
    char rsql[128];
    char csql[128];
    alarmInfo aInfo;

    InitDbConn(connStr);
    memset(&aInfo, '\0', sizeof(aInfo));
    memset(csql, '\0', 128);
    strcpy(csql, "Select * from RiseManager..EmployeeDefine where Email ='");
    strcat(csql, uRegInfo.userName);
    strcat(csql, "' and privateKey='");
    strcat(csql, uRegInfo.passWord);
    strcat(csql, "'");
    query=csql; try
    {
    hr=d_Recordset.CreateInstance (__uuidof(Recordset));
    if(FAILED(hr))
    {
    AfxMessageBox("创建数据集错误!");
    ::CoUninitialize();
    return -1;
    }
    hr=d_Recordset->Open(query,connStr,adOpenDynamic,adLockOptimistic,adCmdText);
    if(FAILED(hr))
    {
    AfxMessageBox("打开数据集错误");
    d_Recordset->Close();
    d_Recordset = NULL;
    ::CoUninitialize();
    return -1;
    }
    if (d_Recordset->adoEOF)
    {
    aInfo.aIType = A_USERINFOERROR;
    strcpy(aInfo.userIp, uRegInfo.localIp); 
    SendMsg(aInfo);
    d_Recordset->Close();
    d_Recordset = NULL;
    ::CoUninitialize();
    return 1;
    }
    }
    catch (_com_error & e)
    {
    AfxMessageBox(e.ErrorMessage ());
    d_Recordset->Close();
    d_Recordset = NULL;
    ::CoUninitialize();
    return -1;
    } memset(rsql, '\0', 128);
    strcpy(rsql, "UserRegist @UserName='");
    strcat(rsql, uRegInfo.userName);
    strcat(rsql, "', @UserIp='");
    strcat(rsql, uRegInfo.localIp);
    strcat(rsql, "'");

    query=rsql;
    try
    {
    hr=d_Connection.CreateInstance (__uuidof(Connection));
    if (FAILED(hr))
    {
    AfxMessageBox("创建连接错误!");
    d_Connection->Close();
    d_Connection = NULL;
    ::CoUninitialize();
    return -1;
    }
    hr = d_Connection->Open(connStr, "", "", adModeUnknown);
    if (FAILED(hr))
    {
    AfxMessageBox("打开连接错误!");
    d_Connection->Close();
    d_Connection = NULL;
    ::CoUninitialize();
    return -1;
    }
    hr = d_Connection->Execute(query, &RecordsAffected, adCmdText);
    if (FAILED(hr))
    {
    AfxMessageBox("执行错误!");
    d_Connection->Close();
    d_Connection = NULL;
    ::CoUninitialize();
    return -1;
    }
    }
    catch (_com_error & e)
    {
    AfxMessageBox(e.ErrorMessage ());
    d_Connection->Close();
    d_Connection = NULL;
    ::CoUninitialize();
    return -1;
    } d_Recordset->Close();
    d_Recordset = NULL;
    d_Connection->Close();
    d_Connection = NULL;
    ::CoUninitialize();
    return 0;
    }
      

  5.   

    // ************** UserQuit ****************
    //
    int UserQuit(const ClientInfo& info)
    {
    ::CoInitialize(NULL); _ConnectionPtr d_Connection;
    _variant_t RecordsAffected;
    _bstr_t connStr;
    HRESULT hr;
    char rsql[128];

    InitDbConn(connStr);
    memset(rsql, '\0', 128);
    strcpy(rsql, "UserQuit @UserName='");
    strcat(rsql, info.userName);
    strcat(rsql, "', @UserIp='");
    strcat(rsql, info.localIp);
    strcat(rsql, "'");

    try
    {
    hr=d_Connection.CreateInstance (__uuidof(Connection));
    if (FAILED(hr))
    {
    AfxMessageBox("创建连接错误!");
    ::CoUninitialize();
    return -1;
    }
    hr = d_Connection->Open(connStr, "", "", adModeUnknown);
    if (FAILED(hr))
    {
    AfxMessageBox("打开连接错误!");
    d_Connection->Close();
    d_Connection = NULL;
    ::CoUninitialize();
    return -1;
    }
    hr = d_Connection->Execute(rsql, &RecordsAffected, adCmdText);
    if (FAILED(hr))
    {
    AfxMessageBox("执行错误!");
    d_Connection->Close();
    d_Connection = NULL;
    ::CoUninitialize();
    return -1;
    }
    }
    catch (_com_error & e)
    {
    AfxMessageBox(e.ErrorMessage ());
    d_Connection->Close();
    d_Connection = NULL;
    ::CoUninitialize();
    return -1;
    } d_Connection->Close();
    d_Connection = NULL;
    ::CoUninitialize();

    return 0;
    }
    // ************ UserReceive ****************
    //
    int UserReceive(const ClientInfo& info)
    {
    ::CoInitialize(NULL); _ConnectionPtr d_Connection;
    _variant_t RecordsAffected;
    _bstr_t connStr;
    _bstr_t query;
    HRESULT hr;
    char rsql[128];
    char temp[6];

    InitDbConn(connStr);
    memset(rsql, '\0', 128); strcpy(rsql, "UserReceive @id='");
    strcat(rsql, itoa(info.task.workflowid, temp, 10));
    strcat(rsql, "', @it='");
    strcat(rsql, itoa(info.task.workflowinstance, temp, 10));
    strcat(rsql, "', @idx='");
    strcat(rsql, info.task.workflowinstanceidx );
    strcat(rsql, "'");
    query=rsql;
    try
    {
    hr=d_Connection.CreateInstance (__uuidof(Connection));
    if (FAILED(hr))
    {
    AfxMessageBox("创建连接错误!");
    d_Connection->Close();
    d_Connection = NULL;
    ::CoUninitialize();
    return -1;
    }
    hr = d_Connection->Open(connStr, "", "", adModeUnknown);
    if (FAILED(hr))
    {
    AfxMessageBox("打开连接错误!");
    d_Connection->Close();
    d_Connection = NULL;
    ::CoUninitialize();
    return -1;
    }
    hr = d_Connection->Execute(query, &RecordsAffected, adCmdText);
    if (FAILED(hr))
    {
    AfxMessageBox("执行错误!");
    d_Connection->Close();
    d_Connection = NULL;
    ::CoUninitialize();
    return -1;
    }
    }
    catch (_com_error & e)
    {
    AfxMessageBox(e.ErrorMessage ());
    d_Connection->Close();
    d_Connection = NULL;
    ::CoUninitialize();
    return -1;
    }
    d_Connection->Close();
    d_Connection = NULL;
    ::CoUninitialize(); return 0;
    }
    // ************ ProcessData ****************
    //
    int ProcessData(char recv[])
    {
    ClientInfo* uInfo = (ClientInfo *)recv;

    if (uInfo != NULL)
    {
    switch (uInfo->infoType)
    {
    case T_REGISTER:
    UserRegister(*uInfo);
    break;
    case T_RESPONSE:
    break;
    case T_SEND:
    UserReceive(*uInfo);
    break;
    case T_QUIT:
    UserQuit(*uInfo);
    break;
    default:
    break;
    }
    }

    return 0;
    }
    // ************ ListenThread ***************
    //DWORD WINAPI ListenThread(LPVOID lpParam)
    {
    WSADATA wsd;
    SOCKET listen;
    SOCKADDR_IN local;
    SOCKADDR_IN remote;
    char * recvbuf = NULL;
    char szip[16];
    int ret;
    int dwSenderSize = sizeof(remote);
    int dwLength = 1024;

    recvbuf = new char[1024];
    memset(recvbuf, '\0', 1024);
    GetlocalIp(szip); if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
    {
    AfxMessageBox("网络初始化失败!");
    return -1;
    }
    listen = socket(AF_INET, SOCK_DGRAM, 0);

    local.sin_family = AF_INET;
    local.sin_port = htons((short)iPort);
    local.sin_addr.s_addr = inet_addr(szip); if (bind(listen, (SOCKADDR *)&local, sizeof(local)) == SOCKET_ERROR)
    {
    AfxMessageBox("绑定失败!");;
    }
    while (1)
    {
    ret = recvfrom(listen, recvbuf, dwLength, 0, (SOCKADDR *)&remote, &dwSenderSize);
    if (ret > 0)
    {
    recvbuf[ret] ='\0';
    } ProcessData(recvbuf);
    Sleep(100);
    } return 0;
    }
    // *************** OnLine ******************
    //
    int OnLine(alarmInfo * info)
    {
    __try
    { ::CoInitialize(NULL);
    _RecordsetPtr m_Recordset;
    _bstr_t query;
    _bstr_t connStr;
    HRESULT hr;
    char str[128];
    strcpy(str,"Select * from RiseOffice..OnLineUser where uname='");
    strcat(str,info->userName);
    strcat(str,"'");
    query=str;
    InitDbConn(connStr);
    try
    {
    hr=m_Recordset.CreateInstance (__uuidof(Recordset));
    if(FAILED(hr))
    {
    AfxMessageBox("创建数据集错误!");
    ::CoUninitialize();
    return -1;
    }
    hr=m_Recordset->Open(query,connStr,adOpenDynamic,adLockOptimistic,adCmdText);
    if(FAILED(hr))
    {
    AfxMessageBox("打开数据集错误");
    m_Recordset->Close();
    m_Recordset = NULL;
    ::CoUninitialize();
    return -1;
    }
    }
    catch (_com_error & e)
    {
    AfxMessageBox(e.ErrorMessage ());
    m_Recordset->Close();
    m_Recordset = NULL;
    ::CoUninitialize();
    return -1;
    } if (!(m_Recordset->adoEOF))
    {
    strcpy(info->userIp, (char*)_bstr_t(m_Recordset->GetCollect("ip"))); 
    m_Recordset->Close();
    m_Recordset = NULL;
    ::CoUninitialize();
    return 1;
    }

    m_Recordset->Close();
    m_Recordset = NULL;
    ::CoUninitialize();
    }
    __except(EXCEPTION_CONTINUE_EXECUTION)
    {}
    return 0;
    }
    // *************** SendMessage *****************
    //
    int SendMsg(const alarmInfo & info)
    {
    // 定义变量
    SOCKET sender;
    struct sockaddr_in remote;
    int ret; remote.sin_family = AF_INET;
    remote.sin_port = htons(lPort);
    remote.sin_addr.s_addr = inet_addr(info.userIp);

    sender = socket(AF_INET, SOCK_DGRAM, 0); ret = sendto(sender, (char*)&info, sizeof(info), 0, (SOCKADDR *)&remote, sizeof(remote));
    if (ret == SOCKET_ERROR)
    {
    AfxMessageBox("RegistUser->sender->sendto");
    } closesocket(sender); return 0;
    }
      

  6.   

    // *************** GetRecorder *****************
    //UINT GetRecorder(LPVOID m_hWnd_Dlg)
    { if(!g_DCheck)
    return 0;
    HWND m_hWndDlg=HWND(m_hWnd_Dlg);
    ::KillTimer(m_hWndDlg,1);
    ::CoInitialize(NULL); HRESULT hr;
    _RecordsetPtr m_Recordset;
    _bstr_t connStr;
    _bstr_t query=_T("Select * From RiseOffice..MobileMsg"); InitDbConn(connStr);

    try
    {
    hr=m_Recordset.CreateInstance (__uuidof(Recordset));
    if(FAILED(hr))
    {
    AfxMessageBox("创建数据集错误!");
    ::CoUninitialize();
    return -1;
    }
    hr=m_Recordset->Open(query,connStr,adOpenDynamic,adLockOptimistic,adCmdText);
    if(FAILED(hr))
    {
    AfxMessageBox("打开数据集错误");
    m_Recordset->Close();
    m_Recordset = NULL;
    ::CoUninitialize();
    return -1;
    }
    }
    catch (_com_error & e)
    {
    AfxMessageBox(e.ErrorMessage ());
    m_Recordset->Close();
    m_Recordset = NULL;
    ::CoUninitialize();
    return -1;
    } ////////////////////////
    int ret;
    char szNo[32];
    char szMsg[512];
    alarmInfo aInfo;
    try
    {
    while(!m_Recordset->adoEOF)
    {
    memset(&aInfo,'\0',sizeof(aInfo));
    aInfo.aIType = A_DATA;
    strcpy(aInfo.actionType, (char*)_bstr_t(m_Recordset->GetCollect("动作名称")));
    strcpy(aInfo.mobileNum, (char*)_bstr_t(m_Recordset->GetCollect("Mobile")));
    aInfo.status = atoi((char*)_bstr_t(m_Recordset->GetCollect("statue")));
    strcpy(aInfo.taskType, (char*)_bstr_t(m_Recordset->GetCollect("任务类型")));
    strcpy(aInfo.userName, (char*)_bstr_t(m_Recordset->GetCollect("name")));
    strcpy(aInfo.wFIIdx, (char*)_bstr_t(m_Recordset->GetCollect("Workflow_Instanceidx")));
    aInfo.wFInstance = atoi((char*)_bstr_t(m_Recordset->GetCollect("Workflow_Instance")));
    aInfo.workFlow = atoi((char*)_bstr_t(m_Recordset->GetCollect("WorkFlow_Id")));

    ret = OnLine(&aInfo);
    if (ret == 0 || (ret == 1 && aInfo.status <= 1))
    {
    if(g_DCheck)
    {
    strcpy(szNo,(char*)_bstr_t(m_Recordset->GetCollect("Mobile")));
    strcpy(szMsg,"您好,现有一份《");
    strcat(szMsg,(char*)_bstr_t(m_Recordset->GetCollect("任务类型")));
    strcat(szMsg,"》需要您");
    strcat(szMsg,(char*)_bstr_t(m_Recordset->GetCollect("动作名称")));
    strcat(szMsg,"。请速办理为盼!");

    if(int lRec = Sms_Send( szNo , szMsg , false , false , false ))
    {
    //更新状态
    m_Recordset->Fields->GetItem(_variant_t("statue"))->Value=_bstr_t("0");
    }
    //AfxMessageBox(szMsg);
    }
    else
    {
    break;
    }
    }

    if (ret == 1 && aInfo.status > 1)
    {
    aInfo.status--;
    m_Recordset->Fields->GetItem(_variant_t("statue"))->Value = _variant_t((short)aInfo.status); SendMsg(aInfo);
    }

    Sleep(20);

    m_Recordset->MoveNext();
    }
    m_Recordset->Close();
    m_Recordset = NULL;
    }
    catch(_com_error * e)
    {
    AfxMessageBox(e->ErrorMessage());
    m_Recordset->Close();
    m_Recordset = NULL;
    ::CoUninitialize();
    return -1;
    } //////////////结束返回////////
    m_Recordset->Close();
    m_Recordset = NULL;
    ::CoUninitialize();
    if (g_DCheck)
    ::SetTimer(m_hWndDlg,1,10000,NULL);
    return 0;
    }
      

  7.   

    to fhqiplj(水园MSN俱乐部理事) (霏霏) : 
       偶是新手,请指教,怎么处理日志,是在程序中生成日志文件?请给个思路好吗?谢谢,
       希望听到大家的批评声