Skip to content

Commit

Permalink
fix memory leak for functions accepting c-string arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Achille Roussel committed Jan 15, 2016
1 parent c8ee876 commit d38ff63
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ffi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ffi

// #include <ffi.h>
// #include <stdint.h>
// #include <stdlib.h>
//
// typedef void (*function)(void);
//
Expand Down Expand Up @@ -156,12 +157,23 @@ func Call(fptr unsafe.Pointer, ret interface{}, args ...interface{}) (err error)
argt := makeArgTypes(varg)
argv := makeArgValues(varg)

defer freeArgValues(argv, varg)

Prepare(rett, argt...).Call(fptr, retv, argv...)

setRetValue(vret, retv)
return
}

func freeArgValues(varg []unsafe.Pointer, args []reflect.Value) {
for i, a := range args {
switch a.Kind() {
case reflect.String:
C.free(*(*unsafe.Pointer)(varg[i]))
}
}
}

func valueOfRet(ret interface{}) reflect.Value {
v := reflect.ValueOf(ret)

Expand Down

0 comments on commit d38ff63

Please sign in to comment.