libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
9.52k stars 1.77k forks source link

SDL_CreateWindowFrom handle leak #9927

Open Beyondyxc opened 4 months ago

Beyondyxc commented 4 months ago

When I use sdlwindow=SDLCreateWindowFrom ((void *) wnd); When creating an SDL window, it was found that the handle resource was not released and kept growing. Wnd is a window handle created by my external QT, The type of wnd is HWND. I called SDL_DestroyWindow (sdLwindow) and SDL_Quit(), but none of them worked. My SDL version is 2.28.0.

Beyondyxc commented 4 months ago

sdl source code

#include "widget_sdl.h"
#include <iostream>

const int FPSCOUNTTIMEMS = 60000;
const int FPSCOUNTTIME = 60;

#define    SDL_USEREVENT_RENDER_IMAGE    0x1001
#define    SDL_USEREVENT_IMAGE_SIZE_CHANGE    0x1002

#ifdef _WIN32
#ifndef bzero
#define    bzero(m,n)        ZeroMemory(m, n)    /**< Mapping bzero() to ZeroMemory() */
#endif
#endif
const int WIDTH_1920 = 1920;
const int HEIGHT_1080 = 1080;
static int player_count_ = 1;
WidgetSDL::WidgetSDL()
{
#ifdef _WIN32
    win_width_ = GetSystemMetrics(SM_CXSCREEN);
    win_height_ = GetSystemMetrics(SM_CYSCREEN);
#else
    win_width_ = WIDTH_1920;
    win_height_ = HEIGHT_1080;
#endif
    player_count_++;
    player_index_ = player_count_;
    SDLRelatedInit();
}

WidgetSDL::~WidgetSDL() {
    OutputDebugString("\n");
    OutputDebugString("Free WidgetSDL\n");
    FreeSDLRes();
    SDLRelatedUninit();
}

bool WidgetSDL::Play() {
    OutputDebugString("\n");
    OutputDebugString("WidgetSDL::Play\n");
    if (sdl_window_ == nullptr) {
        CreateSDLRes();
    }
    return true;
}

void WidgetSDL::Stop() {
    OutputDebugString("\n");
    OutputDebugString("WidgetSDL::Stop\n");
    FreeSDLRes();
}

void WidgetSDL::SetWnd(HWND wnd) {
    OutputDebugString("\n");
    OutputDebugString("WidgetSDL::SetWnd\n");
    wnd_ = wnd;
}

void WidgetSDL::SetSize(int width, int height) {
    win_width_ = width;
    win_height_ = height;
}

void WidgetSDL::SDLRelatedInit() {
    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        OutputDebugString("\n");
        OutputDebugString("WidgetSDL::SDLRelatedInit failed\n");
    }
    else {
        OutputDebugString("\n");
        OutputDebugString("WidgetSDL::SDLRelatedInit success\n");
    }

}

void WidgetSDL::SDLRelatedUninit() {
    OutputDebugString("\n");
    OutputDebugString("WidgetSDL::SDL_Quit start\n");
    SDL_Quit();
    OutputDebugString("\n");
    OutputDebugString("WidgetSDL::SDL_Quit end\n");
}

void WidgetSDL::CreateSDLRes() {
    OutputDebugString("\n");
    OutputDebugString("WidgetSDL::CreateSDLRes\n");
    FreeSDLRes();

    if (!wnd_) {
        sdl_window_ = SDL_CreateWindow(u8"webrtc_player_window",  // 窗口标题
            SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,       // 窗口位置
            win_width_, win_height_,                              // 窗口宽高
            SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALLOW_HIGHDPI             // 窗口属性,指定使用OpenGL
        );
    }
    else {
         sdl_window_ = SDL_CreateWindowFrom((void*)wnd_);
    }
    OutputDebugString("\n");
    OutputDebugString("WidgetSDL::CreateSDLRes done\n");
}

void WidgetSDL::FreeSDLRes() {
    FreeSDLTexture();
    FreeSDLRender();
    if (sdl_window_) {
        SDL_DestroyWindow(sdl_window_);
        sdl_window_ = nullptr;
    }
}

void WidgetSDL::CreateSDLTexture() {
    if (!sdl_texture_) {
        if (sdl_renderer_) {
            sdl_texture_ = SDL_CreateTexture(sdl_renderer_, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING,
                WIDTH_1920, HEIGHT_1080);
        }
        else {

            return;
        }
    }
    else {

    }
}

void WidgetSDL::FreeSDLTexture() {

    if (sdl_texture_) {
        SDL_DestroyTexture(sdl_texture_);
        sdl_texture_ = nullptr;
    }
    else {

        return;
    }

}

void WidgetSDL::CreateSDLRender() {
    if (!sdl_renderer_){
        unsigned int renderer_flags = SDL_RENDERER_ACCELERATED;
        sdl_renderer_ = SDL_CreateRenderer(sdl_window_, -1, renderer_flags);
        SDL_GetWindowSize(sdl_window_, &win_width_, &win_height_);

        SDL_RendererInfo rendererInfo;
        SDL_GetRendererInfo(sdl_renderer_, &rendererInfo);
    }

}
void WidgetSDL::FreeSDLRender() {
    if (sdl_renderer_) {
        SDL_DestroyRenderer(sdl_renderer_);
        sdl_renderer_ = nullptr;
    }
    else {

    }
}
Beyondyxc commented 4 months ago

QT widget create sdl player source code

temp_player = std::make_unique<PlayerWidget>(this);
    if (temp_player) {
        sdl_player = std::make_unique<WidgetSDL>();
        sdl_player->SetWnd(temp_player->Wnd());
        sdl_player->Play();
        qDebug() << "sdl play create";
    }
Beyondyxc commented 4 months ago

QT widget free sdl player source code:

if (sdl_player) {
        sdl_player->Stop();
        sdl_player.reset();
        qDebug() << "sdl play free";
    }
    if (temp_player) {
        temp_player.reset();
    }
Beyondyxc commented 4 months ago

When I create and release, The window handle of Windows Task Manager is constantly rising, and my platform is Win10, QT is 5.12.5, regardless of whether the external QT window is destroyed or not, creating SDL windows will cause handle leakage

slouken commented 4 months ago

Are you calling SDL_PollEvent() to drain the SDL event queue?

Beyondyxc commented 4 months ago

Are you calling SDL_PollEvent() to drain the SDL event queue?

Now I am only using an external QT window to create an SDL window, without involving rendering, and without creating a render and texture, there will be handle leaks. You can take a look at the code I posted

slouken commented 4 months ago

If you create an SDL window, you should process SDL events on the thread that created the window. Try that and see if that fixes the issue?

Beyondyxc commented 4 months ago

If you create an SDL window, you should process SDL events on the thread that created the window. Try that and see if that fixes the issue?

I tried using QT's timer to process SDL events during thread timing, but it still doesn't seem to work. Here is my event handling code. I feel like there may be a problem with my usage. Can you provide a standard example

void WidgetSDL::ProcessEvent() {
    OutputDebugString("\n");
    OutputDebugString("WidgetSDL::ProcessEvent\n");
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
        if (event.type == SDL_QUIT) {

        }
    }
}
slouken commented 4 months ago

This should process events correctly. I'm not sure what's causing the handles to be created.