簡體   English   中英

ThreadPool QueueUserWorkItem引發NullReferenceException

[英]ThreadPool QueueUserWorkItem Throws NullReferenceException

這是一個奇怪的問題,因為我無法重現。 我有兩個能夠重現此問題的客戶,向我發送了WER日志。

我有一個C#應用程序,該應用程序將工作項排隊並在后台執行它們。 在極少數機器中,嘗試執行工作線程的第一行后,立即引發NPE。

Ausnahmeinformationen: System.NullReferenceException
   bei magicsim.SimcPreloaderData+<>c__DisplayClass16_0.<LoadArmoryData>b__0(System.Object)
   bei System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object)
   bei System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   bei System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   bei System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   bei System.Threading.ThreadPoolWorkQueue.Dispatch()
   bei System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

WER信息也非常有趣。

Problemsignatur:
P1: magicsim.exe
P2: 2.2.0.2
P3: 5b7df04c
P4: magicsim
P5: 2.2.0.2
P6: 5b7df04c
P7: 29b
P8: fb
P9: System.NullReferenceException
P10:

因此,這是方法29b中的方法,試圖到達IL行fb

使用ILDASM,我可以驗證此問題簽名的位置與堆棧跟蹤(SimcPreloaderData)相匹配

Method #2 (0600029b) 
    -------------------------------------------------------
        MethodName: <LoadArmoryData>b__0 (0600029B)
        Flags     : [Assem] [HideBySig] [ReuseSlot]  (00000083)
        RVA       : 0x0000d338
        ImplFlags : [IL] [Managed]  (00000000)
        CallCnvntn: [DEFAULT]
        hasThis 
        ReturnType: Void
        1 Arguments
            Argument #1:  Object
        1 Parameters
            (1) ParamToken : (0800020f) Name : _ flags: [none] (00000000)

在ILSpy中,我可以看到上述方法的以下結構:

.method public hidebysig 
    instance void LoadArmoryData (
        string simcString
    ) cil managed 
{
    // Method begins at RVA 0x7b4d
    // Code size 48 (0x30)
    .maxstack 8

    IL_0000: newobj instance void magicsim.SimcPreloaderData/'<>c__DisplayClass16_0'::.ctor()
    IL_0005: dup
    IL_0006: ldarg.0
    IL_0007: stfld class magicsim.SimcPreloaderData magicsim.SimcPreloaderData/'<>c__DisplayClass16_0'::'<>4__this'
    IL_000c: dup
    IL_000d: ldarg.1
    IL_000e: stfld string magicsim.SimcPreloaderData/'<>c__DisplayClass16_0'::simcString
    IL_0013: ldarg.0
    IL_0014: ldstr "Acquiring SimC Executable"
    IL_0019: call instance void magicsim.SimcPreloaderData::set_Label(string)
    IL_001e: ldftn instance void magicsim.SimcPreloaderData/'<>c__DisplayClass16_0'::'<LoadArmoryData>b__0'(object)
    IL_0024: newobj instance void [mscorlib]System.Threading.WaitCallback::.ctor(object, native int)
    IL_0029: call bool [mscorlib]System.Threading.ThreadPool::QueueUserWorkItem(class [mscorlib]System.Threading.WaitCallback)
    IL_002e: pop
    IL_002f: ret
} // end of method SimcPreloaderData::LoadArmoryData

顯然沒有要執行的IL_00fb命令,因此存在NPE,也缺少對在LoadArmoryData內部具體發生位置的引用(除了事實確實如此)。 我只能從上面推測的是,由於某種原因,多線程正在出錯並且試圖訪問一條不存在的指令。

我不知道為什么會發生這種情況,並排除了防病毒和權限干擾。

編輯:完整的方法以供進一步參考。

