在正在运行的程序中查找问题 [英] Finding a problem in a running program

查看:86
本文介绍了在正在运行的程序中查找问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以编译和运行的程序,除了几个依赖于初始化图像(特别是位图)的弹出菜单项.我可以找到生成错误消息的代码部分,但是由于没有语法错误,所以我不确定为什么它不起作用.如果有人认为他们可以提供帮助,我可以发布代码的某些部分....如果没有,则为如何识别哪些类和函数相互依赖提供了一个很好的技巧,以便我可以跟踪代码在做什么将会有很大的帮助.我正在使用VS2010.

以下是弹出菜单中导致位图问题的代码之一:

I have a program that compiles and runs with the exception of a couple of pop-up menu items that are dependent on an image (bitmap, specifically) being initialized. I can find the part of the code that generates the error message, but since there are no syntax errors I am not quite sure why this isn''t working. If anyone thinks they might be able to help I can post some parts of the code....if not, a good tip on how to identify what classes and functions are dependent on each other so that I can trace what the code is doing would be great help. I am using VS2010.

Here is the code from one of the entries in the pop-up menu that leads to the bitmap problem:

void CWinSTMView::OnImageLinDcOffset()
{
    CWinSTMDoc *pDoc = CreateNewDoc(GetDocument());
    strcat(pDoc->m_ChanHead[0]->AppliedFilters,"Linear DC Offset. ");

    int ave;
    //linear DC subtract
    for(int i=0;i< m_NScans;i++)
        {
            ave = 0;
            for(int j=0;j< m_NPts;j++)
            {
                ave += pDoc->m_Chan[0]->ipData[j+i*m_NPts];
            }
            ave /= m_NPts;

            for(int j = 0;j< m_NPts;j++)
                {
                    pDoc->m_Chan[0]->ipData[j+i*m_NPts] -= ave;
                }
        }
    m_Change = 1;
    Invalidate(true);

}



上面的代码未执行,导致执行以下代码:我认为程序的这一部分来自AfxMessageBox("Bitmap has not been init");:



The above code does not execute, it leads to the following code execution: AfxMessageBox("Bitmap has not been init"); from I think this part of the program:

#include "stdafx.h"
#include "winstmbmp.h"

CWinSTMBmp::CWinSTMBmp(void)
{
    ZeroMemory(&m_HistogramData,sizeof(int)*256);
    m_ImageMode     = IMAGE_MODE_HORIZONTAL;
    m_Inversion     = false;
    m_Derivative    = false;
    m_LineDC        = false;
    m_Render        = false;
    m_Bitmap        = NULL;
    m_BmpBits       = NULL;
    m_SizeX         = 0;
    m_SizeY         = 0;

}

CWinSTMBmp::~CWinSTMBmp(void)
{
    if(m_Bitmap != NULL)
        DeleteObject(m_Bitmap);
}

bool CWinSTMBmp::Init(int sizex, int sizey, RGBQUAD* Pal)
{
    // if bmp already exists, delete it and start over
    if (m_Bitmap != NULL)
        DeleteObject(m_Bitmap);

    // create Bitmap in memory
    if (m_Bitmap == NULL)
    {
        ZeroMemory ( &m_ImageInfo, sizeof (BITMAPINFO) );
        m_ImageInfo.bmiHeader.biSize        = sizeof (BITMAPINFOHEADER);
        m_ImageInfo.bmiHeader.biWidth       = sizex;
        m_ImageInfo.bmiHeader.biHeight      = -sizey;
        m_ImageInfo.bmiHeader.biPlanes      = 1;
        m_ImageInfo.bmiHeader.biBitCount    = 32;
        m_ImageInfo.bmiHeader.biCompression = BI_RGB;
        m_ImageInfo.bmiHeader.biSizeImage   = 0;

        m_Bitmap=CreateDIBSection ( NULL, &m_ImageInfo, DIB_RGB_COLORS, (void **)&m_BmpBits, NULL, NULL );
    }

    // copy pallet
    memcpy(m_RGB,Pal,sizeof(RGBQUAD)*256);

    m_SizeX = sizex;
    m_SizeY = sizey;

    return true;
}

HBITMAP CWinSTMBmp::GetHBitmap(void)
{
    return HBITMAP(m_Bitmap);
}

BITMAPINFO* CWinSTMBmp::GetBmpInfo(void)
{
    return &m_ImageInfo;
}

