我已经制作了一个程序,我正在使用此代码再次更改picturebox的图像
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;
}
在这个函数中,我在这行写了//内存泄漏的内存泄漏。 我无法在此函数中释放此IplImage *背景,如果我将在此处发布此图像,那么我将无法在pitureBox中看到图像并且正在关闭此应用程序。
任何人都可以帮我解决这个问题。
感谢。