Memoryblock from void*

Hi, I have a macos function which returns a pointer to some data as a void*. I can find out the length of the memory block from another call but can I just do this.

dim mb as memoryblock = myptr

And just read the data up to the length without problems.

The memoryblock returns a size of -1 but starts showing alot of data in the debugger.

Yes this is fine. When you convert a pointer into a MemoryBlock the Size is unknown (-1), so the debugger doesn’t know when to stop reading memory. The extra data you see in the debugger is whatever happens to be adjacent to your data in memory.

This means you have to be extra careful to respect the reported size when reading or writing to it, because the MemoryBlock class isn’t checking for you and will happily read from or write into adjacent memory if it can (as demonstrated in the debugger.)

Also, be sure to check the documentation to see whether you are required to call a separate function to free the memory when you’re done with it.

Thanks, good to know

With the newish framework you can create a xojo.core.memoryBlock from a Ptr and during construction, you can set the size (especially as you know it).
https://documentation.xojo.com/api/deprecated/xojo.core.memoryblock.html#xojo-core-memoryblock-constructor(p_as_ptr-_size_as_uinteger)