Use g_new0 and g_free to generate raw strings

This commit is contained in:
LAGonauta 2021-03-02 07:11:21 -03:00
parent 94944a9e2a
commit 8856fcbceb
4 changed files with 7 additions and 4 deletions

View file

@ -2,6 +2,7 @@
#include <string>
#include <string_view>
#include <glib.h>
std::wstring sview_to_wstr(const std::string_view str);
char* wsview_to_char(const std::wstring_view wstr);
gchar* wsview_to_char(const std::wstring_view wstr);

View file

@ -10,7 +10,7 @@ char* wsview_to_char(const std::wstring_view wstr)
return nullptr;
}
int final_size = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), nullptr, 0, nullptr, nullptr);
char* strTo = new char[final_size + 1]{ 0 };
gchar* strTo = g_new0(gchar, final_size + 1);
WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), strTo, final_size, nullptr, nullptr);
return strTo;
}

View file

@ -270,7 +270,9 @@ winrtEventToken* winrt_windows_ui_notifications_toast_notification_Activated(win
}
}
callback(wsview_to_char(arguments), nullptr /* user_input */ , 0 /* user_input.size() */, context);
auto args = wsview_to_char(arguments);
callback_data->callback(args, nullptr /* user_input */ , 0 /* user_input.size() */, callback_data->context);
g_free(args);
});
callback_data->SetToken(winrt_event_token_new_from_token(&token));

View file

@ -14,7 +14,7 @@ gboolean winrt_InitApartment()
{
auto message = wsview_to_char(e.message());
std::cerr << message << '\n';
delete[] message;
g_free(message);
if (e.code() == -2147417850 /* RPC_E_CHANGED_MODE */) // harmless
{
return true;