From 2c6e4eed19fae97ac343e8471be048678123d6aa Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Thu, 8 Jul 2021 20:10:13 +0400 Subject: [PATCH] Use unique_ptr for GtkSelectionData --- .../platform/linux/linux_gtk_integration.cpp | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/Telegram/SourceFiles/platform/linux/linux_gtk_integration.cpp b/Telegram/SourceFiles/platform/linux/linux_gtk_integration.cpp index 35b352ea9..8b4eb26c2 100644 --- a/Telegram/SourceFiles/platform/linux/linux_gtk_integration.cpp +++ b/Telegram/SourceFiles/platform/linux/linux_gtk_integration.cpp @@ -68,6 +68,16 @@ constexpr auto kIntrospectionXML = R"INTROSPECTION( )INTROSPECTION"_cs; +struct GtkSelectionDataDeleter { + void operator()(GtkSelectionData *gsel) { + if (gsel) { + gtk_selection_data_free(gsel); + } + } +}; + +using GtkSelectionDataPointer = std::unique_ptr; + Glib::ustring ServiceName; bool GetImageFromClipboardSupported() { @@ -96,11 +106,11 @@ std::vector GetImageFromClipboard() { gdk_atom_intern("image/bmp", true), }; - const auto gsel = [&]() -> GtkSelectionData* { + const auto gsel = [&]() -> GtkSelectionDataPointer { for (const auto &format : supportedFormats) { - if (const auto result = gtk_clipboard_wait_for_contents( - clipboard, - format); gtk_selection_data_get_length(result) > 0) { + if (auto result = GtkSelectionDataPointer( + gtk_clipboard_wait_for_contents(clipboard, format)) + ; gtk_selection_data_get_length(result.get()) > 0) { return result; } } @@ -111,12 +121,8 @@ std::vector GetImageFromClipboard() { return {}; } - const auto guard = gsl::finally([&] { - gtk_selection_data_free(gsel); - }); - - const auto data = gtk_selection_data_get_data(gsel); - const auto length = gtk_selection_data_get_length(gsel); + const auto data = gtk_selection_data_get_data(gsel.get()); + const auto length = gtk_selection_data_get_length(gsel.get()); return std::vector(data, data + length); }