doujing5726 2017-03-14 06:34 采纳率: 0%
浏览 50
已采纳

在Go应用程序中查找内存泄漏

I've got a simple program that reads frames off a webcam and puts pointers to them on a channel in a goroutine. Another portion reads the frame pointers from the channel and pops them onto another queue. This other queue then does some additional processing to them, and when this additional processing is done, a bunch of memory gets allocated that apparently never gets garbage collected. I'm in over my head trying to figure out why:

This function reads the frames off the channel in a loop:

func (mr *OpenCVMotionRunner) Run() error {
log.Println("Starting motion detection... ")

// inMotion := false
win := opencv.NewWindow("GoOpenCV: VideoPlayer")
defer win.Destroy()

test := mr.md.(*CV2FrameDiffMotionDetector)
for {
    f := mr.imageChan.PopFrame()
    mr.frame = nil
    switch f := f.(type) {
    default:
        return fmt.Errorf("Unknown frame type")
    case *BSFrame:
        mr.frame = f.ToOpenCVFrame()
    case *OpenCVFrame:
        mr.frame = f
    }
    f = nil
    test.SetCurrent(mr.frame)
    delta := test.Delta()
    // win.ShowImage(mr.frame.image)
    if delta != nil {
        win.ShowImage(delta)
        opencv.WaitKey(1)
    } else {
        fmt.Println("wtf")
    }
}

return nil

}

It appears that the problematic part is this specifically:

mr.frame = f.ToOpenCVFrame()

This is the type and the method:

// Frame with byte slice image
type BSFrame struct {
    image  []byte
    Time   time.Time
    Width  uint32
    Height uint32
}

func (f *BSFrame) ToOpenCVFrame() *OpenCVFrame {
    img := opencv.DecodeImageMem(f.image)
    return &OpenCVFrame{
        image:  img,
        Time:   f.Time,
        Width:  f.Width,
        Height: f.Height,
    }
}

This is the destination type:

type OpenCVFrame struct {
    image  *opencv.IplImage
    Time   time.Time
    Width  uint32
    Height uint32
}

PopFrame, FYI, is just a method on an type that aliases a channel:

type BSFrameChan chan *BSFrame

func (fc BSFrameChan) PopFrame() (frame Frame) {
    frame = <-fc
    return
}

Hopefully this is enough code for the problem to present itself. The rest is here if needed.

  • 写回答

1条回答 默认 最新

  • dongluoheng3324 2017-03-15 06:23
    关注

    The go library I'm using for the OpenCV bindings provides a Release method on images to deallocate memory when done with an image. This fixes the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试