JJ, thanks for your clear and prompt reply. I tried to free the memory for dataPtr as follows:
var nDataBytes = array.length * array.BYTES_PER_ELEMENT; var dataPtr = Module._malloc(nDataBytes); // Copy data to Emscripten heap (directly accessed from Module.HEAPU8) var dataHeap = new Uint8Array(Module.HEAPU8.buffer, dataPtr, nDataBytes); dataHeap.set(new Uint8Array(array.buffer)); Module._free(dataPtr); return dataHeap.byteOffset; Unfortunately, my code hangs after passing thru the above code twice and I didn't manage to see any error messages. Do you have any idea on what might have gone wrong? Thanks for your help. On Monday, August 25, 2014 8:16:21 PM UTC+8, jj wrote: > > Yes, each malloc() call must be matched with a call to free(). If you > create a typed array view to the memory area you allocated with malloc(), > you should not reference that view after you free() the underlying memory, > or you will be writing to a memory area you have already freed. > > Typed array views that you have allocated with a 'new Uint8Array' or > similar do not need to be deleted, since they are subject to JS VM garbage > collection. > > > 2014-08-25 13:53 GMT+03:00 awt <[email protected] <javascript:>>: > >> Hi, >> >> I have allocated memory for a Uint8Array in JS using the following code: >> >> >> var nDataBytes = array.length * array.BYTES_PER_ELEMENT; >> var dataPtr = Module._malloc(nDataBytes); >> >> // Copy data to Emscripten heap (directly accessed from Module.HEAPU8) >> var dataHeap = new Uint8Array(Module.HEAPU8.buffer, dataPtr, nDataBytes); >> dataHeap.set(new Uint8Array(array.buffer)); >> >> return dataHeap.byteOffset; >> >> I then return the pointer to the memory thru dataHeap.byteOffset to my >> C++ code. Do I have to free the memory in dataPtr and dataHeap.byteOffset in >> my C++ code explicitly using the free and delete calls respectively? >> >> -- >> You received this message because you are subscribed to the Google Groups >> "emscripten-discuss" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected] <javascript:>. >> For more options, visit https://groups.google.com/d/optout. >> > > -- You received this message because you are subscribed to the Google Groups "emscripten-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
