jjhbw / barcode-scanner-webassembly

Using the ZBar C-library in WebAssembly to build a browser-based multi-barcode detector
91 stars 19 forks source link

memory leak issue #1

Open Anurag9589 opened 5 years ago

Anurag9589 commented 5 years ago

It is very nice for scanning using web assembly and very helpful repository, thanks for sharing.

However, I came across following scenario, please read, when running demo sample application from iPad, it keeps adding memory and at certain point page gets crashed. It clearly gets noticed in iPad, however in Chrome it shows memory getting increased and after some memory limit, chrome also shows crash behavior.

please suggest or improve the memory management.

jjhbw commented 5 years ago

Hi Anurag, nice find!

I didn't put much thought in testing for memory leaks, so I agree that this is likely a memory leak.

A new wasm buffer is allocated for each new image frame, which is supposed to be destroyed after prediction has finished.

It is possible that this buffer is not freed correctly. This is the JS code that invokes the buffer freeing method: https://github.com/jjhbw/barcode-scanner-webassembly/blob/1ed4d0241ab8c30b0089cf08e4f846f156f4bdc6/index.html#L91-L93

It should call this wasm function, with a pointer to the buffer as an argument. https://github.com/jjhbw/barcode-scanner-webassembly/blob/1ed4d0241ab8c30b0089cf08e4f846f156f4bdc6/scan.c#L72-L77

Probably something went wrong in invoking this function. Sadly, these bindings are pretty fragile and don't throw errors when something goes wrong.

An alternative, more elegant option would be to re-use the buffer for new frames. This should also improve performance a bit.

I currently dont have time to dive into this, if you find the solution, let me know and i'll merge it in if you want.