Norbyte / ositools

Advanced scripting and mod support for Divinity Original Sin 2
MIT License
376 stars 30 forks source link

Ext.UpdateItemCombo() causes weird crash #45

Closed Shresht7 closed 3 years ago

Shresht7 commented 3 years ago

This one is quite peculiar, and I'm not even sure if this is a script-extender problem. It is easier to explain with an example. Consider the following mod:

Ext.RegisterListener("StatsLoaded", function()
    local combo1 = Ext.GetItemCombo("BOOK_Skill_Air_Blank_A_SCROLL_Teleportation")
    combo1.Name = "NewRecipe1"
    Ext.UpdateItemCombo(combo1)

    local combo2 = Ext.GetItemCombo("BOOK_Skill_Fire_Blank_A_SCROLL_Haste")
    combo2.Name = "NewRecipe2"
    Ext.UpdateItemCombo(combo2)
end)

The mod isn't active when you first load the game. Activating and loading into a save works 100%. The new recipes are generated and work without problem (even if they were created from scratch and/or modify stuff not shown in the example like ingredients, results etc.)

Now, if you launch the game again (the mod is active this time around) and try to continue, the game crashes while loading the save-file. Specifically, when the loading screen reads cleaning up session/module. If the mod was deactivated before exiting or by using LL's mod-manager, the game does not crash when you re-activate the mod from the main menu and load the save.

From what I gather, the game crashes when it has to load the mod 'twice' (once when it gets to the main menu and then into the save), or rather, when it tries to "clean up" the first session before the second module-loading phase. This never happens in the editor, presumably, as it only ever loads the mod once. (I say 'twice', but all that matters is whether or not the mod loaded before the main-menu. If I activate the mod after I get to the main menu, I can load into any number of saves and go back to the main-menu freely.)

To make things even more bizarre, this doesn't happen if you only modify one ItemCombo recipe. In this example, if you comment out combo2.Name = "NewRecipe2" the game will load just fine. Also worth noting that its modifications that count here, I can call Ext.UpdateItemCombo() as many times I want without modification and it won't crash. But I can only modify and update one ItemCombo recipe. Do more than one and it starts crashing on clean-up again.

PS: I always feel bad submitting these crash-issues :( Wish I could help more instead of giving random bits of observations that may or may not be relevant to the problem.

Norbyte commented 3 years ago

I finally have some free time, I'll look into this bug in the coming days. I suspect that it's an allocator issue in one of the stats entry vectors but I'll debug further.

Norbyte commented 3 years ago

I've fixed this issue in the latest Devel build; can you check?

Shresht7 commented 3 years ago

Doesn't crash now. 🎉 What was causing the issue exactly? I mean, the other stat-editing functions of the extender didn't cause this problem, and it only occurred under very specific circumstances. Thanks for the fix! Amazing work as always.

Norbyte commented 3 years ago

It was a bug with how the ingredient/result lists were allocated; it was done differently from how the game allocated them, so when the game tried to free memory that the extender allocated for these lists it crashed.

Shresht7 commented 3 years ago

That makes sense. Thanks again.