bool CWinSTMBmp::Render(int* Data,int Min, int Max)
{
    if(Data == NULL)
    {
        AfxMessageBox("Passing in a NULL Data Ptr");
        return false;
    }

    if(m_Bitmap == NULL)
    {
        AfxMessageBox("Bitmap has not been init");
        return false;
    }

    // Only the Render Function sets these
    m_Min = Min;
    m_Max = Max;

    //clear Histogram data
    ZeroMemory(m_HistogramData,sizeof(int)*256);

    int temp; // color index

    // Keep range, but remove DC component
    if(m_LineDC)
    {
        double LRange = (Max + Min)/2;
        Max -= LRange;
        Min -= LRange;
    }

    //calculate Color Scale factor
    double ColorScale = 256.0 / (Max - Min);

    int SlowScanInc, FastScanInc,SlowMax,FastMax;

    if(m_ImageMode == IMAGE_MODE_HORIZONTAL)
    {
        SlowScanInc = m_SizeX;
        FastScanInc = 1;
        SlowMax     = m_SizeY;
        FastMax     = m_SizeX;
    }
    else // vertical
    {
        SlowScanInc = 1;
        FastScanInc = m_SizeX;
        SlowMax     = m_SizeX;
        FastMax     = m_SizeY;
    }


    for(int K = 0; K < SlowMax; K++)
    {
        int slow = K * SlowScanInc;

        double Ave = 0.0;

        if(m_LineDC)
        {
            for(int J = 0; J < FastMax; J++)
            {
                int fast = J * FastScanInc;
                int index = slow+fast;
                if(!m_Derivative)
                    Ave += Data[index];
                if(m_Derivative && J != 0)
                    Ave += Data[index - FastScanInc] - Data[index];

            }

            if(m_ImageMode == IMAGE_MODE_HORIZONTAL)
                Ave /= m_SizeX;
            else
                Ave /= m_SizeY;
        }//End if m_LinDC


        for(int J = 0; J < FastMax; J++)
        {
            int temp=0;
            int fast = J * FastScanInc;
            int index = slow+fast;

            if(!m_Derivative) // if topo/image mode
            {
                temp = round((Data[index] - Min - Ave) * ColorScale);
            }

            if(m_Derivative && J != 0)  //if Deriv
            {
                temp = round((Data[index - FastScanInc] - Data[index] - Min - Ave) * ColorScale);
            }


            temp = min(temp,255);
            temp = max(temp,0);
            m_HistogramData[temp]++;

            if(!m_Inversion)
            {
                m_BmpBits[index] = m_RGB[255-temp];
            }
            else
            {
                m_BmpBits[index] = m_RGB[temp];
            }

        }// end for
    } // end for

    return true;
}

RGBQUAD* CWinSTMBmp::GetBmpBits(void)
{
    return m_BmpBits;
}

void CWinSTMBmp::SetLineDC(bool enable)
{
    m_LineDC = enable;
}

void CWinSTMBmp::SetInversion(bool enable)
{
    m_Inversion = enable;
}

void CWinSTMBmp::SetDer(bool enable)
{
    m_Derivative = enable;
}

void CWinSTMBmp::UpdatePallet(RGBQUAD* Pal)
{
    // copy pallet
    memcpy(m_RGB,Pal,sizeof(RGBQUAD)*256);
}

int* CWinSTMBmp::GetHistogramData(void)
{
    return m_HistogramData;
}

// Sets all rendering parameters
void CWinSTMBmp::SetProperties(bool Derivative, bool LineDC, int Direction, bool Invert)
{
    m_Inversion = Invert;
    m_Derivative = Derivative;
    m_LineDC    = LineDC;
    m_ImageMode = Direction;
}

void CWinSTMBmp::DeleteBmp(void)
{
    if(m_Bitmap != NULL)
        DeleteObject(m_Bitmap);

    ZeroMemory(&m_HistogramData,sizeof(int)*256);
    m_ImageMode     = IMAGE_MODE_HORIZONTAL;
    m_Inversion     = false;
    m_Derivative    = false;
    m_LineDC        = false;
    m_Render        = false;
    m_Bitmap        = NULL;
    m_BmpBits       = NULL;
    m_SizeX         = 0;
    m_SizeY         = 0;

}


