Skip to content

Commit

Permalink
fix memory leak: live_player_activity: re call the fn showDialog (#330)
Browse files Browse the repository at this point in the history
* fix win api

* Cancel the singleton mode of LiveDanmuku

* fix memory leak: live_player_activity: re call the fn showDialog

* del the incorrect manually destroy dialog box

* roll back live activity and lock the clearing of the msg_q

* Optimize WSAStartup to be called only once

* roll back, del MG_ENABLE_WINSOCK=0

* move WSAStartup to ProgramConfig::instance().init()

* use brls::Logger::error

* fix

---------

Co-authored-by: maye174 <[email protected]>
  • Loading branch information
maye76 and maye76 authored May 3, 2024
1 parent 31c9d11 commit a319bbe
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 32 deletions.
3 changes: 3 additions & 0 deletions wiliwili/include/activity/live_player_activity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "utils/event_helper.hpp"
#include "presenter/live_data.hpp"
#include "live/danmaku_live.hpp"

class VideoView;

Expand Down Expand Up @@ -49,6 +50,8 @@ class LiveActivity : public brls::Activity, public LiveDataRequest {
// 遇到错误重试的延时函数 handle
size_t errorDelayIter = 0;

LiveDanmaku danmaku;

bilibili::LiveVideoResult liveData;

//更新timeLabel
Expand Down
2 changes: 1 addition & 1 deletion wiliwili/include/api/live/danmaku_live.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
using json = nlohmann::json;

typedef void (*on_message_func_t)(const std::string &);
class LiveDanmaku : public brls::Singleton<LiveDanmaku> {
class LiveDanmaku {
public:
int room_id;
int uid;
Expand Down
8 changes: 4 additions & 4 deletions wiliwili/source/activity/live_player_activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "utils/shader_helper.hpp"
#include "utils/config_helper.hpp"

#include "live/danmaku_live.hpp"
#include "live/extract_messages.hpp"
#include "live/ws_utils.hpp"

Expand Down Expand Up @@ -279,6 +278,7 @@ void LiveActivity::onLiveData(const bilibili::LiveRoomPlayInfo& result) {
// todo: 支持轮播视频
this->video->showOSD(false);
showDialog("未开播", "pictures/sorry.png", true);
return;
}
brls::Logger::debug("current quality: {}", liveUrl.current_qn);
for (auto& i : liveUrl.accept_qn) {
Expand All @@ -304,8 +304,8 @@ void LiveActivity::onLiveData(const bilibili::LiveRoomPlayInfo& result) {
}

void LiveActivity::onDanmakuInfo(int roomid, const bilibili::LiveDanmakuinfo& info) {
LiveDanmaku::instance().setonMessage(onDanmakuReceived);
LiveDanmaku::instance().connect(roomid, std::stoll(ProgramConfig::instance().getUserID()), info);
danmaku.setonMessage(onDanmakuReceived);
danmaku.connect(roomid, std::stoll(ProgramConfig::instance().getUserID()), info);
}

void LiveActivity::onError(const std::string& error) {
Expand Down Expand Up @@ -367,7 +367,7 @@ void LiveActivity::retryRequestData() {

LiveActivity::~LiveActivity() {
brls::Logger::debug("LiveActivity: delete");
LiveDanmaku::instance().disconnect();
danmaku.disconnect();
// 取消监控mpv
APP_E->unsubscribe(event_id);
MPV_E->unsubscribe(tl_event_id);
Expand Down
21 changes: 4 additions & 17 deletions wiliwili/source/api/danmaku_live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
#include <queue>
#include <condition_variable>
#include <string>
#ifdef _WIN32
#include <winsock2.h>
#endif

namespace bilibili {
void BilibiliClient::get_live_danmaku_info(int roomid, const std::function<void(LiveDanmakuinfo)> &callback,
Expand Down Expand Up @@ -45,21 +42,12 @@ static void add_msg(std::string &&a) {
cv.notify_one();
}

LiveDanmaku::LiveDanmaku() {
#ifdef _WIN32
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != 0) {
printf("WSAStartup failed with error: %d\n", result);
}
#endif
}
LiveDanmaku::LiveDanmaku() {}

LiveDanmaku::~LiveDanmaku() {
disconnect();
#ifdef _WIN32
WSACleanup();
#endif
std::lock_guard<std::mutex> lock(msg_q_mutex);
while (!msg_q.empty()) msg_q.pop();
}

void LiveDanmaku::connect(int room_id, int64_t uid, const bilibili::LiveDanmakuinfo &info) {
Expand Down Expand Up @@ -132,8 +120,7 @@ void LiveDanmaku::disconnect() {
connected.store(false, std::memory_order_release);

// Wakeup the mainloop
if (mgr && nc)
mg_wakeup(this->mgr, this->nc->id, nullptr, 0);
if (mgr && nc) mg_wakeup(this->mgr, this->nc->id, nullptr, 0);

// Stop Mongoose event loop thread
if (mongoose_thread.joinable()) {
Expand Down
12 changes: 12 additions & 0 deletions wiliwili/source/utils/config_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ extern in_addr_t secondary_dns;
}
#endif

#ifdef _WIN32
#include <winsock2.h>
#endif

#ifndef PATH_MAX
#define PATH_MAX 256
#endif
Expand Down Expand Up @@ -717,6 +721,11 @@ void ProgramConfig::init() {
curl_global_init(CURL_GLOBAL_DEFAULT);
cpr::async::startup(THREAD_POOL_MIN_THREAD_NUM, THREAD_POOL_MAX_THREAD_NUM, std::chrono::milliseconds(5000));

#ifdef _WIN32
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != 0) brls::Logger::error("WSAStartup failed with error: {}", result);
#endif
#if defined(_MSC_VER)
#elif defined(__PSV__)
#elif defined(PS4)
Expand Down Expand Up @@ -843,6 +852,9 @@ void ProgramConfig::exit(char* argv[]) {
cpr::async::cleanup();
curl_global_cleanup();

#ifdef _WIN32
WSACleanup();
#endif
#ifdef IOS
#elif defined(PS4)
#elif __PSV__
Expand Down
14 changes: 7 additions & 7 deletions wiliwili/source/utils/crash_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ LONG WINAPI createMiniDump(_EXCEPTION_POINTERS* pep) {
HMODULE dbghelp = ::LoadLibraryA("dbghelp.dll");
if (!dbghelp) return EXCEPTION_CONTINUE_SEARCH;

auto fnMiniDumpWriteDump = (WINBOOL(WINAPI*)(HANDLE, DWORD, HANDLE, MINIDUMP_TYPE,
auto fnMiniDumpWriteDump = (BOOL(WINAPI*)(HANDLE, DWORD, HANDLE, MINIDUMP_TYPE,
CONST PMINIDUMP_EXCEPTION_INFORMATION, CONST PMINIDUMP_USER_STREAM_INFORMATION,
CONST PMINIDUMP_CALLBACK_INFORMATION))::GetProcAddress(dbghelp, "MiniDumpWriteDump");
auto fnSymInitialize = (WINBOOL(WINAPI*)(HANDLE, PCSTR, WINBOOL))::GetProcAddress(dbghelp, "SymInitialize");
auto fnSymCleanup = (WINBOOL(WINAPI*)(HANDLE))::GetProcAddress(dbghelp, "SymCleanup");
auto fnSymInitialize = (BOOL(WINAPI*)(HANDLE, PCSTR, BOOL))::GetProcAddress(dbghelp, "SymInitialize");
auto fnSymCleanup = (BOOL(WINAPI*)(HANDLE))::GetProcAddress(dbghelp, "SymCleanup");
auto fnSymSetOptions = (DWORD(WINAPI*)(DWORD))::GetProcAddress(dbghelp, "SymSetOptions");
auto fnStackWalk64 = (WINBOOL(WINAPI*)(DWORD, HANDLE, HANDLE, LPSTACKFRAME64, PVOID, PREAD_PROCESS_MEMORY_ROUTINE64,
auto fnStackWalk64 = (BOOL(WINAPI*)(DWORD, HANDLE, HANDLE, LPSTACKFRAME64, PVOID, PREAD_PROCESS_MEMORY_ROUTINE64,
PFUNCTION_TABLE_ACCESS_ROUTINE64, PGET_MODULE_BASE_ROUTINE64, PTRANSLATE_ADDRESS_ROUTINE64))::GetProcAddress(dbghelp, "StackWalk64");
auto fnSymGetSymFromAddr64 = (WINBOOL(WINAPI*)(HANDLE, DWORD64, PDWORD64, PIMAGEHLP_SYMBOL64))::GetProcAddress(dbghelp, "SymGetSymFromAddr64");
auto fnSymGetLineFromAddr64 = (WINBOOL(WINAPI*)(HANDLE, DWORD64, PDWORD, PIMAGEHLP_LINE64))::GetProcAddress(dbghelp, "SymGetLineFromAddr64");
auto fnSymGetModuleInfo64 = (WINBOOL(WINAPI*)(HANDLE, DWORD64, PIMAGEHLP_MODULE64))::GetProcAddress(dbghelp, "SymGetModuleInfo64");
auto fnSymGetSymFromAddr64 = (BOOL(WINAPI*)(HANDLE, DWORD64, PDWORD64, PIMAGEHLP_SYMBOL64))::GetProcAddress(dbghelp, "SymGetSymFromAddr64");
auto fnSymGetLineFromAddr64 = (BOOL(WINAPI*)(HANDLE, DWORD64, PDWORD, PIMAGEHLP_LINE64))::GetProcAddress(dbghelp, "SymGetLineFromAddr64");
auto fnSymGetModuleInfo64 = (BOOL(WINAPI*)(HANDLE, DWORD64, PIMAGEHLP_MODULE64))::GetProcAddress(dbghelp, "SymGetModuleInfo64");
auto fnSymFTA64 = (PFUNCTION_TABLE_ACCESS_ROUTINE64)::GetProcAddress(dbghelp, "SymFunctionTableAccess64");
auto fnSymGMB64 = (PGET_MODULE_BASE_ROUTINE64)::GetProcAddress(dbghelp, "SymGetModuleBase64");

Expand Down
6 changes: 3 additions & 3 deletions xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ package("borealis")
add_configs("winrt", {description = "use winrt api", default = false, type = "boolean"})
add_deps(
"nanovg",
"yoga",
"yoga =2.0.1",
"nlohmann_json",
"fmt",
"tweeny",
Expand All @@ -53,7 +53,7 @@ package("borealis")
add_includedirs("include")
if is_plat("windows") then
add_includedirs("include/compat")
add_syslinks("Wlanapi", "iphlpapi", "Ws2_32")
add_syslinks("wlanapi", "iphlpapi", "ws2_32")
end
on_load(function (package)
local window = package:config("window")
Expand All @@ -65,7 +65,7 @@ package("borealis")
package:add("deps", "sdl2")
end
if driver == "opengl" then
package:add("deps", "glad")
package:add("deps", "glad =0.1.36")
elseif driver == "d3d11" then
package:add("syslinks", "d3d11")
end
Expand Down

0 comments on commit a319bbe

Please sign in to comment.