Allocate memory to pass the pointer to external method.

  1. 5 months ago

    Hi, I need to call this function from an external method, and I'm not figuring out how to allocate the output pointer to pass it's pointer.

    size_t WebPEncodeRGB(const uint8_t* rgb, int width, int height, int stride, float quality_factor, uint8_t** output);

    I Have tried these two forms:

    Note that the difference between my two attempts is that I've first declared the output Ptr ByRef and the second one was by value.

    1 -

    Declare Function WebPEncodeRGB Lib "libwebp.dylib" (rgb As Ptr, width As Int16, height As Int16, stride As Int16, quality_factor As Single, ByRef output As Ptr) As Uint16
    Dim output As Ptr

    and pass output as the last parameter, but it crashes:

    Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
    Exception Codes:       EXC_I386_GPFLT
    Exception Note:        EXC_CORPSE_NOTIFY
    
    Termination Signal:    Segmentation fault: 11
    Termination Reason:    Namespace SIGNAL, Code 0xb
    Terminating Process:   exc handler [60619]
    
    Application Specific Information:
    Performing @selector(performClick:) from sender XOJButton 0x60000375dc30
    
    Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
    0   libwebp.dylib                 	0x000000010d753243 ConvertRGB24ToY_SSE41 + 73
    1   libwebp.dylib                 	0x000000010d75f75a ImportYUVAFromRGBA + 2427
    2   libwebp.dylib                 	0x000000010d75ec64 Import + 269
    3   libwebp.dylib                 	0x000000010d75e508 Encode + 286
    4   libwebp.dylib                 	0x000000010d75e3e4 WebPEncodeRGB + 27

    2 -

    Declare Function WebPEncodeRGB Lib "libwebp.dylib" (rgb As Ptr, width As Int16, height As Int16, stride As Int16, quality_factor As Single, output As Ptr) As Uint16
    Dim output As Ptr

    and pass output as the last parameter, again. This time, the code runs ok, but the pointer remais &h0000000000000000 and I don't have a way to access the resulting data.

    How can I allocate memory to pass it's pointer to an external method?

  2. Andrew L

    Jun 11 San Francisco, CA, USA

    Maybe ByRef output as UInt8?

  3. @Andrew L Maybe ByRef output as UInt8?

    But how can i create a MemoryBlock and pass it's pointer as a Uint8 value?

  4. Norman P

    Jun 11 Pre-Release Testers, Xojo Pro Outside
    Edited 5 months ago

    the ptr is not a uint8
    this

    uint8_t** output);

    is a ptr to a ptr that points to uint8's

    which in xojo parlance for a param is a byref ptr (byref means "ptr to") so in in this case byref ptr is "ptr to a ptr to"
    a memoryblock which is just a bunch of bytes - or uint8's

    so something like this should be close ( I dont know about the other params)

    Declare Function WebPEncodeRGB Lib "libwebp.dylib" (rgb As Ptr, width As Int16, height As Int16, stride As Int16, quality_factor As Single, ByRef output As Ptr) As Uint16
    
    dim mb as new memoryblock(4096) // I truly have no idea how big to make this
    
    dim result as Uint16 = WebPEncodeRGB Lib "libwebp.dylib" (rgb, width, height, stride, quality_factor, mb) 
  5. @Norman P the ptr is not a uint8
    this

    uint8_t** output);

    is a ptr to a ptr that points to uint8's

    which in xojo parlance for a param is a byref ptr (byref means "ptr to") so in in this case byref ptr is "ptr to a ptr to"
    a memoryblock which is just a bunch of bytes - or uint8's

    so something like this should be close ( I dont know about the other params)
    Declare Function WebPEncodeRGB Lib "libwebp.dylib" (rgb As Ptr, width As Int16, height As Int16, stride As Int16, quality_factor As Single, ByRef output As Ptr) As Uint16 dim mb as new memoryblock(4096) // I truly have no idea how big to make this dim result as Uint16 = WebPEncodeRGB Lib "libwebp.dylib" (rgb, width, height, stride, quality_factor, mb)

    Yes, that's it. I just don't know how to allocate and pass the pointer to this structure. It seems like Xojo is not letting the lib to manage the memory. When I create a Null Ptr and pass it ByRef, the code works ok, but I don't have the address to read the bytes, and if I create and allocate a MemoryBlock and pass its Ptr, it crashes with

    EXC_BAD_ACCESS (SIGSEGV)

    .

    What am I doing wrong?

  6. Norman P

    Jun 11 Pre-Release Testers, Xojo Pro Outside

    without seeing your code I have no idea
    it could be rgb or any of the other params that is causing this but without seeing the code that sets those we up cant tell

  7. @Norman P the ptr is not a uint8
    this

    uint8_t** output);

    is a ptr to a ptr that points to uint8's

    which in xojo parlance for a param is a byref ptr (byref means "ptr to") so in in this case byref ptr is "ptr to a ptr to"
    a memoryblock which is just a bunch of bytes - or uint8's

    so something like this should be close ( I dont know about the other params)
    Declare Function WebPEncodeRGB Lib "libwebp.dylib" (rgb As Ptr, width As Int16, height As Int16, stride As Int16, quality_factor As Single, ByRef output As Ptr) As Uint16 dim mb as new memoryblock(4096) // I truly have no idea how big to make this dim result as Uint16 = WebPEncodeRGB Lib "libwebp.dylib" (rgb, width, height, stride, quality_factor, mb)

    Can you point me how to write the declaration and allocate the memory? I already have the structure, I just don't know how to use Dim to allocate the memory for it.

    Thanks for your help.

  8. @Aziel RPereira Jr. Can you point me how to write the declaration and allocate the memory? I already have the structure, I just don't know how to use Dim to allocate the memory for it.

    Thanks for your help.

    This one I have tried, and it crashes with EXC_BAD_ACCESS (SIGSEGV)

  9. Tim H

    Jun 11 Pre-Release Testers Portland, OR USA

    @Aziel RPereira Jr. Can you point me how to write the declaration and allocate the memory? I already have the structure, I just don't know how to use Dim to allocate the memory for it.

    Are you using a Structure? If so you simply dim struct as mystructurename. That allocates the memory. If you want to use a MemoryBlock, then

    dim mb as MemoryBlock
    mb = New MemoryBlock(size)
  10. Norman P

    Jun 11 Pre-Release Testers, Xojo Pro Outside

    @Aziel RPereira Jr. Can you point me how to write the declaration and allocate the memory? I already have the structure, I just don't know how to use Dim to allocate the memory for it.

    Thanks for your help.

    @Norman P without seeing your code I have no idea
    it could be rgb or any of the other params that is causing this but without seeing the code that sets those we up cant tell

  11. Derk J

    Jun 11 Pre-Release Testers, Xojo Pro

    Libwebp is probably also using pthreads you should be aware that xojo is not actually compatible with pthreads depening on their usage.

  12. @Derk J Libwebp is probably also using pthreads you should be aware that xojo is not actually compatible with pthreads depening on their usage.

    ok, I will serach if they use pthreads.

  13. @Norman P the ptr is not a uint8
    this

    uint8_t** output);

    is a ptr to a ptr that points to uint8's

    which in xojo parlance for a param is a byref ptr (byref means "ptr to") so in in this case byref ptr is "ptr to a ptr to"
    a memoryblock which is just a bunch of bytes - or uint8's

    so something like this should be close ( I dont know about the other params)
    Declare Function WebPEncodeRGB Lib "libwebp.dylib" (rgb As Ptr, width As Int16, height As Int16, stride As Int16, quality_factor As Single, ByRef output As Ptr) As Uint16 dim mb as new memoryblock(4096) // I truly have no idea how big to make this dim result as Uint16 = WebPEncodeRGB Lib "libwebp.dylib" (rgb, width, height, stride, quality_factor, mb)

    Sorry for the mistake, When I try this way, Xojo raises en error:

    Parameter "output" expects type Ptr, but this is class MemoryBlock.
    Dim result As Uint16 = WebPEncodeRGB(inputRGB, image.Width, image.Height, image.Width * 4, 50, output)

  14. Ok, I have succeeded to make the code run. In fact, I've removed ByRef, and passed the memoryblock. Now the code runs, and the lib returns the size. Now I will try to manipulate the output memory.

    Thank You all!

  15. Ulrich B

    Jun 12 Pre-Release Testers, Xojo Pro Europe (Germany, Berlin) · xo...

    Did you change your Int parameters too? Int in usual C terms is not Int16 but Int32.

or Sign Up to reply!