由于线程停在某个无法进行垃圾回收的点(可能是因为已对代码进行了优化),因此无法计算表达式的值


前段时间在调试代码的过程中,调试器无法跟踪到变量的值并报异常,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