Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having trouble closing a Window 1

Status
Not open for further replies.

darxpan

Programmer
Oct 14, 2003
33
US
I am programming using DirectX, but I don't think that is effecting the problem. In the Form code when I call "this.Close()", the window doesn't close. It starts to close. It calls the Closing event and runs it all, but then it never returns to the form that called it. The program freezes. I don't get an error. It just locks up.

Any suggestions would be appreciated.
 
When Close() method is called on a Form object then all resources created in this object are closed and finally the form is diposed by calling the code of
protected override void Dispose( bool disposing )
of this object.
Take a look on what you have in this function, the resources you create and if you release them
I think the form cannot close because some resources cannot be released.
-obislavu-
 
I have looked for resources that my not be getting released, but I am not sure what I am looking for. What sort of resources need to be released?

Also, there are several resources (the DriectPlay.Peer object) that were passed into the form and I am passing them back out. Would that have an effect on closing the window? I am doing an almost identical thing with another form and it seems to have no trouble. This is all very strange.
 
If is derived from Component then have implemented the right dipose().
If is an object from unmanaged code then call System.Runtime.InteropServices.Marshal.ReleaseComObject( obj).
-obislavu-
 
Sorry, but I didn't understand your last post. Could you please restate what you said?

Here is what I have found out. I call "this.Close()" in a form and the form doesn't close. I checked and it is locking up before it even gets to the Dispose() method of the form. It never gets run. I didn't change anything in it anyway, so its just the one that the VS designer made for me.

Also I have an exit button on the form and if I click it it closes like it is supposed to. It is only if I close after doing some setup stuff that I have trouble.
 
Ok, I fixed it. The solution was as strange as the problem. There was a panel that I changed from invisible to visible while the window was open. In order to get the window to close I had to hide the panel again. After that the window would close properly.

I did the same thing in a another window and it worked without having to hide the panel. There must be a bug in the MS code.
 
If you think the problem was solved then is Okay but I was talking about resource release.
Let say you have a Form or a User Control and there you have a System.Windows.Forms.Timer member.
The right way to release the timer is to stop it and after that to call dispose() when overriding Dispose() method. Example:
public class MyCtrl : System.Windows.Forms.UserControl

{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Timer MyTimer;
protected override void Dispose( bool disposing )
{
if( disposing )
{
MyTimer.Stop();
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
MyTimer.Dispose();

}
That is required beacuse MyTimer is derived from System.ComponentModel.Component.
As the MyTimer you could have other objects like Excel and in this case you shoul call ReleaseComObject().

-obislavu-
 
Thanks for your help. I have never understood what I was supposed to put in dispose() since C# has automatic garbage collection.
 
In general, you should not be as concerned about memory management in C#. This is because the .NET Framework garbage collector implicitly manages the allocation and release of memory for your objects. However, when your application encapsulates unmanaged resources such as windows, files, and network connections, you should use destructors to free those resources. When the object is eligible for destruction, the garbage collector runs the object's Finalize () method.
I put here some comments about Dispose() and Dispose (bool) methods.
Calling Dispose() allows the resources used by the Component to be reallocated for other purposes.
The Dispose(bool) releases the unmanaged resources used by the Component and optionally releases the managed resources.
This method is called by the public Dispose() method and the Finalize method.
Dispose() invokes the protected Dispose(Boolean) method with the disposing parameter set to true.
Finalize() invokes Dispose(Boolean) with disposing set to false.
When the disposing parameter is true, this method releases all resources held by any managed objects that this Component references.
Inside Dispose(bool) , the value of disposing is checked.
If it is true, some cleanup should be done here and disposing is set to TRUE
Here is an example of how to implement the Dispose(Boolean):
public class MyClass1
{
}
public class MyClass2
{
}
public class MyComponent: System.ComponentModel.Component

{

MyClass1 m_Obj1;
MyClass2 m_Obj2;
bool m_IsDisposed; // true if the component is disposed
public MyComponent()
{
m_IsDisposed = false;
m_Obj1 = new MyClass1();
m_Obj2 = new MyClass2();
}
~MyComponent()
{
Dispose(false);
}
protected override void Dispose(bool disposeAll )
{
if (!m_IsDisposed)
{
if (disposeAll)
{
// Here put code to free all managed resource
m_Obj1 = null;
m_Obj2 = null;
m_IsDisposed = true;
}
// Here put code to release unmanaged resource
base.Dispose(disposeAll);
}
}

}

Recall that, by convention, when disposeAll is TRUE , all resources are to be FREED.
When it is FALSE, only unmanaged resource are to be FREED.
Finnaly , Dispose(bool) as implemented by the base class (in this case Component) is called.
This ensures that any resours used by the base class are released.
In the above example there is a destructor.
The destructor implicitly calls the Finalize() method on the object's base class.
The above destructor code is implicitly translated to:
protected override void Finalize()
{
try
{

Dispose(false);
}
finally
{
base.Finalize();
}
}

2. If you use Designer to create a very sample windows application with form, it generates automatically the following code:

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
This means that the Finalize() method is called recursively for all of the instances in the inheritance chain, from the most derived to the least derived.
The programmer has no control on when the destructor is called because this is determined by the garbage collector. The garbage collector checks for objects that are no longer being used by the application. It considers these objects eligible for destruction and reclaims their memory. Destructors are also called when the program exits.

-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top