4

Diagnostics: When Calling ServerTextControl.Create() Failed

 1 year ago
source link: https://www.textcontrol.com/blog/2022/06/29/diagnostics-when-calling-servertextcontrolcreate-fails/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

When Calling ServerTextControl.Create() Failed

Under rare conditions, the creation of a new ServerTextControl instance can fail and the Dispose method returns false. In these cases, the instance itself exists, but cannot be used. For example, a typical error when loading a document with an instance that could not be created is:

The TXTextControl object must have been completely loaded to use this method.

In these cases, the Create method returned false and the instance should not be used.

Restart IIS?

Some users report that only an IIS restart helps to resolve this situation. Technically, a restart helps as the process is re-created and existing USER handles are being released. But in fact, only USER handles must be released and there is no need to restart IIS or the worker process.

Possible Reasons

The most typical reason that a ServerTextControl cannot be created is that it is not possible to create a new Window handle. In Windows, there is a limit of 10,000 USER handles and 10,000 GDI objects handles. If this number is exceeded, it is not possible to create a new ServerTextControl instance. In typical applications, it is very most unlikely that this number of handles is created.

Sometimes, we see reports from users that this issue is caused after several days and very inconsistent or sporadic. Most likely, in these cases, the ServerTextControl instances are not properly disposed. It is very important to dispose ServerTextControl objects explicitly. We recommend to use them in a using statement:

A better way would be to check whether the Create method has been called successfully:

ServerTextControl Factory Class

To monitor the created instances, we implemented a factory class that returns valid instances of ServerTextControl. This diagnostics class can be also used to check the number of active instances, USER handles and GDI handles. Additionally, it can be used to dispose all active instances.

Using the Class

This class uses the singleton pattern and can be created only once in an application:

Once created, the CreateServerTextControl method creates and returns a new instance:

When using this factory class, it is not required to create the ServerTextControl before using it. This is handled in the factory itself.

The ServerTextControlFactory creates and returns a new instance of ServerTextControl and stores it in a list. In case the instance cannot be created, the class returns null. When the instance is disposed, it is removed from that list again automatically. The method ServerTextControlFactory.Count returns the number of active ServerTextControl instances.

When executing the following code, the created ServerTextControl instance is disposed explicitly in the using statement. Therefore, the counter should be 0 after a successful execution.

Number of instances: 1
Number of instances: 0

In case the ServerTextControl is not disposed, the active instance stays in the list until disposed:

Number of instances: 0
Number of instances: 1

Debugging

When using this factory class in your application, you can always monitor the number of instances to find leaks. In case you would like to dispose all instances - for debugging purposes - the factory class provides the DisposeAll method that loops through all stored instances in order to dispose them:

Number of instances: 0
Number of instances: 1
Number of instances: 2
Number of instances: 3
Number of instances: 4
Number of instances: 5
Disposed instances: 5

Handle Counter

Additionally, the factory class provides properties to return the number of USER and GDI handles in the current process:

USER handles: 30
GDI handles: 26

Further Help

If you need assistance of further help debugging these kind of problems, talk to our engineers - we are here to help!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK