2

我制作了一个程序,在该程序中我再次使用此代码更改图片框的图像

while(1)
{
    this->pictureBox1->Refresh();
    System::Drawing::Bitmap ^bmp;
    bmp=drawImageMeter(data.Max);
    this->pictureBox1->Image =pictureBox1->Image->FromHbitmap(bmp->GetHbitmap());
    delete bmp;
}

drawImageMeter(data.Max);是制作位图并返回它的函数。

我的问题是它在这条线上显示内存泄漏

this->pictureBox1->Image =pictureBox1->Image->FromHbitmap(bmp->GetHbitmap());

英特尔 Vtune 在同一行显示此泄漏:

P810        Memory leak form2.h  Form1.exe  808 New

P34     Kernel resource leak    form2.h  Form1.exe      Not fixed

编辑代码...

delete pictureBox1->Image;
pictureBox1->Image=nullptr;
this->pictureBox1->Refresh();                   
bmp=drawImageMeter(data.Max);
System::IntPtr hbitmap = bmp->GetHbitmap();
this->pictureBox1->Image =pictureBox1->Image->FromHbitmap(hbitmap);//Memory LEAK & Kernel Resource Leak
delete bmp;
DeleteObject((HGDIOBJ)hbitmap );

之后我没有得到 GDI 资源泄漏但仍然在这条线上得到内存泄漏..

this->pictureBox1->Image =pictureBox1->Image->FromHbitmap(hbitmap);//Memory LEAK

drawImageMeter() 定义

System::Drawing::Bitmap^ drawImageMeter(float intensity_value)
{
    IplImage  *Background=cvLoadImage("Dialer.bmp", 1); //Memory Leak
        int width,height;
        width=Background->width;
        height=Background->height;
        if(counter==1)
        {
            counter++;
        needle_center.x=width/2;
        needle_center.y=height/2; 
        needle_top.x=needle_center.x;
        needle_top.y=needle_center.y-140;
        }
        double const PI = 3.14159265358979323;
           int x1 = needle_top.x; 
           int y1 = needle_top.y;

           int x0=needle_center.x;
           int y0=needle_center.y;
           float angle;
            CurrIntensity = intensity_value;
            angle = CurrIntensity-PreIntensity;
            angle= 0.0703125f * angle;
           // degrees, not radians
           float radians = angle * (PI / 180.0f);   // convert degrees to radians
           if (current_max==1)
            {
                current_max++;
                int N1x1 = needle_top.x; 
                int N1y1 = needle_top.y;
                needle1_top.x = ((N1x1-x0) * cos(radians)) - ((N1y1-y0) * sin(radians)) + x0; 
                needle1_top.y = ((N1x1-x0) * sin(radians)) + ((N1y1-y0) * cos(radians)) + y0;
            }
           needle_top.x = ((x1-x0) * cos(radians)) - ((y1-y0) * sin(radians)) + x0; 
           needle_top.y = ((x1-x0) * sin(radians)) + ((y1-y0) * cos(radians)) + y0;

           cvLine(Background, needle_center, needle1_top, CV_RGB(0, 0, 255), 1, 4, 0);

           cvLine(Background, needle_center, needle_top, CV_RGB(255, 0, 0), 1, 4, 0);
         System::Drawing::Bitmap ^bmp = gcnew System::Drawing::Bitmap(Background->width,Background->height,Background->widthStep,System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr)Background->imageData);
         PreIntensity = CurrIntensity;
         return bmp;
    }

在这个函数中,我在写 //Memory Leak. 如果我将在此处发布此图像,我将无法在此函数中发布此 IplImage *Background,因此我将无法在 PitureBox 中看到图像并且正在关闭此应用程序。

谁能帮我解决这个问题。

谢谢。

4

0 回答 0