Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"free" function is absent though "malloc" exists #216

Closed
joker512-tmp opened this issue Aug 25, 2020 · 4 comments
Closed

"free" function is absent though "malloc" exists #216

joker512-tmp opened this issue Aug 25, 2020 · 4 comments
Assignees
Labels
❓ question Further information is requested
Projects

Comments

@joker512-tmp
Copy link

joker512-tmp commented Aug 25, 2020

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

@joker512-tmp joker512-tmp added the 🐞 bug Something isn't working label Aug 25, 2020
@Hywan
Copy link
Contributor

Hywan commented Aug 27, 2020

Hello,

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).

@Hywan Hywan self-assigned this Aug 27, 2020
@Hywan Hywan added ❓ question Further information is requested and removed 🐞 bug Something isn't working labels Aug 27, 2020
@Hywan Hywan added this to 📬 Backlog in Kanban via automation Aug 27, 2020
@Hywan Hywan moved this from 📬 Backlog to 🌱 In progress in Kanban Aug 27, 2020
@joker512-tmp
Copy link
Author

joker512-tmp commented Aug 27, 2020

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.

@Hywan
Copy link
Contributor

Hywan commented Aug 27, 2020

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.

@Hywan
Copy link
Contributor

Hywan commented Sep 10, 2020

Closing as it's not related to this package itself.

Please feel free to raise your voice if you need further help, or if you believe this project is responsible of addressing this issue.

Thanks!

@Hywan Hywan closed this as completed Sep 10, 2020
Kanban automation moved this from 🌱 In progress to 🎉 Done Sep 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
❓ question Further information is requested
Projects
Kanban
  
🎉 Done
Development

No branches or pull requests

2 participants