-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Deprecate BSTR String constructor and setValue method #1300
Conversation
} | ||
// Null check the pointer | ||
Pointer p = getPointer().getPointer(0); | ||
return p != null ? new BSTR(p) : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either this null check is unnessary, or there is a potential problem in line 175.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If bstr
is null we never check line 175 and jump straight here. I believe this is the normal case when new BSTRByReference()
is instantiated before any value is set (e.g., before setValue()
is called)
return new BSTR(getPointer().getPointer(0)); | ||
// If memory address hasn't changed, return the stored BSTR | ||
if (bstr != null) { | ||
if (getPointer().getPointer(0).equals(bstr.getPointer())) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The null check could succeed and after that bstr is updated from a parallel thread and set to null. I suggest to copy the value into a local variable, read that and update bstr if necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True but this is starting to get to be too much work for a band-aid fix to a poor API to start with. I'm really leaning more towards a simple deprecation.
Thanks for the review, but the more I try to think about all the possible ways this approach needs to be protected against itself the more in favor I am of a simple and effective:
|
I pushed my alternate deprecation proposal which preserves the existing implementation and simply makes it more obvious to the user what's happening. If you prefer my earlier iteration, with more safeguards for thread safety I can go that route. |
I had a further thought about this: In my mind
It would be possible to implement both functions with the |
I would not change |
I still think it needs some explicit javadoc reference even if it's not deprecated. And there is a |
You mean you want to spell out in
Where do you see that in jna/contrib/platform/src/com/sun/jna/platform/win32/WTypes.java Lines 152 to 173 in 81a18de
I only see a |
Sorry, I misremembered. It's been a long week. You're right... I reverted to the original, added javadocs and a null check on the String output. Should be good to go I think. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me - I suggest to adjust the change log entry and get this in. Thank you.
Fixes #1292
As noted in the issue, this is already tested for in
c.s.j.p.ByReferencePlatformToStringTest
. I used the gc-based test code noted in the issue (and a few hundred instantiations in an array) to reproduce the issue at will before the fix and confirm this fixes that problem. I also added null checks.This does not protect against all cases, just the two most obvious ones (not throwing exceptions on null and tracking memory when a
BSTR
constructor is used).An alternative solution would be to deprecate the
BSTR
constructor and replace it with aPointer
constructor, making much more clear to the user what is being stored. I think I prefer this (deprecate) alternative but wanted to at least see what it would take to fix the existing one and get feedback.