Software Development

[.NET] ToolTip and "Cannot access a disposed object" exception

The "Cannot access a disposed object" exception is a nightmare for .NET developers.
Like
by Jey Geethan | December 12, 2008
[.NET] ToolTip and "Cannot access a disposed object" exception

The "Cannot access a disposed object" exception is a nightmare for .NET developers. ;) Sometimes, this exception causes the worst backslash for a development project. And looking at exception details, we will find that the call stack will point to


"at System.Windows.Forms.Control.CreateHandle()
 at System.Windows.Forms.Control.get_Handle() "


Or


"at System.Windows.Forms.Form.CreateHandle()
 at System.Windows.Forms.Form.get_Handle() "


Since, we do not know from where the problem stems, it becomes a trial-and-error method to find out the source of the problem. After some days of research, I found out the following facts about the problem. Here it goes.

Reason :

This exception occurs only when we try to access any disposed object, as it says. But the problem is, we would have never written statements that explicitly access a disposed object! So the real reason would be some unseen code that accesses the disposed Form or Object. In my case, the black sheep was the ToolTip control.

I had used the ToolTip to show messages on different forms. But the internal architecture of the ToolTip has been coded by a novice at Microsoft i guess, and its faulty. By going through reflection i found that it uses a variable to store the current form in which it the message is being displayed. I had used the same instance of ToolTip to show messages on other forms too. When I used the ToolTip to show messages on another form after the previous form has been closed, the ToolTip tries to access the form in its private variable (the closed/disposed form) and boom! It throws the exception!

Another instance where this can happen is - Dockable Container, this happens with ToolTip when the container is changed/docked to another parent. Now since, the parent is changed, when the ToolTip tries to access the parent and it is already disposed. So the exception is thrown.

Solution:

The solution I devised was to create a new ToolTip instance for every Form that I showed on the screen. This worked around the problem and it was solved. For those, with Dockable Container problem, try to recreate the controls when they are moved or docked. This will solve the problem.

And microsoft is supposed to have corrected this problem in v3.5 framework. But I haven't checked it out yet.


Jey Geethan

Jey Geethan is a poet, author and an entrepreneur.


Related Articles

Why Microservices?
Software Development

Why Microservices?

by Jey Geethan | December 16, 2022
I have a few tidbits about why to use microservices and why it makes sense to create few microservices as a side project and learn from the same
Top 5 Tips for Software Development Managers To Conduct Effective 1 on 1s With Your Reports
Software Development

Top 5 Tips for Software Development Managers To Conduct Effective 1 on 1s With Your Reports

by Jey Geethan | October 05, 2020
Some of the effective tips that can enhance your 1 on 1s with your reports. In 2020, software development is one of the major industries which provides millions of jobs. In fact, if you look at the statistics, this industry is poised to grow even further. According to Slashdata, it's expected to reach 45 million developers by 2030. As a manager, it's your responsibility that 1 on 1s are effective and help your reports. I want to talk about a few of the things that make this process really effective.
Do you want to use your macbook as a lap-heater?
Software Development

Do you want to use your macbook as a lap-heater?

by Jey Geethan | August 28, 2019
How many times have you felt the coldness of a meeting room and wish you had brought a heater into the room. Now you can convert your macbook into a heater
How to get informative, color prompts on Mac Terminal or iTerm2
Software Development

How to get informative, color prompts on Mac Terminal or iTerm2

by Jey Geethan | August 14, 2019
This article talks about how to get color prompts on your terminal and add some informative things like current git branch and distinctive colors for current path, username etc.
Can you have a dinner conversation around Pull Requests?
Software Development

Can you have a dinner conversation around Pull Requests?

by Jey Geethan | July 11, 2019
I believe that writing code is a way of communication and it enables developers understand better than just talking about the concepts or logic in plain english words.