Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Commit

Permalink
Handle destruction in MediaInfo Finalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
zhulik committed Dec 13, 2015
1 parent dd7189f commit a08be32
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
1 change: 0 additions & 1 deletion go_mediainfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const char *toChar(const wchar_t *c)
}

void *GoMediaInfo_New() {
setlocale(LC_CTYPE, "");
return MediaInfo_New();
}

Expand Down
14 changes: 10 additions & 4 deletions mediainfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,31 @@ package mediainfo
import "C"

import (
"unsafe"
"errors"
"fmt"
"runtime"
"unsafe"
)

type MediaInfo struct {
handle unsafe.Pointer
}

func init() {
C.setlocale(C.LC_CTYPE, C.CString(""))
C.MediaInfoDLL_Load()

if C.MediaInfoDLL_IsLoaded() == 0 {
panic("Cannot load mediainfo")
}
}

func NewMediaInfo() (*MediaInfo) {
return &MediaInfo{handle: C.GoMediaInfo_New()}
func NewMediaInfo() *MediaInfo {
result := &MediaInfo{handle: C.GoMediaInfo_New()}
runtime.SetFinalizer(result, func(f *MediaInfo) {
C.GoMediaInfo_Delete(f.handle)
})
return result
}

func (mi *MediaInfo) Open(path string) error {
Expand All @@ -41,4 +47,4 @@ func (mi *MediaInfo) Close() {

func (mi *MediaInfo) Get(param string) string {
return C.GoString(C.GoMediaInfoGet(mi.handle, C.CString(param)))
}
}

0 comments on commit a08be32

Please sign in to comment.