Make sure we free our data made by C.CString. Also, updated cgo comments to multiline - seems to be preferred style

This commit is contained in:
Donovan Jimenez 2011-01-16 14:40:30 -05:00
parent 690531c87e
commit 20aa6d93c3
2 changed files with 17 additions and 3 deletions

View File

@ -1,6 +1,9 @@
package iconv
// #include <iconv.h>
/*
#include <iconv.h>
#include <stdlib.h>
*/
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 {

View File

@ -1,6 +1,8 @@
package iconv
// #include <errno.h>
/*
#include <errno.h>
*/
import "C"
import (