From 20aa6d93c3c44adba1f050265e9cbf6babfd6a93 Mon Sep 17 00:00:00 2001 From: Donovan Jimenez Date: Sun, 16 Jan 2011 14:40:30 -0500 Subject: [PATCH] Make sure we free our data made by C.CString. Also, updated cgo comments to multiline - seems to be preferred style --- converter.go | 16 ++++++++++++++-- iconv.go | 4 +++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/converter.go b/converter.go index 4413847..e6596db 100644 --- a/converter.go +++ b/converter.go @@ -1,6 +1,9 @@ package iconv -// #include +/* +#include +#include +*/ import "C" import ( @@ -16,7 +19,16 @@ type Converter struct { func NewConverter(fromEncoding string, toEncoding string) (converter *Converter, err Error) { converter = new(Converter) - converter.context, err = C.iconv_open(C.CString(toEncoding), C.CString(fromEncoding)) + // create C strings + toEncodingC := C.CString(toEncoding) + fromEncodingC := C.CString(fromEncoding) + + // open an iconv descriptor + converter.context, err = C.iconv_open(toEncodingC, fromEncodingC) + + // free the C Strings + C.free(unsafe.Pointer(toEncodingC)) + C.free(unsafe.Pointer(fromEncodingC)) // check err if err == nil { diff --git a/iconv.go b/iconv.go index 0d65d97..5d1a2f6 100644 --- a/iconv.go +++ b/iconv.go @@ -1,6 +1,8 @@ package iconv -// #include +/* +#include +*/ import "C" import (