Calling a Chilkat Object's Dispose Method in C#

In C#, the Dispose() method is part of the IDisposable interface, and its purpose is to explicitly release unmanaged resources held by an object.

When you call a Chilkat object's Dispose method, such as Chilkat.Http.Dispose(), you're explicitly telling the Chilkat object to release any unmanaged resources it holds.

Although the internal details are not publicly documented line-by-line, here's a breakdown of what typically happens when calling Dispose() on a Chilkat object instance:


What Chilkat.Http.Dispose() Does Internally

Chilkat .NET objects are wrappers around native C++ libraries. Calling Dispose() on a Chilkat object:

  1. Frees native memory allocated by the underlying C++ Chilkat library.
  2. Releases any open sockets, file handles, or system resources associated with Internet connections.
  3. Nulls out internal unmanaged pointers to avoid dangling references.
  4. May detach event listeners or callbacks, if any were registered.

Why You Should Call It

Even if you're done using the Http object, or other Chilkat objects, unmanaged resources may stay alive until the garbage collector eventually finalizes the object — if it ever does.

Calling Dispose():

  • Immediately frees resources, rather than waiting for GC.
  • Prevents memory leaks and socket exhaustion in long-running apps or loops.
  • Ensures deterministic cleanup, which is especially important in resource-sensitive environments.

Best Practice

Use the using statement to ensure Dispose() is called automatically:

using (var http = new Chilkat.Http())
{
// Use http
} // Dispose() called automatically here

Or, if you manually manage the lifecycle:

var http = new Chilkat.Http();
// ... use http
http.Dispose(); // clean up explicitly

Asynchronous Methods and the TaskCompleted Callback

There are special considerations when calling the Async version of a Chilkat method. See the comments in the code below.


// This event fires from the background thread (inside Chilkat)
//
void http_OnTaskCompleted(object sender, Chilkat.TaskCompletedEventArgs args)
    {
    Chilkat.Task task = args.Task;

    // This event callback is running in the background thread.
    // To update a UI element, we must be on the UI thread..
    this.Invoke((MethodInvoker)delegate
    {
        bool downloadSuccess = task.GetResultBool();
        if (downloadSuccess)
            {
            textBox2.Text = "Download successful!";
            }
        else
            {
            textBox2.Text = task.ResultErrorText;
            }
    });

    // The task object holds a reference to the underlying HTTP implementation object.
    // If desired, we can get load the implementation object in a new Chilkat.Http instance.
    Chilkat.Http http = new Chilkat.Http();
    http.LoadTaskCaller(task);

    // For example, we could get the HTTP response status code for the Download method what was called (via DownloadAsync)
    this.Invoke((MethodInvoker)delegate
    {
        textBox2.AppendText("\r\n" + http.LastStatus.ToString() + "\r\n");
    });

    // There are now 2 references to the underlying implementation object.
    // One from our http object, and one within the task object.
    // To truly delete the underlying implementation object, we need to Dispose of each.
    task.Dispose();
    http.Dispose();
    }

private void asyncDownloadToolStripMenuItem_Click(object sender, EventArgs e)
    {
    // Create Http instance
    var http = new Chilkat.Http();

    // Define URL and local file path
    string url = "https://www.chilkatsoft.com/hamlet.xml";
    string localFilePath = "c:/temp/qa_output/hamlet.xml";

    // Create async task
    Chilkat.Task downloadTask = http.DownloadAsync(url, localFilePath);

    // Hook up the completion event
    http.OnTaskCompleted += http_OnTaskCompleted;

    //  Schedule the task for running on the thread pool.  This changes the task's state
    //  from Inert to Live.
    downloadTask.Run();

    //  The application is now free to do anything else
    //  while the HTTP method call is in progress.  When the task
    //  is completed, the TaskCompleted method is called.

	// ----------------------------------------------------------------------------------------------------------------------
    // Note: We can call the Http object's Dispose method here
    // because in actuality, the C# object holds a reference to the underlying implementation object.
    
    // When we called DownloadAsync, the Task object adds its reference to the underlying implementation object.
    
    // If we Dispose of the Http object here, we're simply removing our reference (i.e. decrementing the reference count)
    // and because the reference count is not yet 0, the underlying implementation object is not yet deleted (which is good,
    // because the background thread (within Chilkat) running our task..
    http.Dispose();
    }

Summary

Chilkat.Http.Dispose() cleans up native resources (like memory, handles, and connections) held by the underlying C++ Chilkat library. Always call it when you're done to avoid leaks — preferably via using.

Let me know if you want to see how this affects behavior in multithreaded or high-load scenarios.