[Gambas-user] Segfault when freeing pointer used for Extern
Benoît Minisini
gambas at ...1...
Wed Nov 6 01:56:50 CET 2013
Le 06/11/2013 01:07, Benoît Minisini a écrit :
>
> What is the exact definition of 'uuid_t' ?
>
> Also, you can use valgrind to know when exactly the faulty memory
> access occurs. This will give better clues.
>
typedef unsigned char uuid_t[16];
You get it in the '/usr/include/uuid.h', that you must read before
writing any external definition.
So, 'uuid_t' is a pointer to 16 'unsigned char', i.e. 16 bytes.
Now your code:
----------------------------------------------------------------------
' Gambas module file
' void uuid_generate(uuid_t out);
Extern UUID_Gen(op As Pointer) As Pointer In "libuuid:1" Exec
"uuid_generate"
' void uuid_unparse(uuid_t uu, char *out)
Extern UUID_ToStr(ip As Pointer, op As Pointer) As Pointer In
"libuuid:1" Exec "uuid_unparse"
Public Sub Main()
Dim OP As Pointer ' ptr to the uuid returned by uuid_generate
Dim SP As Pointer ' ptr to the string returned by uuid_unparse
Dim sOP As String ' our result (UUID as a string)
Dim rtn As Integer ' int rtn code from extern funcs
Dim sArch As String ' system architecture (selects pointer length)
sArch = System.Architecture
Error "Arch=" & sArch
OP = Alloc(IIf(sArch = "x86", 4, 8))
SP = Alloc(IIf(sArch = "x86", 4, 8))
----> Don't do that, use the SizeOf() function.
rtn = UUID_Gen(OP)
----> OP must points at a 16 bytes allocation. So you failed! The
library will erase the memory past the allocation, making everything
crash sooner or later.
Error "UUID_Gen=" & rtn
rtn = UUID_ToStr(OP, SP)
----> That function returns nothing. I don't know why you think Gambas
will make it return something. Moreover, if you read the doc, it tells
you that SP must point at a 37 bytes allocation. So you failed again,
and the library will erase the memory randomly a second time.
You really must know C and how a CPU works to deal with extern
functions. Alas, Gambas can't help there! Or, at least, it does as much
as he can.
--
Benoît Minisini
More information about the User
mailing list