On Thu, 27 Jun 2024 11:26:24 GMT, Julian Waters <jwat...@openjdk.org> wrote:

> …I believe I was referring to the use of C++'s std::unique_ptr, which has the 
> functionality for cleanup that we need.

Yes, `std::unique_ptr` could be useful for handling automatic deallocation of 
objects created with the `new` operator.

It won't help with releasing references to Java objects though.

> That is currently blocked by awt.dll not being allowed to use the C++ 
> Standard Library, but if one looks at the current awt.dll with dumpbin, 
> awt.dll is actually already is linked to msvcp.dll, meaning it already uses 
> C++ Standard Library somehow (I don't know what exactly makes it dependent on 
> msvcp.dll)

I remember STL has been banned from AWT code, yet I don't know the reasons for 
it. It could be to keep the size smaller.

I can see that `awt.dll` depends on `msvcp140.dll` and imports one function 
from it: `?_Xlength_error@std@@YAXPEBD@Z`. It doesn't look like the C++ 
Standard Library is used. I can't find where it comes from though… It could be 
worth getting rid of this dependency.

I built a small sample app which uses `std::unique_ptr` and `std::make_unique` 
and it does not depend on `msvcp140.dll` at all. This makes sense, template 
libraries are header-only and will become part of the executable (or the 
dynamic library).

The only references to `std` namespace that I found in client are in 
`ShellFolder.cpp` which uses `std::bad_alloc`, it uses this type in `catch` 
blocks and it may throw an object of this class.

So, it looks `awt.dll` doesn't use the C++ Standard Library.

However, using C++ objects with 
[RAII](https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization) 
makes the code shorter and frees the programmer from repeating clean-up blocks¹.

Indeed, it's already used by C++ classes in AWT: 
[`JNILocalFrame`](https://github.com/openjdk/jdk/blob/79a23017fc7154738c375fbb12a997525c3bf9e7/src/java.desktop/windows/native/libawt/windows/awt_Toolkit.h#L64-L77)
 and 
[`CriticalSection`](https://github.com/openjdk/jdk/blob/79a23017fc7154738c375fbb12a997525c3bf9e7/src/java.desktop/windows/native/libawt/windows/awt_Toolkit.h#L85-L119)
 along with its `Lock` class inside.

¹ Repeated clean-up blocks can be seen in #18584 in nearly all the files.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/19867#issuecomment-2194603793

Reply via email to