我在Wince5.0上编写了一个自己的VC程序 
最开始运行2个小时就死机了,我把new的地方,都相应的写上了delete 
但是现在运行100多小时,又死机了,改delete的地方都写了 
实在想不出哪个地方出错了,大家帮忙分析一下

解决方案 »

  1.   

    前阵子找程序的内存泄露找半天。。结果是调用GetTextExtentPoint32这个函数是传了个GetDC 进去。。忘记ReleaseDC
      

  2.   

    CARM_181View::CARM_181View()
    {
    // TODO: add construction code here
    // TODO: 在此处添加构造代码
    m_serialPort.OpenPort(this,_T("COM2:"),9600,8,1,0); //初始化串口设置
    m_initCmd = 0;
    m_hang = 100; // 当前行数
    m_lie = 100;  //当前列
    m_cmd = 0;  //当前命令
    m_counter = 0;
    for(int i=0;i<12;i++)
    for(int j=0;j<40;j++)
    m_arrayAll[12][40] = 32;//32为空格
    VERIFY(font.CreateFont(
    FONTHEIGHT,                 // nHeight
    14,                         // nWidth
    0,                         // nEscapement
    0,                         // nOrientation
    FW_NORMAL,                 // nWeight
    FALSE,                     // bItalic
    FALSE,                     // bUnderline
    0,                         // cStrikeOut
    ANSI_CHARSET,              // nCharSet
    OUT_RASTER_PRECIS,        // nOutPrecision
    CLIP_DEFAULT_PRECIS,       // nClipPrecision
    CLEARTYPE_QUALITY,  // nQuality
    FIXED_PITCH | FF_MODERN,  // nPitchAndFamily
    _T("Courer New")));                 // lpszFacename
    VERIFY(font_Chinese.CreateFont(
    FONTHEIGHT-2,                 // nHeight
    14,                         // nWidth
    0,                         // nEscapement
    0,                         // nOrientation
    FW_NORMAL,                 // nWeight
    FALSE,                     // bItalic
    FALSE,                     // bUnderline
    0,                         // cStrikeOut
    GB2312_CHARSET,              // nCharSet
    OUT_RASTER_PRECIS,        // nOutPrecision
    CLIP_DEFAULT_PRECIS,       // nClipPrecision
    DEFAULT_QUALITY,  // nQuality
    FIXED_PITCH | FF_MODERN,  // nPitchAndFamily
    _T("宋体")));                 // lpszFacename
    VERIFY(font_pDC.CreateFont(
    19,                // nHeight
    8,                        // nWidth
    0,                         // nEscapement
    0,                         // nOrientation
    FW_SEMIBOLD,                 // nWeight
    FALSE,                     // bItalic
    FALSE,                     // bUnderline
    0,                         // cStrikeOut
    CHINESEBIG5_CHARSET,       // nCharSet
    OUT_RASTER_PRECIS,         // nOutPrecision
    CLIP_DEFAULT_PRECIS,       // nClipPrecision
    DEFAULT_QUALITY,           // nQuality
    FIXED_PITCH | FF_MODERN,   // nPitchAndFamily
    _T("宋体")));              // lpszFacename
    pen.CreatePen(PS_SOLID, 2, RGB(255,0,0));
    }CARM_181View::~CARM_181View()
    {
    MMRESULT mmr=timeKillEvent(m_TimerID);
        if(mmr==TIMERR_NOERROR )
        {
            m_TimerID=NULL;
            mmr=timeEndPeriod(m_wTimerRes);
        }
    dcShow.Detach();
    }BOOL CARM_181View::PreCreateWindow(CREATESTRUCT& cs)
    {
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs return CView::PreCreateWindow(cs);
    }/////////////////////////////////////////////////////////////////////////////
    // CARM_181View drawingvoid CARM_181View::OnDraw(CDC* pDC)
    {
    CARM_181Doc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    // TODO: add draw code for native data here
    pDC->MoveTo(0,480);
    pDC->LineTo(642,480);
    pDC->LineTo(642,0);
    // pDC->MoveTo(0,80);
    // pDC->LineTo(642,80);
    pDC->MoveTo(39,0);
    pDC->LineTo(39,480);
    pDC->MoveTo(600,0);
    pDC->LineTo(600,480);
    int i = 0;
    for(i=0;i < 4;i++)   //画左边4个指示灯
    {
    pDC->Ellipse(5,(i+1)*80,35,(i+1)*80+30);
    }
    for(i=0;i < 4;i++)  //画右边4个指示灯
    {
    pDC->Ellipse(605,(i+1)*80,635,(i+1)*80+30);
    } int xLcd = 5;
    int yLcd = 120;
    pDC->ExtTextOut(xLcd,yLcd,ETO_CLIPPED,NULL,_T("关节"),NULL);
    pDC->ExtTextOut(xLcd,yLcd+80,ETO_CLIPPED,NULL,_T("直角"),NULL);
    pDC->ExtTextOut(xLcd,yLcd+160,ETO_CLIPPED,NULL,_T("工具"),NULL);
    pDC->ExtTextOut(xLcd,yLcd+240,ETO_CLIPPED,NULL,_T("用户"),NULL);
    xLcd = 605;
    pDC->ExtTextOut(xLcd,yLcd,ETO_CLIPPED,NULL,_T("高速"),NULL);
    pDC->ExtTextOut(xLcd,yLcd+80,ETO_CLIPPED,NULL,_T("中速"),NULL);
    pDC->ExtTextOut(xLcd,yLcd+160,ETO_CLIPPED,NULL,_T("低速"),NULL);
    pDC->ExtTextOut(xLcd,yLcd+240,ETO_CLIPPED,NULL,_T("微动"),NULL);
    // pDC->SelectObject(RGB(255,255,0));
    CPen pen(PS_SOLID, 2, RGB(0,255,i*10));
    pDC->SelectObject(&pen);
    for(i=0;i<24;i++)
    {
    pDC->MoveTo(40,i);
    pDC->LineTo(600,i);
    }

    // CString stradsf("如果设置为如果设置为如果设置为如果设置为");
    // pDC->SelectObject(&font);
    // pDC->ExtTextOut(40,240,ETO_CLIPPED,NULL,stradsf,NULL);
    pDC->SelectObject(&font_pDC);
    // CPen pen_pDC(PS_SOLID,3,RGB(0,255,0));
    // pDC->SelectObject(&pen_pDC);
    pDC->SetTextColor(RGB(10,255,190));
    pDC->ExtTextOut(5,20,ETO_CLIPPED,NULL,_T("坐标"),NULL);
    pDC->ExtTextOut(605,20,ETO_CLIPPED,NULL,_T("速度"),NULL);}/////////////////////////////////////////////////////////////////////////////
    // CARM_181View diagnostics
      

  3.   

    LRESULT CARM_181View::OnReceiveData(WPARAM ch1, LPARAM port) //接收到字符进行处理
    {
    //处理初始化的内容:收0x55,0xAA,发0x50,0x50,0xD0,0xD0
    unsigned char ch = (unsigned char)ch1;
    // dc.ExtTextOut(20,0,ETO_CLIPPED,NULL,"收到消息处理命令",NULL);
    if(m_initCmd < 2)
    {
    m_initCmd = 2;
    TransportInit(ch);
    return 0;
    }
    //接受其余的信息
    if(0x80<=ch&&ch<=0x88) //为命令操作码
    {
    m_cmd = ch;          //存储当前命令
    m_hang = 100;  //设置为初始的行号
    m_lie = 100;  //设置为初始的列号
    }
    switch (m_cmd)
    {
    case 0x80://80H----清屏
    ClearScreen_80(ch);
    break;
    case 0x81://81H,N1,N2-----光标移到N1行,N2列。 N1(0---15) N2(0---39)
    Show_81(ch);
    break;
    case 0x82://82H,N1----清N1行。     N1(0---15) 
    ClearHang_82(ch);
    break;
    case 0x83://83H,N1,N2,N3-----在N1行,N2列反显(N3为长度)。
    Show_83(ch);
    break;
    case 0x84://84H,N1,N2,N3-----在N1行,N2列正显(N3为长度)。
    Show_84(ch);
    break;
    case 0x85://85H,N1,N2-----在N1行N2列显示光标。
    ShowCaret_85(ch);
    break;
    case 0x86://86H----关闭光标。
    ShutCaret_86(ch);
    break;
    case 0x87://87H,L1,L2----控制灯。
    ControlLight_87(ch);
    break;
    case 0x88://88H,M1----LCD对比度。M1(0---7)
    Lcd_88(ch);
    break;
    default:
    break;
    // MessageBox(_T("error Cmd_view.cppOnReceiveData"));
    }
    return 0;
    }void CARM_181View::ClearScreen_80(WPARAM ch)
    {
    for(int i=0;i<12;i++)
    for(int j=0;j<40;j++)
    m_arrayAll[i][j] = 0x32;
    for(int k=0;k<12;k++)
    ShowLine(k);
    }void CARM_181View::Show_81(WPARAM ch)
    {
    unsigned char ch1 = (unsigned char)ch;
    if(ch != 0x81)
    {
    if(m_hang == 100)             //设置行号和列号
    m_hang = ch;
    else if(m_lie == 100)
    m_lie = ch;
    else
    {
    // m_caretPoint.x = 16+16*m_lie; //显示光标
    // m_caretPoint.y = 33*m_hang+82;
    // SetCaretPos(m_caretPoint);
    // ShowCaret();
    m_arrayAll[m_hang][m_lie] = ch;
    m_counter++;
    if(m_counter == 40)    //40个字符接收完成
    {
    ShowLine(m_hang);
    m_counter = 0;
    }
    m_lie++;
    }
    }
    }void CARM_181View::ClearHang_82(WPARAM ch)
    {
    if((ch != 0x82)&&(m_hang == 100))
    {
    m_hang = ch; //要清除的行数
    for(int i = 0; i<40; i++)
    {
    m_arrayAll[m_hang][i] = 0x20;
    }
    ShowLine(m_hang);
    }
    }
    void CARM_181View::Show_83(WPARAM ch)
    {
    CSize sz;
    CString strTemp("");
    int i=0;
    CClientDC dc(this);
    dc.SelectObject(&font);
    if((ch != 0x83)&&(m_hang == 100))
    m_hang = ch;
    else if((ch != 0x83)&&(m_lie == 100))
    m_lie = ch;
    else if(ch != 0x83)  //处理反显问题,ch为反显的长度
    {
    char *CharPre = new char[m_lie+1];  //计算前面显示的长度
    for(i = 0;i<m_lie;i++)
    {
    CharPre[i] = m_arrayAll[m_hang][i];
    }
    CharPre[m_lie] = 0;
    //从ASCII转换到UNICODE
    int unicodeLen1 = MultiByteToWideChar(CP_ACP,0,CharPre,-1,NULL,0); 
    wchar_t *pUnicode1;       
    pUnicode1 = new wchar_t[unicodeLen1+1];       
    memset(pUnicode1,0,(unicodeLen1+1)*sizeof(wchar_t));           
    MultiByteToWideChar(936,0,CharPre,-1,(LPWSTR)pUnicode1,unicodeLen1);
    strTemp.Empty();
    strTemp.Format(_T("%s"),pUnicode1);
    // strTemp = strTemp.Mid(0,unicodeLen1-1);
    sz = dc.GetTextExtent(strTemp);
    // delete CharPre;
    char *All = new char[ch+1];
    All[ch] = 0;
    for(i = m_lie;i< m_lie+ch;i++)
    {
    All[i-m_lie] = m_arrayAll[m_hang][i]; // 存储要反显的内容
    } //从ASCII转换到UNICODE
    int unicodeLen = MultiByteToWideChar(CP_ACP,0,All,-1,NULL,0); 
    wchar_t *pUnicode;       
    pUnicode = new wchar_t[unicodeLen+1];       
    memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));           
    MultiByteToWideChar(936,0,All,-1,(LPWSTR)pUnicode,unicodeLen); strTemp.Empty();
    strTemp.Format(_T("%s"),pUnicode);
    // strTemp = strTemp.Mid(0,unicodeLen-1);
    dc.SetTextColor(RGB(255,255,255)); //白笔
    dc.SetBkColor(RGB(0,0,0));         //黑背景
    dc.ExtTextOut(sz.cx+40,FONTHEIGHT*m_hang+BLANK,ETO_CLIPPED,NULL,strTemp,NULL);
    delete pUnicode;
    delete pUnicode1;
    delete CharPre;
    delete All;
    m_cmd = 0;
    }}void CARM_181View::Show_84(WPARAM ch)
    {
    CSize sz;
    CString strTemp("");
    CClientDC dc(this);
    dc.SelectObject(&font);
    if((ch != 0x84)&&(m_hang == 100))
    m_hang = ch;
    else if((ch != 0x84)&&(m_lie == 100))
    m_lie = ch;
    else if(ch != 0x84)  //处理反显问题,ch为反显的长度
    {
    strTemp.Empty();
    int i = 0;
    char *CharPre = new char[m_lie+1]; //计算前面显示的长度
    for(i = 0;i<m_lie;i++)
    {
    CharPre[i] = m_arrayAll[m_hang][i];
    }
    CharPre[m_lie] = 0;
    //从ASCII转换到UNICODE
    int unicodeLen1 = MultiByteToWideChar(CP_ACP,0,CharPre,-1,NULL,0); 
    wchar_t *pUnicode1;       
    pUnicode1 = new wchar_t[unicodeLen1+1];       
    memset(pUnicode1,0,(unicodeLen1+1)*sizeof(wchar_t));           
    MultiByteToWideChar(936,0,CharPre,-1,(LPWSTR)pUnicode1,unicodeLen1);
    strTemp.Empty();
    strTemp.Format(_T("%s"),pUnicode1);
    // strTemp = strTemp.Mid(0,unicodeLen1-1);
    sz = dc.GetTextExtent(strTemp);
    // delete CharPre; char *All = new char[ch+1];
    All[ch] = 0;
    for(i = m_lie;i< m_lie+ch;i++)
    {
    All[i-m_lie] = m_arrayAll[m_hang][i]; // 存储要反显的内容
    }
    //从ASCII转换到UNICODE
    int unicodeLen = MultiByteToWideChar(CP_ACP,0,All,-1,NULL,0); 
    wchar_t *pUnicode;       
    pUnicode = new wchar_t[unicodeLen+1];       
    memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));           
    MultiByteToWideChar(936,0,All,-1,(LPWSTR)pUnicode,unicodeLen); strTemp.Empty();
    strTemp.Format(_T("%s"),pUnicode);
    // strTemp = strTemp.Mid(0,unicodeLen-1);
    dc.SetTextColor(RGB(0,0,0)); //黑笔
    dc.SetBkColor(RGB(255,255,255));         //白背景
    dc.ExtTextOut(sz.cx+40,FONTHEIGHT*m_hang+BLANK,ETO_CLIPPED,NULL,strTemp,NULL);
    delete CharPre;
    delete pUnicode1;
    delete pUnicode;
    delete All;
    m_cmd = 0;
    }}void CARM_181View::ShowCaret_85(WPARAM ch)
    {
    if(ch != 0x85 && m_hang==100) //设置行
    {
    m_hang = ch;
    }
    else if(m_hang != 100 && m_lie == 100)//设置列
    {
    m_lie = ch;
    m_caretPoint.x = 16*m_lie;
    m_caretPoint.y = 33*m_hang+82;
    SetCaretPos(m_caretPoint);
    ShowCaret();
    }
    }void CARM_181View::ShutCaret_86(WPARAM ch)
    {
    HideCaret();
    }
    void CARM_181View::ControlLight_87(WPARAM ch)
    {
    if(ch == 0x55 || ch == 0xAA)
    return;
    if(ch != 0x87)//不是命令时
    {
    if(m_hang == 100)
    {
    m_hang = ch&0x0000F;
    }
    else if(m_lie == 100)   //命令传输完成
    {
    m_lie = ch&0x0000F;
    LcdShow();
    }
    }
    }void CARM_181View::Lcd_88(WPARAM ch)
    {

    }void CARM_181View::ShowLine(int num)
    {
    // Sleep(10);
    // ReadEvent.SetEvent();
    int i = 0;
    for(i=0;i<40;i++)
    {
    AllShow[i] = (char)m_arrayAll[num][i];
    }
    //从ASCII转换到UNICODE
    int unicodeLen = MultiByteToWideChar(CP_ACP,0,AllShow,-1,NULL,0); 
    wchar_t *pUnicode;       
    pUnicode = new wchar_t[unicodeLen+1];
    memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));           
    MultiByteToWideChar(936,0,AllShow,-1,(LPWSTR)pUnicode,unicodeLen);

    /* CString strTemp("");
    strTemp.Format(_T("%s"),pUnicode);
    strTemp = strTemp.Mid(0,unicodeLen-1);   //除去多余的部分
    CString str(strTemp);
    */ CString strSpace("                                        ");   //清除原先的显示
    dcShow.ExtTextOut(40,FONTHEIGHT*num+BLANK,ETO_CLIPPED,NULL,strSpace,NULL);
    CString str(pUnicode);
    // str.Format(_T("%s"),pUnicode);
    // CClientDC dc(this);
    // dc.SelectObject(&font);// if(m_cmd == 0x82 || m_cmd == 0x80)           
    // {
    // dcShow.ExtTextOut(317,FONTHEIGHT*num,ETO_CLIPPED,NULL,str,NULL);
    // }
    // if(m_cmd == 0x81)   
    // {
    // dcShow.ExtTextOut(0,33*num+82,ETO_CLIPPED,NULL,str,NULL);
    // }
    // Sleep(10);
    // dcShow.SelectObject(&font); int  width = 40;
    if(num == 11)
    {
    CBrush m_brush(RGB(200,200,200));
    CRect rect_temp(width,FONTHEIGHT*num+BLANK,600,FONTHEIGHT*num+BLANK+40);
    dcShow.FillRect(&rect_temp,&m_brush); dcShow.SetBkColor(RGB(200,200,200));
    dcShow.SelectObject(&font_Chinese);
    for(i = 0;i<unicodeLen-1;i++)
    {
    dcShow.ExtTextOut(width,FONTHEIGHT*num+BLANK,ETO_CLIPPED,NULL,str[i],NULL);
    if((int)str[i]>0x3400)
    width += 28;
    else
    width += 14;
    }
    dcShow.SetBkColor(RGB(255,255,255));
    /* dcShow.SelectObject(&font_Chinese);
    dcShow.ExtTextOut(40,FONTHEIGHT*num+BLANK,ETO_CLIPPED,NULL,str,NULL);
    dcShow.SelectObject(&font);
    */
    dcShow.SelectObject(&font);
    return;
    }

    dcShow.ExtTextOut(40,FONTHEIGHT*num+BLANK,ETO_CLIPPED,NULL,str,NULL);
    if((num == 0) && (m_arrayAll[0][21] == 0xA1) && (m_arrayAll[0][22] == 0xF1))    //解决黑圈问题
    {
    /* CString str111(str,17);
    CSize sz;
    sz = dcShow.GetTextExtent(str111);
    dcShow.SelectObject(&font_Chinese);
    dcShow.ExtTextOut(40,50,ETO_CLIPPED,NULL,str[18],NULL);
    dcShow.ExtTextOut(40+sz.cx,FONTHEIGHT*num+BLANK,ETO_CLIPPED,NULL,str[19],NULL);
    dcShow.SelectObject(&font);*/
    CSize sz;
    for(int i=0;i<30;i++)
    {
    if(pUnicode[i] == 9679)
    break;
    }
    CString str111(pUnicode,i);
    sz = dcShow.GetTextExtent(str111);
    CClientDC dc1(this);
    CBrush brush(RGB(0,250,0));
    dc1.SelectObject(&brush);
    dc1.Ellipse(sz.cx+41,BLANK+6,sz.cx+61,BLANK+26);
    }
    delete pUnicode;
    m_cmd = 0;
    }void CARM_181View::LcdShow()
    {
    CClientDC dc(this);
    int i = 0;
    for(i=0;i < 4;i++)   //画左边4个指示灯
    {
    dc.Ellipse(5,(i+1)*80,35,(i+1)*80+30);
    }
    for(i=0;i < 4;i++)  //画右边4个指示灯
    {
    dc.Ellipse(605,(i+1)*80,635,(i+1)*80+30);
    }
    //显示现在要显示的
    CBrush brush(RGB(255,0,0));
    dc.SelectObject(&brush);
    if(m_hang == 0x04)     //转换列
    m_hang = 3;
    if(m_hang == 0x08)
    m_hang = 4;
    if(m_lie == 0x04)
    m_lie = 3;
    if(m_lie == 0x08)
    m_lie = 4;
    // m_lie = m_lie-1;
    if((m_hang == 0)||(m_lie == 0))
    return;
    m_hang = 4-m_hang;
    dc.Ellipse(5,(m_lie)*80,35,(m_lie)*80+30);
    dc.Ellipse(605,(m_hang+1)*80,635,(m_hang+1)*80+30);
    }
      

  4.   


    void CARM_181View::OnInitialUpdate() 
    {
    CView::OnInitialUpdate();

    // TODO: Add your specialized code here and/or call the base class
    CreateSolidCaret(10,1);
    AllShow[40] = 0;
    // CeSetThreadPriority(this,251);
    TIMECAPS tc;
    MMRESULT mmr=timeGetDevCaps(&tc,sizeof(TIMECAPS)); //获取系统最小时间间隔
    m_wTimerRes=min(tc.wPeriodMax,tc.wPeriodMin);
    mmr=timeBeginPeriod(m_wTimerRes); //设置最小时间分辨率
    m_TimerID = timeSetEvent(21,m_wTimerRes,CallBackFunc,(DWORD)this,TIME_PERIODIC);
    dcShow.Attach(*this->GetDC());
    dcShow.SelectObject(&font);
    // dcShow.SelectObject(&pen);
    HANDLE m_DispThread= AfxBeginThread(DisplayThread,this);
    if( !m_DispThread )return ;
    CeSetThreadPriority(m_DispThread,200);
    }
    void CARM_181View::TransportInit(WPARAM ch1)
    {
    unsigned char ch = (unsigned char)ch1;
    if(ch == 0x55)
    {
    m_initCmd++;
    }
    else if(ch == 0xAA)
    {
    m_initCmd++;
    if(m_initCmd == 2) 
    {
    char a[4];                 //可能出错
    a[0] = 0x50;
    a[1] = 0x50;
    a[2] = 0xD0;
    a[3] = 0xD0;
    m_serialPort.OnSend(a,4);
    // CClientDC dc(this);
    // dc.ExtTextOut(20,0,ETO_CLIPPED,NULL,"收到初始化命令",NULL);
    }
    }
    // if(m_initCmd > 2)
    // MessageBox(_T("处理初始化的内容错误:收0x55,0xAA,发0x50,0x50,0xD0,0xD0"));
    }UINT DisplayThread(LPVOID pParam)
    {
    CARM_181View* pView = (CARM_181View*)pParam;
    int counter;
    while(TRUE)
    {
    counter = 0;
    WaitForSingleObject(DisplayEvent.m_hObject,INFINITE);
    while((pRead != pWrite) && (counter<50))
    {
    counter++;
    critical_section.Lock();
    pView->OnReceiveData(pRead->ReadQueue(pRead),2);
    pRead = pRead->GetAfterP();
    critical_section.Unlock();
    }
    }
    }
    void PASCAL    CallBackFunc   (UINT uID, // Timer identifier
                                   UINT uMsg,       // Not currently used
                                   DWORD dwUser,    // User information
                                   DWORD dw1,       // Windows reserved
                                   DWORD dw2) // is also reserved
    {
    /* static int IsRead = 0;
    if(IsRead == 3)
    IsRead =0;
    if(IsRead != 2)
    {
    IsRead++;
    ReadEvent.SetEvent();
    }
    else
    {
    IsRead++;
    // ReadEvent.SetEvent();
    DisplayEvent.SetEvent();
    }*/
    static bool IsRead = TRUE;
    if(IsRead == TRUE)
    {
    IsRead = FALSE;
    ReadEvent.SetEvent();
    }
    else
    {
    IsRead = TRUE;
    DisplayEvent.SetEvent();
    // ReadEvent.SetEvent();
    }/* CClientDC dc(pView);
    static float time = 0;
    time += 0.05;
    CString str("");
    str.Format(_T("%.2f"),time);
    dc.ExtTextOut(200,100,ETO_CLIPPED,NULL,str,NULL);*/
    }
      

  5.   

    这是view类中的全部代码,所有的显示都是在这里实现的
    写的有点乱
      

  6.   

    CClientDC   dc(this)  
     
    DeleteDC(dc);
      

  7.   

    你这个问题还真是不太清楚,再补充一点, CreatePen, CreateFont等函数,最后最好调用一下 DeleteObject,这是释放函数。New不delete会造成内存耗尽
    GetDC不ReleaseDC会造成再也不能绘图
    CreateBrush不DeleteObject,会造成资源的耗尽。程序死掉,这个原因太多,一时看不出来。
      

  8.   

    有测试了一次,还是100多个小时就死机了
    操作系统应该没有死,应为点击开始还能够弹出菜单,但是不能弹出二级菜单
    详细看了一下,系统的内存是8k,8k,8k...的往上涨的,wince的颗粒度不是4k吗?
      

  9.   

    主要的是下面几个函数,帮忙看看有没有问题
    void CARM_181View::ShowLine(int num)
    {
    // Sleep(10);
    // ReadEvent.SetEvent();
    int i = 0;
    for(i=0;i<40;i++)
    {
    AllShow[i] = (char)m_arrayAll[num][i];
    }
    //从ASCII转换到UNICODE
    int unicodeLen = MultiByteToWideChar(CP_ACP,0,AllShow,-1,NULL,0); 
    wchar_t *pUnicode;       
    pUnicode = new wchar_t[unicodeLen+1];
    memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));           
    MultiByteToWideChar(936,0,AllShow,-1,(LPWSTR)pUnicode,unicodeLen);

    /* CString strTemp("");
    strTemp.Format(_T("%s"),pUnicode);
    strTemp = strTemp.Mid(0,unicodeLen-1);   //除去多余的部分
    CString str(strTemp);
    */ CString strSpace("                                        ");   //清除原先的显示
    dcShow.ExtTextOut(40,FONTHEIGHT*num+BLANK,ETO_CLIPPED,NULL,strSpace,NULL);
    CString str(pUnicode);
    // str.Format(_T("%s"),pUnicode);
    // CClientDC dc(this);
    // dc.SelectObject(&font);// if(m_cmd == 0x82 || m_cmd == 0x80)           
    // {
    // dcShow.ExtTextOut(317,FONTHEIGHT*num,ETO_CLIPPED,NULL,str,NULL);
    // }
    // if(m_cmd == 0x81)   
    // {
    // dcShow.ExtTextOut(0,33*num+82,ETO_CLIPPED,NULL,str,NULL);
    // }
    // Sleep(10);
    // dcShow.SelectObject(&font); int  width = 40;
    if(num == 11)
    {
    CBrush m_brush(RGB(200,200,200));
    CRect rect_temp(width,FONTHEIGHT*num+BLANK,600,FONTHEIGHT*num+BLANK+40);
    dcShow.FillRect(&rect_temp,&m_brush); dcShow.SetBkColor(RGB(200,200,200));
    dcShow.SelectObject(&font_Chinese);
    for(i = 0;i<unicodeLen-1;i++)
    {
    dcShow.ExtTextOut(width,FONTHEIGHT*num+BLANK,ETO_CLIPPED,NULL,str[i],NULL);
    if((int)str[i]>0x3400)
    width += 28;
    else
    width += 14;
    }
    dcShow.SetBkColor(RGB(255,255,255));
    /* dcShow.SelectObject(&font_Chinese);
    dcShow.ExtTextOut(40,FONTHEIGHT*num+BLANK,ETO_CLIPPED,NULL,str,NULL);
    dcShow.SelectObject(&font);
    */
    dcShow.SelectObject(&font);
    delete []pUnicode;
    return;
    }

    dcShow.ExtTextOut(40,FONTHEIGHT*num+BLANK,ETO_CLIPPED,NULL,str,NULL);
    if((num == 0) && (m_arrayAll[0][21] == 0xA1) && (m_arrayAll[0][22] == 0xF1))    //解决黑圈问题
    {
    /* CString str111(str,17);
    CSize sz;
    sz = dcShow.GetTextExtent(str111);
    dcShow.SelectObject(&font_Chinese);
    dcShow.ExtTextOut(40,50,ETO_CLIPPED,NULL,str[18],NULL);
    dcShow.ExtTextOut(40+sz.cx,FONTHEIGHT*num+BLANK,ETO_CLIPPED,NULL,str[19],NULL);
    dcShow.SelectObject(&font);*/
    CSize sz;
    for(int i=0;i<30;i++)
    {
    if(pUnicode[i] == 9679)
    break;
    }
    CString str111(pUnicode,i);
    sz = dcShow.GetTextExtent(str111);
    CClientDC dc1(this);
    CBrush brush(RGB(0,250,0));
    dc1.SelectObject(&brush);
    dc1.Ellipse(sz.cx+41,BLANK+6,sz.cx+61,BLANK+26);
    }
    delete []pUnicode;
    m_cmd = 0;
    }
      

  10.   

    void CARM_181View::Show_83(WPARAM ch)
    {
    CSize sz;
    CString strTemp("");
    int i=0;
    CClientDC dc(this);
    dc.SelectObject(&font); if((ch != 0x83)&&(m_hang == 100))
    m_hang = ch;
    else if((ch != 0x83)&&(m_lie == 100))
    m_lie = ch;
    else if(ch != 0x83)  //处理反显问题,ch为反显的长度
    {
    char *CharPre = new char[m_lie+1];  //计算前面显示的长度
    for(i = 0;i<m_lie;i++)
    {
    CharPre[i] = m_arrayAll[m_hang][i];
    }
    CharPre[m_lie] = 0;
    //从ASCII转换到UNICODE
    int unicodeLen1 = MultiByteToWideChar(CP_ACP,0,CharPre,-1,NULL,0); 
    wchar_t *pUnicode1;       
    pUnicode1 = new wchar_t[unicodeLen1+1];       
    memset(pUnicode1,0,(unicodeLen1+1)*sizeof(wchar_t));           
    MultiByteToWideChar(936,0,CharPre,-1,(LPWSTR)pUnicode1,unicodeLen1);
    strTemp.Empty();
    strTemp.Format(_T("%s"),pUnicode1);
    // strTemp = strTemp.Mid(0,unicodeLen1-1);
    sz = dc.GetTextExtent(strTemp);
    // delete CharPre;
    char *All = new char[ch+1];
    All[ch] = 0;
    for(i = m_lie;i< m_lie+ch;i++)
    {
    All[i-m_lie] = m_arrayAll[m_hang][i]; // 存储要反显的内容
    } //从ASCII转换到UNICODE
    int unicodeLen = MultiByteToWideChar(CP_ACP,0,All,-1,NULL,0); 
    wchar_t *pUnicode;       
    pUnicode = new wchar_t[unicodeLen+1];       
    memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));           
    MultiByteToWideChar(936,0,All,-1,(LPWSTR)pUnicode,unicodeLen); strTemp.Empty();
    strTemp.Format(_T("%s"),pUnicode);
    // strTemp = strTemp.Mid(0,unicodeLen-1);
    dc.SetTextColor(RGB(255,255,255)); //白笔
    dc.SetBkColor(RGB(0,0,0));         //黑背景
    dc.ExtTextOut(sz.cx+40,FONTHEIGHT*m_hang+BLANK,ETO_CLIPPED,NULL,strTemp,NULL);
    delete []pUnicode;
    delete []All;
    delete []pUnicode1;
    delete []CharPre;
    m_cmd = 0;
    }}void CARM_181View::Show_84(WPARAM ch)
    {
    CSize sz;
    CString strTemp("");
    CClientDC dc(this);
    dc.SelectObject(&font);
    if((ch != 0x84)&&(m_hang == 100))
    m_hang = ch;
    else if((ch != 0x84)&&(m_lie == 100))
    m_lie = ch;
    else if(ch != 0x84)  //处理反显问题,ch为反显的长度
    {
    strTemp.Empty();
    int i = 0;
    char *CharPre = new char[m_lie+1]; //计算前面显示的长度
    for(i = 0;i<m_lie;i++)
    {
    CharPre[i] = m_arrayAll[m_hang][i];
    }
    CharPre[m_lie] = 0;
    //从ASCII转换到UNICODE
    int unicodeLen1 = MultiByteToWideChar(CP_ACP,0,CharPre,-1,NULL,0); 
    wchar_t *pUnicode1;       
    pUnicode1 = new wchar_t[unicodeLen1+1];       
    memset(pUnicode1,0,(unicodeLen1+1)*sizeof(wchar_t));           
    MultiByteToWideChar(936,0,CharPre,-1,(LPWSTR)pUnicode1,unicodeLen1);
    strTemp.Empty();
    strTemp.Format(_T("%s"),pUnicode1);
    // strTemp = strTemp.Mid(0,unicodeLen1-1);
    sz = dc.GetTextExtent(strTemp);
    // delete CharPre; char *All = new char[ch+1];
    All[ch] = 0;
    for(i = m_lie;i< m_lie+ch;i++)
    {
    All[i-m_lie] = m_arrayAll[m_hang][i]; // 存储要反显的内容
    }
    //从ASCII转换到UNICODE
    int unicodeLen = MultiByteToWideChar(CP_ACP,0,All,-1,NULL,0); 
    wchar_t *pUnicode;       
    pUnicode = new wchar_t[unicodeLen+1];       
    memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));           
    MultiByteToWideChar(936,0,All,-1,(LPWSTR)pUnicode,unicodeLen); strTemp.Empty();
    strTemp.Format(_T("%s"),pUnicode);
    // strTemp = strTemp.Mid(0,unicodeLen-1);
    dc.SetTextColor(RGB(0,0,0)); //黑笔
    dc.SetBkColor(RGB(255,255,255));         //白背景
    dc.ExtTextOut(sz.cx+40,FONTHEIGHT*m_hang+BLANK,ETO_CLIPPED,NULL,strTemp,NULL);
    delete []pUnicode;
    delete []All;
    delete []pUnicode1;
    delete []CharPre;
    m_cmd = 0;
    }}
      

  11.   

    终于查到原因了,因为有个函数里面:
    CClientDC dc1(this); 
    CBrush brush(RGB(0,250,0)); 
    dc1.SelectObject(&brush); 
    dc1.Ellipse(sz.cx+41,BLANK+6,sz.cx+61,BLANK+26); 
    哪位能够给我解释一下为什么会内存泄露?
      

  12.   

    人家不都告诉你了吗??
    你没有ReleaseDC和DeleteObject
      

  13.   


    改为以下代码试试,dc用完以后要恢复到原来的配置,如果brush选入dc的话,brush对象不会释放
    其他类似的地方也要这样处理,如果还不行,就不知道了CClientDC dc1(this);
    CBrush brush(RGB(0,250,0));
    CBrush *pOldBrush = dc1.SelectObject(&brush);
    dc1.Ellipse(sz.cx+41,BLANK+6,sz.cx+61,BLANK+26);dc1.SelectObject(pOldBrush );
      

  14.   

    To Conry:
    我也试了一下,这样还是不行,在pc机上也有内存泄露
      

  15.   

    在经常用到CClientDC dc1、CBrush brush的地方,声明成全局变量。
    WinCE的内存机制与Win98/2k/xp不同,因为它常用于嵌入式设备上,嵌入式设备多数是小内存。如果你反复开辟内存,比如不断地new,delete,尽管你都做了delete,但内存的“碎片”会越积越多,尤其是在CPU速度慢的设备上,系统还没来得及delete,你又new了。非数组变量也是同样,比如函数内的局部变量,你调用该函数的次数越多越快,内存的“碎片”就越多,所以还是多用全局变量。
    你的CClientDC dc1、CBrush brush占用内存又更多,如果经常调用、快速调用,内存会慢慢消耗掉!
      

  16.   

    我之前也是这样,以为只要有new的地方有DELETE就没关系,写的一个程序,显示一个3*2的小方块,显示8个,没用ReleaseDC 跑了半天提示内存不足