StormHub / NetUV

.Net standard/Core binding for Libuv
Other
76 stars 20 forks source link

When to Dispose Async #56

Closed oliverw closed 7 years ago

oliverw commented 7 years ago

Is it valid to Dispose an Async Handle within its callback like this?:

var disposer = loop.CreateAsync((handle) =>
{
    tcp.Dispose();

    handle.Dispose();
});

The purpose of this example is to schedule the disposal of another loop-bound resource on the loop thread.

StormHub commented 7 years ago

Yes it is, normal once off running for async handle. There are some cases where you need to worry about closure, I typically do something like this:

        asyncHandle = this.loop.CreateAsync(
            handle =>
            {
                var disposible = handle.UserToken as IDisposable;
                disposible?.Dispose();
                handle.Dispose();
            });
        asyncHandle.UserToken = tcp;   // Save to the handle cookie
        asyncHandle.Send();
oliverw commented 7 years ago

Thanks for the clarification.