public void LoadArmoryData(String simcString)
{
    SimC simc;
    Label = "Acquiring SimC Executable";
    ThreadPool.QueueUserWorkItem((_) =>
    {
        try
        {
            simc = SimCManager.AcquireSimC();
        }
        catch (Exception e)
        {
            MessageBox.Show("Could not acquire SimC because of an Exception: " + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            App.Current.Dispatcher.Invoke(() =>
            {
                PreloadingFailed(this, new EventArgs());
            });
            return;
        }
        Label = "Generating SimC Profile";
        Regex nameRegex = new Regex("(warrior|paladin|hunter|rogue|priest|deathknight|shaman|mage|warlock|monk|druid|demonhunter)+=\"?([^\r\n\"]+)\"?");
        String name = nameRegex.Match(simcString).Groups[2].Value;
        String text = simcString + "\nsave=./characters/" + name + ".simc";
        try
        {
            if (!Directory.Exists("characters"))
            {
                Directory.CreateDirectory("characters");
            }
            if (File.Exists("characters/" + name))
            {
                File.Delete("characters/" + name);
            }
            File.WriteAllText("characters/" + name + ".simc", text);
        }
        catch (UnauthorizedAccessException e)
        {
            MessageBox.Show("Could not write character profile due to a permissions issue. Please retry, running as Administrator." + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            App.Current.Dispatcher.Invoke(() =>
            {
                PreloadingFailed(this, new EventArgs());
            });
            return;
        }
        if (simc.RunSim("characters/" + name + ".simc"))
        {
            Label = "SimC Profile Generated";
            charName = name;
            App.Current.Dispatcher.Invoke(() =>
            {
                PreloadingComplete(this, new EventArgs());
            });
        }
        else
        {
            Label = "Failed to Generate Profile";
            MessageBox.Show("Failed to generate profile. Please check your input and try again.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

            App.Current.Dispatcher.Invoke(() =>
            {
                PreloadingFailed(this, new EventArgs());
            });
        }
    });
}

編輯2(出於完整性考慮):

實際上,29b引用b__0,而不是LoadArmoryData。

如果您在ILSpy中查看正確的方法,則會看到以下命令序列,這意味着明顯的問題和可能的解決方案。

// string contents = simcString + "\nsave=./characters/" + value + ".simc";
IL_00ce: ldarg.0
IL_00cf: ldfld string magicsim.SimcPreloaderData/'<>c__DisplayClass16_0'::simcString
IL_00d4: ldstr "\nsave=./characters/"
IL_00d9: ldloc.0
IL_00da: ldstr ".simc"
IL_00df: call string [mscorlib]System.String::Concat(string, string, string, string)
IL_00e4: stloc.1
// File.WriteAllText("characters/" + value + ".simc", contents);
IL_00e5: ldstr "characters/"
IL_00ea: ldloc.0
IL_00eb: ldstr ".simc"
IL_00f0: call string [mscorlib]System.String::Concat(string, string, string)
IL_00f5: ldloc.1
IL_00f6: call void [mscorlib]System.IO.File::WriteAllText(string, string)
// if (simc.RunSim("characters/" + value + ".simc"))
IL_00fb: ldarg.0
IL_00fc: ldfld class magicsim.SimC magicsim.SimcPreloaderData/'<>c__DisplayClass16_0'::simc
IL_0101: ldstr "characters/"
IL_0106: ldloc.0
IL_0107: ldstr ".simc"
IL_010c: call string [mscorlib]System.String::Concat(string, string, string)
IL_0111: callvirt instance bool magicsim.SimC::RunSim(string)
// (no C# code)
IL_0116: brfalse.s IL_0163

我認為問題中的分析是有缺陷的(例如,異常來自<LoadArmoryData>b__0(System.Object) ,它表示LoadArmoryData中的lambda表達式,其簽名需要對象參數;但是您正在查看IL中的LoadArmoryData,這是一種其簽名需要字符串參數的方法)。

這只是一個簡單的NullReferenceException,可能來自nameRegex.Match(simcString).Groups[2].Value; 正則表達式不匹配時。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM