You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi. I have a function in WebAssembly which gets an image matrix as a parameter. The image format is "unsigned char*" pointer. To pass it I allocate the memory with "dataPtr = instance.exports.malloc(dataLen)" call, fill the data under this pointer with an image and use "dataPtr" as a parameter to my function. In C++ world I usually need to free memory afterwards. I don't have such function here. And after some number of runs I finally get a memory out-of-bounds access, and I need to restart python. Is there any way to free the memory in WebAssembly from python?
Below is the code to reproduce the problem:
image_list = bytearray(image.flatten().tolist()) # convert numpy matrix to byte array
dataLen = len(image_list) # amount of memory we need for image
dataPtr = instance.exports.malloc(dataLen) # allocate memory with malloc
uint8_view = instance.memory.uint8_view(offset = dataPtr) # get "uint8_view" for pointer
uint8_view[:dataLen] = image_list[:] # fill data under pointer with bytes
instance.exports.setImage(dataPtr, imageWidth, imageHeight) # call WebAssembly function
# instance.exports.free(dataPtr) # absent function to free memory after call
This code works normally except of the aforementioned memory leak which leads to runtime error after some runs:
<ipython-input-49-35a9fe36b2bc> in func(image)
2 image_list = bytearray(image.flatten().tolist())
3 dataLen = len(image_list)
----> 4 dataPtr = instance.exports.malloc(dataLen)
5 uint8_view = instance.memory.uint8_view(offset = dataPtr)
6 uint8_view[:dataLen] = image_list[:]
RuntimeError: Call error: Error when calling invoke: A `memory out-of-bounds access` trap was thrown at code offset 76788
I didn't find anything about memory freeing in the documentation (including "malloc" function, which is not documented feature).
Thank you for your help
The text was updated successfully, but these errors were encountered:
The same way you export a malloc and a setImage functions, you should also export a free function. Check the greet example (the greet.rs file and the greet.py file).
The thing here is that I don't explicitly import "malloc", but it's presented. My compilation commands looks like this: emcc -O2 src/Graph.cpp src/Selection.cpp -o core.wasm -s EXPORTED_FUNCTIONS='["_setImage", "_getMask", "_updateSelection"]' -s EXTRA_EXPORTED_RUNTIME_METHODS='["cwrap"]' -s TOTAL_MEMORY=268435456
So, no "malloc" in "EXPORTED_FUNCTIONS". And when you use wasm from JS both "malloc" and "free" functions are presented.
The easy solution is just to make wrapper for "free" function and call it inside, but it looks like both have to be supported by default.
Personally, I would open an issue on emscripten to understand why free isn't exported by default. If malloc is, it is correct to assume free should be exported too.
Hi. I have a function in WebAssembly which gets an image matrix as a parameter. The image format is "unsigned char*" pointer. To pass it I allocate the memory with "dataPtr = instance.exports.malloc(dataLen)" call, fill the data under this pointer with an image and use "dataPtr" as a parameter to my function. In C++ world I usually need to free memory afterwards. I don't have such function here. And after some number of runs I finally get a
memory out-of-bounds access
, and I need to restart python. Is there any way to free the memory in WebAssembly from python?Below is the code to reproduce the problem:
This code works normally except of the aforementioned memory leak which leads to runtime error after some runs:
I didn't find anything about memory freeing in the documentation (including "malloc" function, which is not documented feature).
Thank you for your help
The text was updated successfully, but these errors were encountered: