Fix crash caused by freeing internal V8 BackingStore memory #64
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes a crash that would happen in random places after
krom_create_texture_from_bytes()
was called.According to this document which I found here (linked from the
BackingStore
docstring), references toBackingStore
objects have to be managed via shared pointers that automatically free the backing store memory when the last reference is removed. Sincekrom_create_texture_from_bytes()
manually freed the backing store data if the image was not set to be readable, there would be a crash later on when V8 attempted to free the memory, for example when the Haxe array to which the backing store corresponded got GC'd. I guess similar issues would happen if the array was accessed again from Haxe.This was quite difficult to find, since there were a dozen or so different stack traces of the crash that were completely different every time the crash occured and the crash happened in various different background threads. I guess this was due to the fact that the time at which the last reference from the backing store gets removed varies a lot in V8. Luckily there was one stack trace that actually contained an entry for the BackingStore destructor:
After noticing this, it was rather easy to find the cause, given the fact that I was able to reproduce the crash every time an Armory scene with the Nishita sky model was used, which calls
kha.Image.fromBytes()
.Due to #63 I wasn't able to test my fix with the latest Armorcore main branch but with 93b696f, it should still work with newer versions though :)
Edit: also fixed this for
krom_create_texture_from_bytes3d()
. There could be some more misuses of backing stores, but the other usages look correct to me (though I still understand too little of the Armorcore and Kinc code in order to be certain here).