3d0c/gmf

Memory leak

Closed this issue · 3 comments

VLZH commented

Hello! I have some problem. And I can't resolve it. When I use sws_scale i have a memory leak.
In examples, I show only the main part of the code.

// reader.go
// ...
func (v *VFile) GetFrame() *gmf.Frame {
	f := gmf.NewFrame().
				SetWidth(960).
				SetHeight(480).
				SetFormat(gmf.AV_PIX_FMT_YUV420P)
	if err := f.ImgAlloc(); err != nil {
		log.Fatal(err)
	}
	return f
}
//...
for _, f := range frames {
	tmpFrame = v.GetFrame()
	v.SwsContext.Scale(f, tmpFrame)
	tmpFrame.SetPts(f.Pts())
	tmpFrame.SetPktPts(f.PktPts())
	tmpFrame.SetPktDts(f.PktDts())
	scaledFrames = append(scaledFrames, tmpFrame)
	f.Free() // free source frame
}
// ...

In next part i send scaledFrames through channel to another file(writer.go), create packages from frames:

//writer.go
//....
packets, err = stream.CodecCtx().Encode(f.Frames, f.Flush)
// I know, that in method Encode frames will be released
//...

And as result, i have memory leak. If do not scale frames and only clone data, memory will not leak.

// reader.go (without memory leak)
// ...
for _, f := range frames {
	// example without leak
	tmpFrame = v.GetFrame()
	tmpFrame.Free()
	// example
	tmpFrame = f.CloneNewFrame()
	tmpFrame.SetPts(f.Pts())
	tmpFrame.SetPktPts(f.PktPts())
	tmpFrame.SetPktDts(f.PktDts())
	scaledFrames = append(scaledFrames, tmpFrame)
	f.Free()
}
// ...

I have not checked how work example. Maybe i have problem by wrong version ffmpeg. If i will find some answers i write information bellow.
P.S Sorry for my English)

VLZH commented

Another example with memory leak:

for _, f := range frames {
	tmpFrame = v.GetFrame()
	v.SwsContext.Scale(f, tmpFrame)
	tmpFrame.SetPts(f.Pts())
	tmpFrame.SetPktPts(f.PktPts())
	tmpFrame.SetPktDts(f.PktDts())
	tmpFrame.Free()
	scaledFrames = append(scaledFrames, f) // f is not scaled frame
	// f.Free()
}
3d0c commented

Various memory leaks has been fixed with #101

VLZH commented

@3d0c Thank you!