diff --git a/client/src/include/lib3270/ipc.h b/client/src/include/lib3270/ipc.h index 1b8705d..4c79eb4 100644 --- a/client/src/include/lib3270/ipc.h +++ b/client/src/include/lib3270/ipc.h @@ -100,6 +100,42 @@ */ TN3270_PUBLIC std::vector getActions(); + /** + * @brief Lib3270 dynamic memory block wrapper. + * + */ + template + class lib3270_auto_cleanup { + private: + T *data; + + public: + lib3270_auto_cleanup(T *data) { + this->data = data; + } + + ~lib3270_auto_cleanup() { + lib3270_free((void *) this->data); + } + + operator bool() const noexcept { + return this->data != NULL; + } + + T * operator->() { + return this->data; + } + + operator T *() const noexcept { + return this->data; + } + + }; + + /** + * @brief TN3270 Event. + * + */ class TN3270_PUBLIC Event { public: diff --git a/client/src/session/local/get.cc b/client/src/session/local/get.cc index b187d4e..fa1f5bf 100644 --- a/client/src/session/local/get.cc +++ b/client/src/session/local/get.cc @@ -46,39 +46,39 @@ std::lock_guard lock(const_cast(this)->sync); - lib3270_autoptr(char) text = lib3270_get_string_at_address(hSession, 0, -1, '\n'); + lib3270_auto_cleanup text = lib3270_get_string_at_address(hSession, 0, -1, '\n'); if(!text) { throw std::runtime_error( _("Can't get screen contents") ); } - return std::string(text); + return std::string((char *) text); } std::string Local::Session::get(int baddr, int len, char lf) const { std::lock_guard lock(const_cast(this)->sync); - lib3270_autoptr(char) text = lib3270_get_string_at_address(hSession, baddr, len, lf); + lib3270_auto_cleanup text = lib3270_get_string_at_address(hSession, baddr, len, lf); if(!text) { throw std::runtime_error( _("Can't get screen contents") ); } - return std::string(text); + return std::string((char *) text); } std::string Local::Session::get(unsigned int row, unsigned int col, int len, char lf) const { std::lock_guard lock(const_cast(this)->sync); - lib3270_autoptr(char) text = lib3270_get_string_at(hSession, row, col, len, lf); + lib3270_auto_cleanup text = lib3270_get_string_at(hSession, row, col, len, lf); if(!text) { throw std::runtime_error( _("Can't get screen contents") ); } - return std::string(text); + return std::string((char *) text); } diff --git a/client/src/testprogram/testprogram.cc b/client/src/testprogram/testprogram.cc index 35b20ac..3ee0e5c 100644 --- a/client/src/testprogram/testprogram.cc +++ b/client/src/testprogram/testprogram.cc @@ -171,7 +171,7 @@ int main(int argc, char **argv) { - const char * session = ":a"; + const char * session = ""; // ":a"; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant" -- libgit2 0.21.2