由於線程停在某個無法進行垃圾回收的點(可能是因為已對代碼進行了優化),因此無法計算表達式的值


前段時間在調試代碼的過程中,調試器無法跟蹤到變量的值並報異常,AnisometryT Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible, possibly because the code is optimized.

如下圖所示:

 

 發現在以上代碼的下面,有一個類型的構造方法含有很多個參數:

后查找資料,發現在 .net 3.5 以及32位應用程序中,單個方法中所有參數所占用內存的總大小不能超過256個字節。

用一段簡單的代碼重現了以上現象:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace ConsoleApplication1
{
    class TestClass
    {
        public void TestParams()
        {
            MyObject mo = new MyObject();
            Console.WriteLine(mo.ID);
            Bytes128 bytes128 = new Bytes128();            
          
            TestParams256(bytes128, bytes128);
        }

        private void TestParams256(Bytes128 bytes1281, Bytes128 bytes1282)
        {
            Console.WriteLine(bytes1281);
        }

        private void TestParams128(Bytes128 bytes128)
        {
            Console.WriteLine(bytes128);
        }
    }

    struct Bytes16
    {
        int word;
        int word2;
        int word3;
        int word4;
    }

    struct Bytes32
    {
        Bytes16 word16_1;
        Bytes16 word16_2;
    }

    struct Bytes64
    {
        Bytes32 word32_1;
        Bytes32 word32_2;
    }

    struct Bytes128
    {
        Bytes64 word64_1;
        Bytes64 word64_2;
    }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class MyObject
    {
        public int ID
        {
            get;
            set;
        }
    }
}
View Code

運行並在mo變量處斷點:

 

其中方法TestParams256的兩個參數都為Bytes128,Bytes128為Struct值類型,傳遞的時候會采用值傳遞,兩個參數共占用256個字節,導致以上現象出現。

 

解決方法:

1、編譯為x64位程序

2、采用.net 4.0 或更高版本的Framework

3、將Struct值類型定義為Class引用類型

4、將方法拆分為幾個方法,確保每個方法所傳遞參數占用內存大小小於256個字節。

 

另外,以上異常只是影響調試,在程序運行過程中並不會有影響。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM