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

[Java.Interop] Check for disposed state in JniValueManager methods #239

Merged

Conversation

radekdoulik
Copy link
Member

@radekdoulik radekdoulik requested a review from jonpryor January 3, 2018 21:06
@radekdoulik radekdoulik force-pushed the pr-check-disposed-state-in-value-manager branch from d942d9a to 2721667 Compare January 3, 2018 21:09
@@ -44,9 +44,13 @@ partial class CreationOptions {
public abstract partial class JniValueManager : ISetRuntime, IDisposable {

public JniRuntime Runtime { get; private set; }
protected bool disposed = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally disposed is not protected, and I don't believe that it needs to be. MonoRuntimeValueManager can use RegisteredInstances instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, how about replacing it with public bool Disposed { get; private set; } = false; property? That shouldn't do any harm.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe protected IsDisposed as here https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.channels.message.isdisposed?view=netcore-2.0

As the Disposed name is used for events in .NET frameworks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an IsDisposed property won't do "harm" per-se, but I don't like it, and it is generally not something that is provided.

See for example:

In fact, I can (easily!) find only one place where an (Is)?Disposed-like property is present:

$ pwd
…/mono/mcs/class/corlib
$ git grep -l IDisposable System* | xargs grep Disposed
System.IO.IsolatedStorage/IsolatedStorageFile.cs:		internal bool IsDisposed {
System.IO.IsolatedStorage/IsolatedStorageFile.cs:				throw new ObjectDisposedException ("IsolatedStorageFile");
System.Security/SecureString.cs:					throw new ObjectDisposedException ("SecureString");
System.Security/SecureString.cs:				throw new ObjectDisposedException ("SecureString");
System.Security/SecureString.cs:				throw new ObjectDisposedException ("SecureString");
System.Security/SecureString.cs:				throw new ObjectDisposedException ("SecureString");
System.Security/SecureString.cs:				throw new ObjectDisposedException ("SecureString");
System.Security/SecureString.cs:				throw new ObjectDisposedException ("SecureString");
System.Security/SecureString.cs:				throw new ObjectDisposedException ("SecureString");

Note that it's internal, and all the others are not members.

What about corefx?

$ pwd
…/mono/external/corefx
$ git grep -l IDisposable src | xargs grep 'public.*Disposed' | grep -v tests/
src/System.ComponentModel.Primitives/ref/System.ComponentModel.Primitives.cs:        public event System.EventHandler Disposed { add { } remove { } }
src/System.ComponentModel.TypeConverter/ref/System.ComponentModel.cs:        public event System.EventHandler Disposed { add { } remove { } }
src/System.Runtime/ref/System.Runtime.cs:    public partial class ObjectDisposedException : System.InvalidOperationException
src/System.Runtime/ref/System.Runtime.cs:        public ObjectDisposedException(string objectName) { }
src/System.Runtime/ref/System.Runtime.cs:        public ObjectDisposedException(string message, System.Exception innerException) { }
src/System.Runtime/ref/System.Runtime.cs:        public ObjectDisposedException(string objectName, string message) { }

Two public events, not properties. Looking for protected members is no better.

TL;DR: Providing public/protected APIs to query the "disposed" status of an instance is not common. Providing such a method is decidedly uncommon, and I don't see any need to provide such an API here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not uncommon in the .NET framework. See https://docs.microsoft.com/en-us/dotnet/api/?view=netframework-4.7&term=IsDisposed

Some of these are abstract classes implementing IDisposable iface, which provide protected IsDisposed property. I think it makes sense to provide a single place to check for disposed status in this case, where we provide the dispose infrastructure as part of the abstraction.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice search URL; I wasn't aware of that.

That said, it finds 14 matches, most of which are System.Windows (7), System.Xaml (2), and System.Printing (2), leaving 3 in a nominally "core" namespace (System.Runtime, System.ServiceModel).

This is not what I'd call "not uncommon."

Alas, searching for IDisposable isn't entirely useful, as most matches are for explicitly implemented interface methods (bleh), so I guess searching for Dispose is the "better" bet. After hitting Load more results a lot -- and still not hitting any "end" -- there are (at least!) 213 matches for Dispose() Method.

Compare to 14.

This is not what I'd call "not uncommon"; it is very uncommon.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of these 213 matches are not abstract classes implementing IDisposable. So I would still say it is not uncommon, to clarify, for abstract classes.

It is not that important though, so I will update the PR and move along.

@radekdoulik radekdoulik force-pushed the pr-check-disposed-state-in-value-manager branch from 2721667 to b9f0ca9 Compare January 4, 2018 08:28
@radekdoulik radekdoulik force-pushed the pr-check-disposed-state-in-value-manager branch from b9f0ca9 to 5e7f8d3 Compare January 4, 2018 08:43
@jonpryor jonpryor merged commit a158b03 into dotnet:master Jan 4, 2018
@github-actions github-actions bot locked and limited conversation to collaborators Apr 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants