• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2029
  • Last Modified:

System.ObjectDisposedException: Cannot access a disposed object named "UltraGrid".

hi
      I am getting error like
            System.ObjectDisposedException: Cannot access a disposed object named "UltraGrid".
Object name: "UltraGrid".
   at System.Windows.Forms.Control.CreateHandle()
   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


      I have a grid, in grid row select change event i am creating a some controls in a panel dynamically. before creating the contorl i am disposing the contorls(if control != null) from the panel(This is for avoid memory leaks).if i try clicking quickly from one row then to another on a slow machine in the grid, i am getting the above error. If am not disposing controls it will work properly. but there is a memory leake. this is the code i used for dispose contorls

      foreach(System.Windows.Forms.Control ctrl in panel.Controls)
                        {
                              if (ctrl != null)
                              {
                                    ctrl.Dispose();
                              }
                        }

Please any one give me a solution why this error is occuring.
0
sabeesh
Asked:
sabeesh
  • 4
  • 4
  • 2
  • +2
1 Solution
 
Daniel Van Der WerkenIndependent ConsultantCommented:
Use the Garbage Collector static class:

GC.Collect();

instead of that foreach() loop.  
0
 
AlexFMCommented:
Just remove this loop, it is impossible to make memory leaks using managed code.
0
 
devsolnsCommented:
Agreed, whether you call it or not dispose will get called.  If not by you then by the finalizer.  Is it important to do so here, probably not.  Even with you calling disposed it doesnt mean it will be garbage collection ready as there still may be refs to it.

Definatly do not get into calling GC.Collect(), that would be a bad idea.  It should be used by someone who truly knows if it will indeed have any value and here it does NOT.

Explicityly releasing unmanaged resources "inside of Dipose()" implementation may or may not add performance value.  Things like database connections on the other hand or different.

Keep in mind this pattern,

If an object implements IDisposable then you can use

using(MyObj mo = new MyObj())
{
     //use object
}

which rougly translates to

MyObj mo = null;
try {
     mo = new MyObj();
     //use object
}
finally {
     if(mo != null)mo.Dispose();
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
devsolnsCommented:
And the other thing i forgot to mention is that exception is because your trying to performan an operation on an object that has already been disposed.  Perhaps somewhere else code is trying to make use.  Set ref to null will help.
0
 
sabeeshAuthor Commented:
No where it is refering , once cleared all controls from panel it will create a new set of contorl and even it is a managed code it will dispose all the controls only after close the form. so i have to dispose manually.
0
 
AlexFMCommented:
Why do you care about this?
0
 
sabeeshAuthor Commented:
Why i want to dispose all the controls in panel is ,GC wont call when i was clicking from one row to other.so i have to manually dispose the controls in a panel before i  am  creating a new set of controls in panel w.r.to grid row select change.
0
 
Daniel Van Der WerkenIndependent ConsultantCommented:
Okay, here's what you do.

When the control is created, setup a callback function for when it's deleted.

For example,

                  InfoForm myF = new InfoForm();
                  myF.HandleDestroyed += new EventHandler(myF_HandleDestroyed);

Then, in the callback, do the Displose.  Or something like this.
0
 
devsolnsCommented:
"Why i want to dispose all the controls in panel is ,GC wont call when i was clicking from one row to other.so i have to manually dispose the controls in a panel before i  am  creating a new set of controls in panel w.r.to grid row select change."

This isn't making sense to me.  Sure you can call dispose if you'd like but saying you "have to" in order for that to work is wrong.  I dont do a lot of .NET UI programming but I think I can saftely you are never  "required" to release unmanaged resources before doing anything else.  That would imply they are  sharing the same underlying unmanaged resource.  Perhaps it would be best to see all of your code.
0
 
devsolnsCommented:
Ok i see, that is the only method to remove controls unlike most IDisposable objects I work with use the Close/Dispose pattern.

Anyway lets see your entire source.
0
 
sabeeshAuthor Commented:
public void ClearCardView()
            {
                  if (_placementInfo != null)
                  {
                        _placementInfo = null;
                        
                        foreach(System.Windows.Forms.Control ctrl in panel.Controls)
                        {
                              if (ctrl != null)
                              {
                                    ctrl.Dispose();                                    
                              }
                        }                        
                  }
                  panel.Controls.Clear();
            }

protected override void OnGridAfterSelectChange(object sender, System.Object rowObject, System.EventArgs e)
            {
                  
                        ClearCardView();
                        panel.Controls.Clear();
                        _placementInfo = (rowObject as RDB.BusinessInfo.rdbPlacementInfo);

                        if (_placementInfo != null)
                        {
                              try
                              {
                                    switch (_placementInfo.PlacementSystemCode)
                                    {
                                          case rdbSystemCodes.Permanent:
                                                DisplayPlacementCardView(_placementInfo);
                                                break;
                                          case rdbSystemCodes.Contract:
                                                DisplayContractPlacementCardView(_placementInfo);
                                                break;
                                          case rdbSystemCodes.Temporary:
                                                DisplayContractPlacementCardView(_placementInfo);
                                                break;
                                          default:
                                                DisplayContractPlacementCardView(_placementInfo);
                                                break;
                                    }
                              }                        
                              catch (Exception ex)
                              {
                                    rdbErrorDispenser.ShowError("DISPLAY_DATA_FAILED", ex.Message,
                                          RDBApp.UserPreferenceInfo.LogErrors);                              
                              }
                        }
                  
            }

private void DisplayPlacementCardView(RDB.BusinessInfo.rdbPlacementInfo pPlacementInfo)
            {
                  UI.UserControls.ucControlGrid ctrlGrid = new RDB.UI.UserControls.ucControlGrid();    // Create the ControlGrid
                  ctrlGrid.Location = new System.Drawing.Point(0, 0);
                  ctrlGrid.Width = panel.Width;
                  ctrlGrid.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right ;
                  ctrlGrid.AddUserControl(GetPhoneCard());
                  ctrlGrid.AddUserControl(GetPlacementCardView(pPlacementInfo));
                  ctrlGrid.Parent = panel;
                  panel.AutoScroll = true;
            }
0
 
sabeeshAuthor Commented:
Hi
 I got the solution for this. this error is because of,before paint process happening the object got distoroyed.
I used ctrlGrid.Update() method for forfully do the paint process.
0
 
NetminderCommented:
Closed, 125 points refunded.
Netminder
Site Admin
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now