日期:2014-05-18  浏览次数:20997 次

【疑难杂症】结构体定义会导致程序崩溃
C# code

//类定义
    public class Classxxoo
    {                                                             
        private const int MAX_CLIPENUM_IN_BLOCK = 254;              
        private const int BLOCK_INDEX_NUM = (8 * 1024);           
                                                                  
        //[StructLayout(LayoutKind.Sequential)]
        private struct Block_File
        {
            public uint frameNum;  
            public uint clipStime; 
            public uint clipEtime; 
            public uint clip_size; 
            public uint index_num; 
        }                          

        //[StructLayout(LayoutKind.Sequential)]
        private struct clipIdx_info_t
        {
            public byte channel;
            public byte status;          
            public ushort fileInfoIdx;   
            public Block_File blkfile;   //注意,这一行注释掉就正常工作了
            public uint index_off;       
            public uint clip_off;        
        }                                

        //[StructLayout(LayoutKind.Sequential)]
        private struct KeyIndex
        {
            public uint cKeyOffset;    
            public uint TimeStampSec; 
        }

        //[StructLayout(LayoutKind.Sequential)]
        private struct blockIdx_info_t
        {
            public uint block_inf;   
            public uint block_size;  
            public uint block_no;    
            public byte channel;     
            public byte block_status;
            public byte block_type;  
            public byte clip_num;    
            public uint recover_num; 
            public uint write_Size;  
            public uint blockStime;  
            public uint blockEtime;  
            public uint index_num;   
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 28)]
            public byte[] rev;
      
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
            public clipIdx_info_t[] clip_info;
         
        }

        private blockIdx_info_t m_FileIndex;

        public Classxxoo( )
        {
            m_FileIndex = new blockIdx_info_t();
        }
    }


//------------------运行代码------------------------

    class Program
    {
        static void Main(string[] args)
        {
            Classxxoo xxoo = new Classxxoo( );
            Console.WriteLine(xxoo.ToString());
            Console.Read();
        }
    }






异常提示


未处理 System.TypeLoadException
  Message=未能从程序集“ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”中加载类型“ConsoleApplication1.Classxxoo”。
  Source=ConsoleApplication1
  TypeName=ConsoleApplication1.Classxxoo
  StackTrace:
  在 ConsoleApplication1.Program.Main(String[] args)
  在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
  在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
  在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
  在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
  在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: 


------解决方案--------------------
我将你的代码直接复制编译可以通过啊,只不过有很多“赋值未使用”“一直保持默认值”之类的警告而已
------解决方案--------------------
在VS2010上DEBUG没问题。。楼主单独创建个项目,把你上面的代码粘贴上跑下喃?

只有你上面的这些代码确实跑不出问题啊
------解决方案--------------------