Got unexpected NullReferenceException when patch a method
What happened?
Most of my patch method work well, just this method fail to patch. The method is like this:
public double TheMethod(bool someBool = true)
{
return someDouble;
}
My patch method is like this, I try both prefix and postfix but got the same error.
[HarmonyPrefix]
[HarmonyPatch("TheMethod")]
public static bool TheMethod(bool someBool = true)
{
return true;
}
some clue: The code run well, both original method and patch method run without any error, but after run the code, it show the error.
btw, How can I debug error like this? I can't even know which variable is null.
BepInEx Distribution
Stable from GitHub
Log outputs
[Error : Unity Log] NullReferenceException: Object reference not set to an instance of an object
Stack trace:
(wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition.Glue:ThiscallStructRetPtr<TheMethod>
Environment
- OS: windows 10
- BepInEx: 5.4.20
- Game: NA
Greetings!
Please provide more information about the environment you tested this on. What game did you use to reproduce the issue (if it was some dummy test game, please share the binaries)?
The environment information is important, as I cannot reproduce the problem with our own Test Game set (we have various Unity builds 2019-2022, games are 64-bit).
Thx for quick response! For the game information, sry I can't, I afraid it against the ToS. Or is there any way I can provide it in privately?
more environment information: the game is 32-bit unity build: 2018.4.24.3174801 but for the plugin, the build system seems use 2018.4.25 when build(like the log below), I don't if it may cause this error.
Tool depends on UnityEngine.Modules (>= 2018.4.24.3174801) but UnityEngine.Modules 2018.4.24.3174801 was not found. An approximate best match of UnityEngine.Modules 2018.4.25 was resolved.
There is a currently known issue in MonoMod that affects applying certain runtime patches in 32-bit games. A fix has been submitted but not yet merged (see https://github.com/MonoMod/MonoMod.Common/pull/28).
Until the fix is merged into MonoMod, you can try using this custom-built MonoMod with the fix included: MMTestFix.zip
Both DLLs go into BepInEx/core folder.
Omg, I try the MMTestFix.zip you provide, it work now, both prefix and postfix work well.
Hope we'll have a release version soon.
Thx