// render an unsigned short array
bool CWinSTMBmp::RenderUI(unsigned short* Data, int Min, int Max)
{
    if(Data == NULL)
    {
        AfxMessageBox("Passing in a NULL Data Ptr");
        return false;
    }

    if(m_Bitmap == NULL)
    {
        AfxMessageBox("Bitmap has not been init");
        return false;
    }

    // Only the Render Function sets these
    m_Min = Min;
    m_Max = Max;

    //clear Histogram data
    ZeroMemory(m_HistogramData,sizeof(int)*256);

    int temp; // color index

    // Keep range, but remove DC component
    if(m_LineDC)
    {
        double LRange = (Max + Min)/2;
        Max -= LRange;
        Min -= LRange;
    }

    //calculate Color Scale factor
    double ColorScale = 256.0 / (Max - Min);

    int SlowScanInc, FastScanInc,SlowMax,FastMax;

    if(m_ImageMode == IMAGE_MODE_HORIZONTAL)
    {
        SlowScanInc = m_SizeX;
        FastScanInc = 1;
        SlowMax     = m_SizeY;
        FastMax     = m_SizeX;
    }
    else // vertical
    {
        SlowScanInc = 1;
        FastScanInc = m_SizeX;
        SlowMax     = m_SizeX;
        FastMax     = m_SizeY;
    }


    for(int K = 0; K < SlowMax; K++)
    {
        int slow = K * SlowScanInc;

        double Ave = 0.0;

        if(m_LineDC)
        {
            for(int J = 0; J < FastMax; J++)
            {
                int fast = J * FastScanInc;
                int index = slow+fast;
                if(!m_Derivative)
                    Ave += Data[index];
                if(m_Derivative && J != 0)
                    Ave += Data[index - FastScanInc] - Data[index];

            }

            if(m_ImageMode == IMAGE_MODE_HORIZONTAL)
                Ave /= m_SizeX;
            else
                Ave /= m_SizeY;
        }//End if m_LinDC


        for(int J = 0; J < FastMax; J++)
        {
            int temp=0;
            int fast = J * FastScanInc;
            int index = slow+fast;

            if(!m_Derivative) // if topo/image mode
            {
                temp = round((Data[index] - Min - Ave) * ColorScale);
            }

            if(m_Derivative && J != 0)  //if Deriv
            {
                temp = round((Data[index - FastScanInc] - Data[index] - Min - Ave) * ColorScale);
            }


            temp = min(temp,255);
            temp = max(temp,0);
            m_HistogramData[temp]++;

            if(!m_Inversion)
            {
                m_BmpBits[index] = m_RGB[255-temp];
            }
            else
            {
                m_BmpBits[index] = m_RGB[temp];
            }

        }// end for
    } // end for

    return true;
}

bool CWinSTMBmp::RenderMetaBmp(int * Data, int Min, int Max)
{
    if(Data == NULL)
    {
        AfxMessageBox("Passing in a NULL Data Ptr");
        return false;
    }

    if(m_Bitmap == NULL)
    {
        AfxMessageBox("Bitmap has not been init");
        return false;
    }

    if(Min == 0 && Max == 0)
    {
        Min = m_Min;
        Max = m_Max;
    }

    //clear Histogram data
    ZeroMemory(m_HistogramData,sizeof(int)*256);

    int temp; // color index

    // Keep range, but remove DC component
    if(m_LineDC)
    {
        double LRange = (Max + Min)/2;
        Max -= LRange;
        Min -= LRange;
    }

    //calculate Color Scale factor
    double ColorScale = 256.0 / (Max - Min);

    int SlowScanInc, FastScanInc,SlowMax,FastMax;

    if(m_ImageMode == IMAGE_MODE_HORIZONTAL)
    {
        SlowScanInc = m_SizeX;
        FastScanInc = 1;
        SlowMax     = m_SizeY;
        FastMax     = m_SizeX;
    }
    else // vertical
    {
        SlowScanInc = 1;
        FastScanInc = m_SizeX;
        SlowMax     = m_SizeX;
        FastMax     = m_SizeY;
    }


    for(int K = 0; K < SlowMax; K++)
    {
        int slow = K * SlowScanInc;

        double Ave = 0.0;

        if(m_LineDC)
        {
            for(int J = 0; J < FastMax; J++)
            {
                int fast = J * FastScanInc;
                int index = slow+fast;
                if(!m_Derivative)
                    Ave += Data[index];
                if(m_Derivative && J != 0)
                    Ave += Data[index - FastScanInc] - Data[index];

            }

            if(m_ImageMode == IMAGE_MODE_HORIZONTAL)
                Ave /= m_SizeX;
            else
                Ave /= m_SizeY;
        }//End if m_LinDC


        for(int J = 0; J < FastMax; J++)
        {
            int temp=0;
            int fast = J * FastScanInc;
            int index = slow+fast;

            if(!m_Derivative) // if topo/image mode
            {
                temp = round((Data[index] - Min - Ave) * ColorScale);
            }

            if(m_Derivative && J != 0)  //if Deriv
            {
                temp = round((Data[index - FastScanInc] - Data[index] - Min - Ave) * ColorScale);
            }


            temp = min(temp,255);
            temp = max(temp,0);
            m_HistogramData[temp]++;

            if(!m_Inversion)
            {
                m_BmpBits[(SlowMax-1)*SlowScanInc-slow +fast] = m_RGB[255-temp];
            }
            else
            {
                m_BmpBits[(SlowMax-1)*SlowScanInc-slow +fast] = m_RGB[temp];
            }

        }// end for
    } // end for

    return true;
}

