Closed wflohry closed 3 years ago
Hi @wflohry,
thanks for the pull request! I've merged it onto the master, and backported to c++17 branches.
In a9f9c76643df277c8c733fad19e86232a0adf8fb I also hid the external data destroy internals inside of the external_data
class, and moved the v8::PersistentHandleVisitor
implementation there.
The current v8 library does not provide any guarantees about when garbage collection of external objects will be performed, even after the underlying context is destroyed. This can cause issues with tests (valgrind, asan, etc.) and during runtime (especially for large externally-managed objects).
This PR seeks to the implement basic cleanup method [0] during v8pp::context destruction by visiting all objects created with
external_data<T>
, which are derived from a new base classexternal_data_base
. This manual destruction incurs a small runtime cost during v8pp::context (including one call todestroyObjects
and a vtable lookup for each object) but is likely preferable to calling the GC cleanup directly.The tables below show the before and after results of a leak check of v8pp_test using valgrind:
Before:
After:
The 8 bytes in the 'after' leak summary don't seem to be from
external_data
but ratherv8pp::factory<>
in test_class.cpp.[0] https://itnext.io/v8-wrapped-objects-lifecycle-42272de712e0