G
Guest
I understand the basics of finalization and implementing IDisposable and how one is guaranteed access to managed objects only when working through the IDisposable interface.
My question is to what extent can I rely on access to my private data during the finalization process when *not* coming through the IDisposable interface? Are private members still valid or are they managed objects that may already be gone? Are some types accessible (int, char, double) but others not (string)?
Here is my dilemma... class A provides management for the creation and use of objects of class B, including marking these objects as "in use" (and unmarking them) out in the external world. Each B object instantiated keeps a reference to the A object used to create it. Class B implements the IDisposable interface and I want to ensure that the external "in use" flag for a B object is properly cleaned up no matter how the B object is being disposed of. Here is some pseudocode:
class A
{
public B CreateB(int ID)
{
if (MarkInUse(ID))
return new B(this, ID);
else
return null;
}
public bool MarkInUse(int ID) { ... }
public void MarkNotInUse(int ID) { ... }
}
class B : IDisposable
{
private A m_Manager;
private int m_ID;
public B(A manager, int ID)
{
m_Manager = manager;
m_ID = ID;
}
~B() { Dispose(false); }
public Dispose()
{
Dispose(true);
GC.SupressFinalize();
}
protected Dispose(bool disposing)
{
if (disposing)
// Clean up managed resources here
// Now clean up external resources
m_Manager.MarkNotInUse(m_ID); // ??? Is this valid ???
}
}
Can I still refer to 'm_Manager' here? I know I can do so reliably in the "if (disposing)" conditional -- but I need to unmark the object even if the calling code forgets to call Dispose on my object! I could duplicate the code necessary to unmark the object but need to keep two strings and an int to be able to do it. I'm reasonably sure that 'int' private data is accessible during finalization, but how about strings?
Thanks!
-- TB
My question is to what extent can I rely on access to my private data during the finalization process when *not* coming through the IDisposable interface? Are private members still valid or are they managed objects that may already be gone? Are some types accessible (int, char, double) but others not (string)?
Here is my dilemma... class A provides management for the creation and use of objects of class B, including marking these objects as "in use" (and unmarking them) out in the external world. Each B object instantiated keeps a reference to the A object used to create it. Class B implements the IDisposable interface and I want to ensure that the external "in use" flag for a B object is properly cleaned up no matter how the B object is being disposed of. Here is some pseudocode:
class A
{
public B CreateB(int ID)
{
if (MarkInUse(ID))
return new B(this, ID);
else
return null;
}
public bool MarkInUse(int ID) { ... }
public void MarkNotInUse(int ID) { ... }
}
class B : IDisposable
{
private A m_Manager;
private int m_ID;
public B(A manager, int ID)
{
m_Manager = manager;
m_ID = ID;
}
~B() { Dispose(false); }
public Dispose()
{
Dispose(true);
GC.SupressFinalize();
}
protected Dispose(bool disposing)
{
if (disposing)
// Clean up managed resources here
// Now clean up external resources
m_Manager.MarkNotInUse(m_ID); // ??? Is this valid ???
}
}
Can I still refer to 'm_Manager' here? I know I can do so reliably in the "if (disposing)" conditional -- but I need to unmark the object even if the calling code forgets to call Dispose on my object! I could duplicate the code necessary to unmark the object but need to keep two strings and an int to be able to do it. I'm reasonably sure that 'int' private data is accessible during finalization, but how about strings?
Thanks!
-- TB