Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDL_CreateWindowFrom handle leak #9927

Open
Beyondyxc opened this issue May 30, 2024 · 9 comments
Open

SDL_CreateWindowFrom handle leak #9927

Beyondyxc opened this issue May 30, 2024 · 9 comments

Comments

@Beyondyxc
Copy link

When I use sdl_window_=SDL_CreateWindowFrom ((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 (sdL_window_) and SDL_Quit(), but none of them worked. My SDL version is 2.28.0.

@Beyondyxc
Copy link
Author

Beyondyxc commented May 30, 2024

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
Copy link
Author

Beyondyxc commented May 30, 2024

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
Copy link
Author

Beyondyxc commented May 30, 2024

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
Copy link
Author

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
Copy link
Collaborator

slouken commented May 30, 2024

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

@Beyondyxc
Copy link
Author

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
Copy link
Collaborator

slouken commented May 30, 2024

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
Copy link
Author

Beyondyxc commented May 31, 2024

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
Copy link
Collaborator

slouken commented May 31, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants