PDA

View Full Version : چگونه unmanaged resource را آزاد کنیم



the Dead
دوشنبه 22 بهمن 1386, 19:05 عصر
اگر بر فرض مثال از روش زیر استفاده کنیم جای Clean up unmanaged resources چه کدی باید بگذاریم
مثلاً باید اشاره گرها به Heap را مساوی Null قرار دهیم؟

public class sample_class : IDisposable
{

public void Dispose()
{
// Clean up unmanaged resources...
}
}

hassan razavi
دوشنبه 22 بهمن 1386, 20:19 عصر
خود Garbage Collection اینکارها را انجام میده.

the Dead
دوشنبه 22 بهمن 1386, 20:42 عصر
خود Garbage Collection اینکارها را انجام میده.
یعنی پس فقط نکتش اینه که ما تابع dispose() را صدا بزنیم و بقییه عملیات آزاد سازی توسط Garbage Collector انجام میشه.درسته؟

1.در کد دیگری هنگام finalize از آزاد سازی managed resource ها خود داری شده آیا این معنیش اینه که اگر متد finalize اجرا شود خودش ازاد سازی managed resource ها را بعهده میگیرد؟
2.سوال بعدی اینکه جای این دو خط // Dispose managed resources. و
// Clean up unmanaged resources here. نیز آیا نیاز به نوشتن کد خاصی نیست و این کار را نیز Garbage Collector انجام میدهد؟

public class MyResourceWrapper : IDisposable
{

private bool disposed = false;
public void Dispose()
{

CleanUp(true);
GC.SuppressFinalize(this);
}
private void CleanUp(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
// Dispose managed resources.
}
// Clean up unmanaged resources here.
}
disposed = true;
}
~MyResourceWrapper()
{
CleanUp(false);
}
}

Alireza_Salehi
دوشنبه 22 بهمن 1386, 22:34 عصر
خود Garbage Collection اینکارها را انجام میده.


از کی تا حالا Garbage Collector منابع Unmanaged را آزاد می کند؟

the Dead
دوشنبه 22 بهمن 1386, 22:51 عصر
از کی تا حالا Garbage Collector منابع Unmanaged را آزاد می کند؟
احسند منم همینو میگم
حالا جواب پست شماره 3 چی میشه؟

رضا عربلو
سه شنبه 23 بهمن 1386, 00:53 صبح
ببینید، بایستی ببنید که شما از چه منابعی استفاده کرده اید که حالا می خواهید آزاد کنید. اگر از توابع API استفاده می کنید غالب این منابع همراه با یک Handle برای شما فراهم می گردد و همین منابع با استفاده از همین هندل نیز آزاد می گردند..
برای مثال اگر شما با استقاده از توابع API یک فایل را باز کنید و هندل آن را بدست بیاورید همین هندل در آزاد کردن آن نیز بکار می رود.

the Dead
سه شنبه 23 بهمن 1386, 07:19 صبح
به این دو سوال هم اگه لطف کنین جواب بدین!
1.در کد دیگری هنگام finalize از آزاد سازی managed resource ها خود داری شده آیا این معنیش اینه که اگر متد finalize اجرا شود خودش ازاد سازی managed resource ها را بعهده میگیرد؟
2.سوال بعدی اینکه جای خط // Dispose managed resources. چه کدی باید نوشت؛ به خاطر اینکه آزاد سازی managed resources به عهده Garbage Collector است و نیازی به دخالت ما نیست؟

public class MyResourceWrapper : IDisposable
{

private bool disposed = false;
public void Dispose()
{

CleanUp(true);
GC.SuppressFinalize(this);
}
private void CleanUp(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
// Dispose managed resources.
}
// Clean up unmanaged resources here.
}
disposed = true;
}
~MyResourceWrapper()
{
CleanUp(false);
}
}

Alireza_Salehi
سه شنبه 23 بهمن 1386, 08:49 صبح
به این دو سوال هم اگه لطف کنین جواب بدین!
1.در کد دیگری هنگام finalize از آزاد سازی managed resource ها خود داری شده آیا این معنیش اینه که اگر متد finalize اجرا شود خودش ازاد سازی managed resource ها را بعهده میگیرد؟
2.سوال بعدی اینکه جای خط // Dispose managed resources. چه کدی باید نوشت؛ به خاطر اینکه آزاد سازی managed resources به عهده Garbage Collector است و نیازی به دخالت ما نیست؟

