diff --git a/src/core/host.c b/src/core/host.c index f19380d..ac6ddd8 100644 --- a/src/core/host.c +++ b/src/core/host.c @@ -57,6 +57,7 @@ #include #include +#include #include #include #include @@ -321,7 +322,7 @@ LIB3270_EXPORT const char * lib3270_get_default_host(const H3270 GNUC_UNUSED(*hS { #ifdef _WIN32 { - HKEY hKey; + lib3270_auto_cleanup(HKEY) hKey; DWORD disp = 0; LSTATUS rc = RegCreateKeyEx( HKEY_LOCAL_MACHINE, @@ -350,8 +351,6 @@ LIB3270_EXPORT const char * lib3270_get_default_host(const H3270 GNUC_UNUSED(*hS DWORD dwRet = RegQueryValueEx(hKey,"host",NULL,NULL,(LPBYTE) default_host, &cbData); - RegCloseKey(hKey); - if(dwRet == ERROR_SUCCESS) { default_host = (char *) realloc(default_host,cbData+1); diff --git a/src/core/session.c b/src/core/session.c index 1e96224..589592d 100644 --- a/src/core/session.c +++ b/src/core/session.c @@ -359,7 +359,7 @@ static void lib3270_session_init(H3270 *hSession, const char *model, const char #ifdef _WIN32 // Get defaults from registry. { - HKEY hKey; + lib3270_auto_cleanup(HKEY) hKey = 0; DWORD disp = 0; LSTATUS rc = RegCreateKeyEx( HKEY_LOCAL_MACHINE, @@ -398,7 +398,6 @@ static void lib3270_session_init(H3270 *hSession, const char *model, const char } - RegCloseKey(hKey); } } #endif // _WIN32 diff --git a/src/core/toggles/init.c b/src/core/toggles/init.c index dec7d18..a9fbc5f 100644 --- a/src/core/toggles/init.c +++ b/src/core/toggles/init.c @@ -163,7 +163,7 @@ void initialize_toggles(H3270 *session) #ifdef _WIN32 { - HKEY hKey; + lib3270_auto_cleanup(HKEY) hKey = 0; DWORD disp = 0; LSTATUS rc = RegCreateKeyEx( HKEY_LOCAL_MACHINE, @@ -201,7 +201,6 @@ void initialize_toggles(H3270 *session) } } - RegCloseKey(hKey); } } diff --git a/src/include/lib3270.h b/src/include/lib3270.h index c8cd256..13e4213 100644 --- a/src/include/lib3270.h +++ b/src/include/lib3270.h @@ -56,6 +56,7 @@ #define LIB3270_AUTOPTR_FUNC_NAME(TypeName) lib3270_autoptr_cleanup_##TypeName #define lib3270_autoptr(TypeName) TypeName * __attribute__ ((__cleanup__(LIB3270_AUTOPTR_FUNC_NAME(TypeName)))) + #define lib3270_auto_cleanup(TypeName) TypeName __attribute__ ((__cleanup__(LIB3270_AUTOPTR_FUNC_NAME(TypeName)))) #endif // __GNUC__ diff --git a/src/include/windows/lib3270/os.h b/src/include/windows/lib3270/os.h index c4237f3..9b6ede6 100644 --- a/src/include/windows/lib3270/os.h +++ b/src/include/windows/lib3270/os.h @@ -41,4 +41,11 @@ #include #include + inline void lib3270_autoptr_cleanup_HKEY(HKEY *hKey) { + if(*hKey) { + RegCloseKey(*hKey); + *hKey = 0; + } + } + #endif // LIB3270_OS_H_INCLUDED -- libgit2 0.21.2