On Mon, Aug 7, 2023 at 7:52 AM 'Michael Hagar' via emscripten-discuss <
[email protected]> wrote:

> It's mentioned in the docs here:
> https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html?highlight=memory_growth#access-memory-from-javascript
>
> That if using the ALLOW_MEMORY_GROWTH option, the JS side needs to refresh
> the views on the memory buffer.
>
> If I used Module._malloc, and then memory grows, are the values returned
> from the original Module._malloc call still valid? If the values returned
> refer to specific positions in the underlying buffer, it seems like they
> would be invalidated, but that doesn't seem to be the case (they still seem
> valid).
>
> What's going on here? I have a feeling I'm misunderstanding how Emscripten
> works with C++ memory.


When the memory grows it doesn't affect previously malloc'd memory and all
old pointers are still valid.

The memory views used by JS (HEAP8, HEAP32, etc) are automatically updated
via calling `updateMemoryViews` from the `growMemory` JS function (which is
ultimately where the memory gets grows when `malloc` calls `sbrk` to get
more memory):
https://github.com/emscripten-core/emscripten/blob/0538c9dba690359baa7c09e99a7ff597796c4939/src/library.js#L171-L172

cheers,
sam



> --
> 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].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/emscripten-discuss/432220a3-fcc4-46db-901e-145deb1fcdd5n%40googlegroups.com
> <https://groups.google.com/d/msgid/emscripten-discuss/432220a3-fcc4-46db-901e-145deb1fcdd5n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/emscripten-discuss/CAL_va2-j7SM1q1BikeZNi2wJ-Y4yv3gOS%3DqXHPo06cMO4TW%3D9A%40mail.gmail.com.

Reply via email to