public class MyResourceWrapper : IDisposable
{

private bool disposed = false;
public void Dispose()
{

CleanUp(true);
GC.SuppressFinalize(this);
}
private void CleanUp(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
// Dispose managed resources.
}
// Clean up unmanaged resources here.
}
disposed = true;
}
~MyResourceWrapper()
{
CleanUp(false);
}
}


این لینک ها رو ببینید:
Implementing Finalize and Dispose to Clean Up Unmanaged Resources (http://msdn2.microsoft.com/en-us/library/b1yfkh5e.aspx)
Object.Finalize Method (http://msdn2.microsoft.com/en-us/library/system.object.finalize.aspx)
Garbage Collection (http://msdn2.microsoft.com/en-us/library/0xy59wtx.aspx)

the Dead
سه شنبه 23 بهمن 1386, 10:35 صبح
این لینک ها رو ببینید:
Implementing Finalize and Dispose to Clean Up Unmanaged Resources (http://msdn2.microsoft.com/en-us/library/b1yfkh5e.aspx)
Object.Finalize Method (http://msdn2.microsoft.com/en-us/library/system.object.finalize.aspx)
Garbage Collection (http://msdn2.microsoft.com/en-us/library/0xy59wtx.aspx)
http://msdn2.microsoft.com/en-us/library/system.idisposable.aspx
این لینک درست توضیح داده بود که جریان از چه قراره
فقط برای اینکه ایهام من برطرف بشه فقط بفرمایین اگر ما قسمت قرمز را حذف کنیم و قصد ما هم این باشه که وقتی dispose() را صدا میزنیم فقط Unmanaged Resource آزاد بشه آیا بعد از اتمام برنامه managed Resource های این کلاس که ما در لحظه dispose دستی آزاد نکردیم به وسیله Garbage Collector آزاد میشوند؟

public class DisposeExample
{
// A base class that implements IDisposable.
// By implementing IDisposable, you are announcing that
// instances of this type allocate scarce resources.
public class MyResource: IDisposable
{
// Pointer to an external unmanaged resource.
private IntPtr handle;
// Other managed resource this class uses.
private Component component = new Component();
// Track whether Dispose has been called.
private bool disposed = false;

// The class constructor.
public MyResource(IntPtr handle)
{
this.handle = handle;
}

// Implement IDisposable.
// Do not make this method virtual.
// A derived class should not be able to override this method.
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}

// Dispose(bool disposing) executes in two distinct scenarios.
// If disposing equals true, the method has been called directly
// or indirectly by a user's code. Managed and unmanaged resources
// can be disposed.
// If disposing equals false, the method has been called by the
// runtime from inside the finalizer and you should not reference
// other objects. Only unmanaged resources can be disposed.
private void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if(!this.disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if(disposing)
{
// Dispose managed resources.
component.Dispose();
}

// Call the appropriate methods to clean up
// unmanaged resources here.
// If disposing is false,
// only the following code is executed.
CloseHandle(handle);
handle = IntPtr.Zero;

// Note disposing has been done.
disposed = true;

}
}

// Use interop to call the method necessary
// to clean up the unmanaged resource.
[System.Runtime.InteropServices.DllImport("Kernel32")]
private extern static Boolean CloseHandle(IntPtr handle);

// Use C# destructor syntax for finalization code.
// This destructor will run only if the Dispose method
// does not get called.
// It gives your base class the opportunity to finalize.
// Do not provide destructors in types derived from this class.
~MyResource()
{
// Do not re-create Dispose clean-up code here.
// Calling Dispose(false) is optimal in terms of
// readability and maintainability.
Dispose(false);
}
}
public static void Main()
{
// Insert code here to create
// and use the MyResource object.
}
}

Alireza_Salehi
سه شنبه 23 بهمن 1386, 10:56 صبح
نگران Managed Resource ها نباشید GC در زمان مناسب اونها رو آزاد میکنه. فقط به فکر Unmanaged ها باشید.