Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MustDisposeResource is not allowed on struct types #31

Open
tvardero opened this issue May 24, 2024 · 0 comments
Open

MustDisposeResource is not allowed on struct types #31

tvardero opened this issue May 24, 2024 · 0 comments

Comments

@tvardero
Copy link

tvardero commented May 24, 2024

As of version 2023.3.0, it is not allowed to annotate struct types with attribute MustDisposeResource.

My example would be this:

[MustDisposeResource]
public struct ResourceToken<TResource> : IDisposable
where TResource : ResourceBase
{
    private readonly ResourceManager _manager;
    private readonly object lockObject = new();
    private TResource _value;
    private bool _disposed;

    public ResourceToken(ResourceManager manager, TResource value) { ... }

    public readonly TResource Value { ... }

    public void Dispose()
    {
        if (_disposed) return;

        lock (lockObject)
        {
            if (_disposed) return;

            _manager.NotifyTokenDisposed(_value.InstanceId);
            
            _value = null!;
            _disposed = true;
        }
    }
}

This ResourceToken is used for reference tracking for loaded resources, so I could release resource when it is no longer in use by consumers. Token should be disposed, otherwise resource reference count will leak.
It is struct, and the attribute is valid only on properties, classes and fields.

Please allow using this attribute on structs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant