1

我在图片框中制作了一个强度计。为了制作这个强度计,我使用了一张图片作为拨号器(Dialer.bmp)并使用线制作针。我正在使用 openCV 执行此操作。并更改指针指针,我们在表单加载时创建了一个线程。代码如下

private: System::Void FocusExposure_Load(System::Object^  sender, System::EventArgs^  e) {
 if(ExposureThreadStatus)
                     th->Abort();
                 th = gcnew Thread(gcnew ThreadStart(this,&FocusExposure::UpdateIntensity)); 
                th->Start();
                ExposureThreadStatus = true;
                }


void UpdateIntensity()
{
Intensity_Values data;
 while(1)
 {

 data = ReadImage(focusBuffer);
    System::Drawing::Bitmap ^bmp=drawImageMeter(data.Max);
     this->pictureBox1->Image =this->pictureBox1->Image->FromHbitmap(bmp->GetHbitmap());
     delete bmp;
     Sleep(1000);                   
 }
}

 System::Drawing::Bitmap^ drawImageMeter(float intensity_value)
{
        IplImage  *Background =cvLoadImage("Dialer.bmp", 1);
        int width,height;
        if(counter==1)
        {
        width=Background->width;
           height=Background->height;
            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;

}

此代码工作正常,并根据我的要求提供输出。但唯一的问题是,当我打开表单时,它会导致内存泄漏。我在任务管理器中看到过,也使用过 Intel Vtune 分析器。此分析器在以下行显示不匹配的分配/解除分配

IplImage *Background =cvLoadImage("Dialer.bmp", 1);

我们需要重新加载此图像,因为我们正在图像上绘制线条,并且当​​指针指针发生变化时,它需要没有指针的拨号器图像。

任何人都可以向我建议任何解决此内存泄漏问题的解决方案。

任何帮助将不胜感激。

4

1 回答 1

3

似乎drawImageMeter()不止一次被调用。问题是每次cvLoadImage()执行时,它都会在 HEAP 上为像素分配空间。因此,在显示图像后,您应该释放它,cvReleaseImage()以便释放数据。

一个快速而肮脏(和可怕)的解决方法是制作variablestatic

static IplImage* Background =cvLoadImage("Dialer.bmp", 1);

但是你真的应该改变你的应用程序的设计,所以Background它只分配一次。为此,您可以将其设为全局变量并将其加载到main()方法中,或者将其设为以下参数drawImageMeter()

System::Drawing::Bitmap^ drawImageMeter(IplImage* Background, float intensity_value)
{
   // code
}
于 2012-09-25T17:52:47.347 回答