读书人

这段unsafe代码会不会造成内存泄露或者

发布时间: 2012-03-24 14:00:46 作者: rapoo

这段unsafe代码会不会造成内存泄露或者其他运行时问题?
vs2010+win7经测试,可以正常调用。疑惑rt,如果存在问题,该如何解决?谢谢。

C# code
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace WindowsFormsApplication3//测试程序{       public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {         int ret= mycl.test(testarr.arr);            for (int k = 0; k < testarr.arr.Length; ++k)            {                textBox1.Text = textBox1.Text + testarr.arr[k].ToString();            }          public class mycl           {            [DllImport("testdll.dll")]             public static extern int test(int[] arr);//testdll为c++写的win32 dll,里面有个test函数,对传入的数组进行初始化           }         public class testarr         {           public unsafe static int[] arr = new int[100];         }             }


[解决办法]
testdll的test没有内存问题就 没问题.
[解决办法]
这个应该不会。
[解决办法]
这个应该不会。
[解决办法]
上面的代码虽然带unsafe关键字,但其实根本不包含不安全代码
arr 只是正常的托管数组。关键字new无法分配非托管内存。不安全的固定大小缓冲区的定义类似于:
C# code
unsafe struct testarr{    public fixed int arr[32];  //请留意括号'[]'的位置} 

读书人网 >C#

热点推荐