maplibre / maplibre-native

MapLibre Native - Interactive vector tile maps for iOS, Android and other platforms.
https://maplibre.org
BSD 2-Clause "Simplified" License
1.03k stars 299 forks source link

Use `EnvAttachingDeleter` for `LocalGlyphRasterizer` and `AssetManagerFileSource` #2487

Closed louwers closed 3 months ago

louwers commented 3 months ago

The DefaultRefDeleter of these classes sometimes gets called on another thread than the one they were created on. This is because a class that composes them is held with a shared pointer that is shared between threads (e.g. std::shared_ptr`) and sometimes another thread holds to it longer. This causes problems because the env pointer of the creation thread is used, which cannot be used on another thread.

From the source code of jni.hpp:

The default policy is to delete the reference using the same JNIEnv as was passed to the constructor, but in cases where the object may be deleted on a different thread (commonly, the Java finalizer thread), EnvGettingDeleter or EnvAttachingDeleter may be needed.

There are likely other cases where we need to make this change. But these are the places where a crash is either reported or observed.

Closes #2045