@@ -31,19 +31,17 @@ import (
3131// Returns the JSON message if decoding successful, otherwise an error.
3232func Decode (xdrTypeName XdrType , xdrBinary []byte ) (json.RawMessage , error ) {
3333 var jsonStr , errStr string
34- // scope just added to show matching alloc/frees
35- {
36- goRawXdr := CXDR (xdrBinary )
37- b := C .CString (string (xdrTypeName ))
34+ goRawXdr := CXDR (xdrBinary )
35+ defer FreeGoXDR (goRawXdr )
3836
39- result := C .xdr_to_json ( b , goRawXdr )
40- C .free (unsafe .Pointer (b ))
37+ b := C .CString ( string ( xdrTypeName ) )
38+ defer C .free (unsafe .Pointer (b ))
4139
42- jsonStr = C .GoString ( result . json )
43- errStr = C . GoString (result . error )
40+ result : = C .xdr_to_json ( b , goRawXdr )
41+ defer C . free_conversion_result (result )
4442
45- C . free_conversion_result (result )
46- }
43+ jsonStr = C . GoString (result . json )
44+ errStr = C . GoString ( result . error )
4745
4846 if errStr != "" {
4947 return json .RawMessage (jsonStr ), errors .New (errStr )
@@ -59,3 +57,7 @@ func CXDR(xdr []byte) C.xdr_t {
5957 len : C .size_t (len (xdr )),
6058 }
6159}
60+
61+ func FreeGoXDR (xdr C.xdr_t ) {
62+ C .free (unsafe .Pointer (xdr .xdr ))
63+ }
0 commit comments