Skip to content

Commit

Permalink
std::unique_ptr awt_Canvas.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
TheShermanTanker authored Nov 10, 2023
1 parent e30d680 commit 7fe76d5
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions src/java.desktop/windows/native/libawt/windows/awt_Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
* questions.
*/

#include <functional>
#include <memory>
#include <type_traits>

#include "awt_Toolkit.h"
#include "awt_Canvas.h"
#include "awt_Win32GraphicsConfig.h"
Expand Down Expand Up @@ -205,31 +209,13 @@ MsgRouting AwtCanvas::HandleEvent(MSG *msg, BOOL synthetic)
void AwtCanvas::_SetEraseBackground(void *param) {
JNIEnv *env = (JNIEnv *) JNU_GetEnv(jvm, JNI_VERSION_1_2);

class ResourceGuard final {
JNIEnv* env;
SetEraseBackgroundStruct* background;
jobject canvas;

public:
ResourceGuard(JNIEnv* env, SetEraseBackgroundStruct* background, jobject canvas)
: env(env), background(background), canvas(canvas) {

}
~ResourceGuard() {
env->DeleteGlobalRef(canvas);
delete background;
}
};

SetEraseBackgroundStruct *sebs = static_cast<SetEraseBackgroundStruct *>(param);
jobject canvas = sebs->canvas;
std::unique_ptr<SetEraseBackgroundStruct> sebs(static_cast<SetEraseBackgroundStruct *>(param));
std::unique_ptr<std::remove_pointer<jobject>::type, std::function<void (jobject)>> canvas(sebs->canvas, [&env] (jobject canvas) -> void { env->DeleteGlobalRef(canvas); });
jboolean doErase = sebs->doErase;
jboolean doEraseOnResize = sebs->doEraseOnResize;

ResourceGuard guard(env, sebs, canvas);

PDATA pData;
JNI_CHECK_PEER_RETURN(canvas);
JNI_CHECK_PEER_RETURN(canvas.get());

AwtCanvas *c = (AwtCanvas*) pData;
c->m_eraseBackground = doErase;
Expand Down

0 comments on commit 7fe76d5

Please sign in to comment.