[Gambas-user] Segfault when freeing pointer used for Extern

Jussi Lahtinen jussi.lahtinen at ...626...
Wed Nov 6 02:03:09 CET 2013


Correct code:


' void uuid_generate(uuid_t out);
Extern UUID_Gen(op 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) In "libuuid:1" Exec
"uuid_unparse"

Public Sub Main()

  Dim OP As Pointer = Alloc(SizeOf(gb.Byte), 16)   ' ptr to the uuid
returned by uuid_generate
  Dim SP As Pointer = Alloc(SizeOf(gb.Byte), 37)   ' ptr to the string
returned by uuid_unparse

  Dim sOP As String    ' our result (UUID as a string)


  UUID_Gen(OP)

  UUID_ToStr(OP, SP)

  sOP = String@(SP)

  Print sOP

  Free(OP)
  Free(SP)

End


Jussi



On Wed, Nov 6, 2013 at 2:56 AM, Benoît Minisini <
gambas at ...1...> wrote:

> 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
>
>
> ------------------------------------------------------------------------------
> November Webinars for C, C++, Fortran Developers
> Accelerate application performance with scalable programming models.
> Explore
> techniques for threading, error checking, porting, and tuning. Get the most
> from the latest Intel processors and coprocessors. See abstracts and
> register
> http://pubads.g.doubleclick.net/gampad/clk?id=60136231&iu=/4140/ostg.clktrk
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>



More information about the User mailing list