Skip to content

Commit

Permalink
Fix memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Danile71 committed Aug 30, 2019
1 parent 19b4e91 commit ecf6b47
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
1 change: 0 additions & 1 deletion cgo/ffmpeg/ffmpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ int wrap_avcodec_encode_jpeg(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket
avcodec_close(jpegContext);
return -1;
}

avcodec_close(jpegContext);
return 0;
}
Expand Down
9 changes: 5 additions & 4 deletions cgo/ffmpeg/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ func fromCPtr(buf unsafe.Pointer, size int) (ret []uint8) {

type VideoFrame struct {
Image image.YCbCr
frame *C.AVFrame
Raw []byte
Size int
}

func (self *VideoFrame) Free() {
self.Image = image.YCbCr{}
C.av_frame_free(&self.frame)
self.Raw = make([]byte, 0)
}

func freeVideoFrame(self *VideoFrame) {
Expand Down Expand Up @@ -81,7 +80,7 @@ func (self *VideoDecoder) Decode(pkt []byte) (img *VideoFrame, err error) {
CStride: cs,
SubsampleRatio: image.YCbCrSubsampleRatio420,
Rect: image.Rect(0, 0, w, h),
}, frame: frame}
}}
runtime.SetFinalizer(img, freeVideoFrame)

packet := C.AVPacket{}
Expand All @@ -93,8 +92,10 @@ func (self *VideoDecoder) Decode(pkt []byte) (img *VideoFrame, err error) {
tmp := *(*[]byte)(unsafe.Pointer(&packet.data))
img.Raw = make([]byte, img.Size)
copy(img.Raw, tmp)
tmp = nil
}

C.av_frame_free(&frame)
C.av_free_packet(&packet)
}

return
Expand Down

0 comments on commit ecf6b47

Please sign in to comment.