Allocate memory to pass the pointer to external method.

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:

[code]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[/code]

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?

Maybe ByRef output as UInt8?

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

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) 

[quote=441040:@Norman Palardy]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)

[code]
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)
[/code][/quote]

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 [quote]EXC_BAD_ACCESS (SIGSEGV)[/quote] .

What am I doing wrong?

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

[quote=441040:@Norman Palardy]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)

[code]
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)
[/code][/quote]
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.

[quote=441045:@Aziel R. Pereira 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.[/quote]
This one I have tried, and it crashes with EXC_BAD_ACCESS (SIGSEGV)

[quote=441045:@Aziel R. Pereira 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.
[/quote]
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)

[quote=441045:@Aziel R. Pereira 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.[/quote]

[quote=441044:@Norman Palardy]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[/quote]

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.

[quote=441040:@Norman Palardy]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)

[code]
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)
[/code][/quote]
Sorry for the mistake, When I try this way, Xojo raises en error:

[quote]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)
[/quote]

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!

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