void CWinSTMBmp::NewLUT(RGBQUAD* newRGB)
{
    memcpy(m_RGB,newRGB,sizeof(RGBQUAD)*256);

}



当我关闭正在运行的程序中的窗口后,出现一条错误消息,提示遇到了不正确的参数".我还没有在代码中找到这个.另外,当我在调试模式下运行它时,文件"f:\ dd \ vctools \ vc7libs \ ship \ atlmfc \ src \ mfc \ filelist.cpp"在行:225上显示"Debug Assertion Failed!". br/>
使用CLR和运行时异常引发"运行时,请参见以下代码部分,其转折点为:



An error message appears after I close the window in the running program saying, ''encountered an improper argument''. I haven''t found this yet in the code. Also, when I run it in debug mode I get a ''Debug Assertion Failed!'' for File: f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\filelist.cpp, at Line: 225.

Running with CLR and Run Time Exception Throws refers to the following section of code, with a break point at:

WinSTM.exe!CWinSTMDoc::CopyDoc(CWinSTMDoc * pDocNew, int chan)  Line 486 + 0x3b bytes   C++
WinSTM.exe!CWinSTMView::CreateNewDoc(CWinSTMDoc * pDoc)  Line 2717  C++
WinSTM.exe!CWinSTMView::OnImageLinDcOffset()  Line 5052 + 0x11 bytes    C++



这些调用引用以下代码:



These calls refer to the following code:

void CWinSTMView::OnImageLinDcOffset()
{
    CWinSTMDoc *pDoc = CreateNewDoc(GetDocument());


CreateNewDoc代码参考:


CreateNewDoc code reference:

if(((CWinSTMApp*)AfxGetApp())->m_Default.PmtForSave)
            pNewDoc->SetModifiedFlag(true);


CopyDoc代码参考:


CopyDoc code reference:

CString OldPath = GetPathName();
	pDocNew->SetPathName(OldPath + " *");
	
	CString OldTitle = GetTitle();
	pDocNew->SetTitle(OldTitle + " *");
	pDocNew->m_Ready = true;


这使我回到了开始的代码.但是,我仍然不确定到底出了什么问题.

感谢所有的建议!我希望这对正在跟踪程序错误的其他人有所帮助.

最好,

Andrew


This brings me back to the code at the beginning. However, I am still not sure exactly what is going wrong.

Thanks for all the advice! I hope this is helpful for anyone else who is tracking down a bug in a program.

Best,

Andrew

推荐答案

一个可能有用的技巧是注释掉大部分代码,然后在调试器上运行应用程序(可以选择按比例处理所有异常),它是可行的,然后添加更多代码,然后重试.

确保已启用所有运行时检查.

修改您的消息,以便您知道哪个消息框调用都会显示错误.

添加更多验证并检查您的代码,以帮助验证所有功能均按预期工作.使用断言.

由于您使用原始数组和指针,因此请确保正确完成所有索引.
One trick that might help is to comment out most of the code then run the application on the debugger (with the option to srop on all exceptions), it is works, then add some more code and try again.

Ensure that all run-time checks are enabled.

Modify your message so that you knows which message box call display the error.

Add more validation and check to your code to help validated everything works as expected. Uses assertions.

Since you works with raw arrays and pointers, ensure that all your indexing is properly done.


阅读并理解错误消息!


您可以尝试使用 Log4Net [
You could try instrumenting the code using Log4Net[^], or someting similar. It''s also worth running the application under the debugger, enabling break on throw for CLR exceptions ...

Best regards
Espen Harlinn


这篇关于在正在运行的程序中查找问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