C# - 更改非托管对象的引用

问题描述 投票:-4回答:1

因此,假设创建一个对非托管对象的引用...

RandomObject x;

然后我将该引用分配给非托管对象。

x = new RandomObject(argX, argY);

但后来我决定我想要x引用该对象的不同实例:

x = new RandomObject(argZ, argV);

在这种情况下发生了什么?当我将x重新分配给非托管对象的不同实例时,是否处理了“新的RandomObject(argX,argY)”?或者我必须在重新分配之前自己处理它?我要问的是这个对象的生命周期。

c# dispose unmanaged
1个回答
1
投票

在C#中,垃圾收集器将管理所有对象并释放任何不再具有任何引用的对象的内存。

使用你的例子:

RandomObject x;
// A RandomObject reference is defined.

x = new RandomObject(argX, argY);
// Memory is allocated, a new RandomObject object is created in that memory location, 
// and "x" references that new object.

x = new RandomObject(argZ, argV);
// Memory is allocated, a new RandomObject object is created in that memory location,
// and "x" references that new object. The first RandomObject object no longer has 
// any references to it and will eventually be cleaned up by the garbage collector.

非托管资源

即使管理C#中的所有对象,也存在“非托管资源”,例如打开文件或打开连接。当具有非托管资源的对象超出范围时,它将被垃圾收集,但垃圾收集器将不会释放这些非托管资源。这些对象通常实现IDisposable,它允许您在清理资源之前处置它们。

例如,StreamWriter类中有一个非托管资源,它打开一个文件并写入它。

这是一个未能释放非托管资源的示例:

// This will write "hello1" to a file called "test.txt".
System.IO.StreamWriter sw1 = new System.IO.StreamWriter("test.txt");
sw1.WriteLine("hello1");

// This will throw a System.IO.IOException because "test.txt" is still locked by 
// the first StreamWriter.
System.IO.StreamWriter sw2 = new System.IO.StreamWriter("test.txt");
sw2.WriteLine("hello2");

要正确释放文件,您必须执行以下操作:

// This will write "hello1" to a file called "test.txt" and then release "test.txt".
System.IO.StreamWriter sw1 = new System.IO.StreamWriter("test.txt");
sw1.WriteLine("hello1");
sw1.Dispose();

// This will write "hello2" to a file called "test.txt" and then release "test.txt".
System.IO.StreamWriter sw2 = new System.IO.StreamWriter("test.txt");
sw2.WriteLine("hello2");
sw2.Dispose();

幸运的是,实现IDisposable的对象可以在using语句中创建,然后当它超出范围时,将自动调用Dispose()。这比手动调用Dispose(如上例所示)更好,因为如果使用块中有任何意外的退出点,您可以放心,您的资源已被释放。

using (System.IO.StreamWriter sw1 = new System.IO.StreamWriter("test.txt"))
{
  sw1.WriteLine("hello1");
}

using (System.IO.StreamWriter sw2 = new System.IO.StreamWriter("test.txt"))
{
  sw2.WriteLine("hello2");
}
© www.soinside.com 2019 - 2024. All rights reserved.