[Embind] How to delete pointer in C++

155 views
Skip to first unread message

Thomas

unread,
Jul 4, 2020, 12:43:43 PM7/4/20
to emscripten-discuss
Here is my command: emcc --bind -o test.js C/keyGen/sources/test.cpp -s ENVIRONMENT='web' -s MODULARIZE=1 -s ALLOW_MEMORY_GROWTH=1 -s ASSERTIONS=1 -s SAFE_HEAP=1 -s DETERMINISTIC=1

I experienced weird behaviors when deleting pointers in my C++ program...
before I delete the pointer, it seems like the memory the pointer is pointing to has already corrupted.


Do I need to call delete[] ptr if ptr is a class member variable?

Weird things for me: If i try to delete the pointer after use, right before the delete[] ptr, the memory that the ptr is pointing to seems corrupted...If i don't delete it, the data stays consistent.

Will dynamically allocated array defined in the C++ class be automatically freed after the class instance is deleted (or there is no function using it)?

Can anybody explain to me how memory allocated for class member variable that is a pointer?

Many thanks.

Osman Zakir

unread,
Mar 5, 2021, 4:38:02 PM3/5/21
to emscripte...@googlegroups.com
Do you have an array of pointers?  If you want to delete just single pointers, the normal version of delete will be fine.  

I think it'd be better to use smart pointers, though.  I'd need to look at the code to say for sure, however.  

--
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 emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/7aaf1d72-30ff-4d6e-9cc1-bdf49c4b744ao%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages