rosylilly / hpdf

a libharu binding for golang
MIT License
7 stars 3 forks source link

free(): invalid pointer #6

Closed Ortuna closed 9 years ago

Ortuna commented 9 years ago

*** Error in `/tmp/go-build060701743/_/home/ortuna/code/hpdf/_test/hpdf.test': free(): invalid pointer: 0x0000000001f03374 ***
======= Backtrace: =========
/usr/lib/libc.so.6(+0x7198e)[0x7f41663f498e]
/usr/lib/libc.so.6(+0x76dee)[0x7f41663f9dee]
/usr/lib/libc.so.6(+0x775cb)[0x7f41663fa5cb]
/tmp/go-build060701743/_/home/ortuna/code/hpdf/_test/hpdf.test[0x43fb15]
======= Memory map: ========
00400000-0063c000 r-xp 00000000 00:23 885086                             /tmp/go-build060701743/_/home/ortuna/code/hpdf/_test/hpdf.test
0083c000-00853000 rw-p 0023c000 00:23 885086                             /tmp/go-build060701743/_/home/ortuna/code/hpdf/_test/hpdf.test
00853000-0086e000 rw-p 00000000 00:00 0 
01e87000-01f0c000 rw-p 00000000 00:00 0                                  [heap]
c000000000-c000001000 rw-p 00000000 00:00 0 
c207ff0000-c208100000 rw-p 00000000 00:00 0 
7f4160000000-7f4160021000 rw-p 00000000 00:00 0 
7f4160021000-7f4164000000 ---p 00000000 00:00 0 
7f4164e66000-7f4164e7c000 r-xp 00000000 00:11 1286761                    /usr/lib/libgcc_s.so.1
7f4164e7c000-7f416507b000 ---p 00016000 00:11 1286761                    /usr/lib/libgcc_s.so.1
7f416507b000-7f416507c000 rw-p 00015000 00:11 1286761                    /usr/lib/libgcc_s.so.1
7f416507c000-7f416507d000 ---p 00000000 00:00 0 
7f416507d000-7f416587d000 rw-p 00000000 00:00 0                          [stack:18307]
7f416587d000-7f416587e000 ---p 00000000 00:00 0 
7f416587e000-7f416607e000 rw-p 00000000 00:00 0                          [stack:18306]
7f416607e000-7f4166181000 r-xp 00000000 00:11 2255407                    /usr/lib/libm-2.21.so
7f4166181000-7f4166381000 ---p 00103000 00:11 2255407                    /usr/lib/libm-2.21.so
7f4166381000-7f4166382000 r--p 00103000 00:11 2255407                    /usr/lib/libm-2.21.so
7f4166382000-7f4166383000 rw-p 00104000 00:11 2255407                    /usr/lib/libm-2.21.so
7f4166383000-7f416651c000 r-xp 00000000 00:11 2255361                    /usr/lib/libc-2.21.so
7f416651c000-7f416671c000 ---p 00199000 00:11 2255361                    /usr/lib/libc-2.21.so
7f416671c000-7f4166720000 r--p 00199000 00:11 2255361                    /usr/lib/libc-2.21.so
7f4166720000-7f4166722000 rw-p 0019d000 00:11 2255361                    /usr/lib/libc-2.21.so
7f4166722000-7f4166726000 rw-p 00000000 00:00 0 
7f4166726000-7f416673e000 r-xp 00000000 00:11 2255381                    /usr/lib/libpthread-2.21.so
7f416673e000-7f416693d000 ---p 00018000 00:11 2255381                    /usr/lib/libpthread-2.21.so
7f416693d000-7f416693e000 r--p 00017000 00:11 2255381                    /usr/lib/libpthread-2.21.so
7f416693e000-7f416693f000 rw-p 00018000 00:11 2255381                    /usr/lib/libpthread-2.21.so
7f416693f000-7f4166943000 rw-p 00000000 00:00 0 
7f4166943000-7f4166958000 r-xp 00000000 00:11 7414                       /usr/lib/libz.so.1.2.8
7f4166958000-7f4166b57000 ---p 00015000 00:11 7414                       /usr/lib/libz.so.1.2.8
7f4166b57000-7f4166b58000 r--p 00014000 00:11 7414                       /usr/lib/libz.so.1.2.8
7f4166b58000-7f4166b59000 rw-p 00015000 00:11 7414                       /usr/lib/libz.so.1.2.8
7f4166b59000-7f4166b8e000 r-xp 00000000 00:11 39649                      /usr/lib/libpng16.so.16.16.0
7f4166b8e000-7f4166d8d000 ---p 00035000 00:11 39649                      /usr/lib/libpng16.so.16.16.0
7f4166d8d000-7f4166d8e000 r--p 00034000 00:11 39649                      /usr/lib/libpng16.so.16.16.0
7f4166d8e000-7f4166d8f000 rw-p 00035000 00:11 39649                      /usr/lib/libpng16.so.16.16.0
7f4166d8f000-7f4166e45000 r-xp 00000000 00:11 3458838                    /usr/lib/libhpdf-2.3.0.so
7f4166e45000-7f4167044000 ---p 000b6000 00:11 3458838                    /usr/lib/libhpdf-2.3.0.so
7f4167044000-7f416704a000 r--p 000b5000 00:11 3458838                    /usr/lib/libhpdf-2.3.0.so
7f416704a000-7f416704b000 rw-p 000bb000 00:11 3458838                    /usr/lib/libhpdf-2.3.0.so
7f416704b000-7f416706d000 r-xp 00000000 00:11 2255403                    /usr/lib/ld-2.21.so
7f41670a0000-7f4167245000 rw-p 00000000 00:00 0 
7f416725a000-7f416726c000 rw-p 00000000 00:00 0 
7f416726c000-7f416726d000 r--p 00021000 00:11 2255403                    /usr/lib/ld-2.21.so
7f416726d000-7f416726e000 rw-p 00022000 00:11 2255403                    /usr/lib/ld-2.21.so
7f416726e000-7f416726f000 rw-p 00000000 00:00 0 
7ffc9fabd000-7ffc9fade000 rw-p 00000000 00:00 0                          [stack]
7ffc9fb18000-7ffc9fb1a000 r--p 00000000 00:00 0                          [vvar]
7ffc9fb1a000-7ffc9fb1c000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
SIGABRT: abort
PC=0x7f41663b64b7
signal arrived during cgo execution

goroutine 36 [syscall, locked to thread]:
runtime.cgocall_errno(0x404f60, 0xc208018e80, 0xc200000000)
    /usr/lib/go/src/runtime/cgocall.go:130 +0xf5 fp=0xc208018e60 sp=0xc208018e38
_/home/ortuna/code/hpdf._Cfunc_free(0x1f03374)
    /home/ortuna/code/hpdf/:876 +0x45 fp=0xc208018e80 sp=0xc208018e60
_/home/ortuna/code/hpdf.(*PDF).LoadTTFontFromFile(0xc208038258, 0x5b4630, 0x22, 0xc208018f01, 0xc20800a870, 0xf, 0x0, 0x0)
    /home/ortuna/code/hpdf/font.go:64 +0x117 fp=0xc208018ed8 sp=0xc208018e80
_/home/ortuna/code/hpdf.TestLoadTTFontFromFile(0xc2080310e0)
    /home/ortuna/code/hpdf/font_test.go:23 +0x4c fp=0xc208018f78 sp=0xc208018ed8
testing.tRunner(0xc2080310e0, 0x8421d0)
    /usr/lib/go/src/testing/testing.go:447 +0xbf fp=0xc208018fd0 sp=0xc208018f78
runtime.goexit()
    /usr/lib/go/src/runtime/asm_amd64.s:2232 +0x1 fp=0xc208018fd8 sp=0xc208018fd0
created by testing.RunTests
    /usr/lib/go/src/testing/testing.go:555 +0xa8b

goroutine 1 [chan receive]:
testing.RunTests(0x5c9878, 0x841f00, 0x60, 0x60, 0x853401)
    /usr/lib/go/src/testing/testing.go:556 +0xad6
testing.(*M).Run(0xc2080400a0, 0x859e80)
    /usr/lib/go/src/testing/testing.go:485 +0x6c
main.main()
    _/home/ortuna/code/hpdf/_test/_testmain.go:242 +0x1d5

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
    /usr/lib/go/src/runtime/asm_amd64.s:2232 +0x1

rax     0x0
rbx     0x7f
rcx     0x7f41663b64b7
rdx     0x6
rdi     0x4781
rsi     0x4781
rbp     0x7ffc9fadcf00
rsp     0x7ffc9fadcb68
r8      0x7f41664e1140
r9      0x0
r10     0x8
r11     0x206
r12     0x7ffc9fadcd18
r13     0x7f
r14     0x7ffc9fadcd18
r15     0x2
rip     0x7f41663b64b7
rflags  0x206
cs      0x33
fs      0x0
gs      0x0
exit status 2

I've added a test for it in my other PR. I believe the free shouldn't be used since it's not something we are declaring dynamically?

rosylilly commented 9 years ago

I think the free is required.

ref: http://blog.golang.org/c-go-cgo#TOC_2.

Ortuna commented 9 years ago

That is also the reference I was checking. The example they give they are themselves creating the cs variable via the C.CString function. This should be freed (I'm assuming it was allocated by go). In font.go#L59 we get a pointer back. (the free on fonts.go#L60 looks good to me.) I get the crash at a defer on fonts.go#L63. We free a pointer coming back from C.HPDF_LoadTTFontFromFile. I'm still new to go, so I maybe totally wrong as well.

rosylilly commented 9 years ago

Oh, sorry. I didn't read the code.

You are right. We do not call free for GoString.