-
Notifications
You must be signed in to change notification settings - Fork 0
Refactorings/decouple backend #26
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -74,10 +74,11 @@ | |||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| void showDriverInUse(const Context &ctx) { | ||||||||||||||||||||
| void showDriverInUse(Context &ctx) { | ||||||||||||||||||||
| ctx.mLogger(LogSeverity::Message, "Driver in use:"); | ||||||||||||||||||||
| SDLContext *sdlCtx = getBackendContext(ctx); | ||||||||||||||||||||
| SDL_RendererInfo info; | ||||||||||||||||||||
| SDL_GetRendererInfo(ctx.mSDLContext->mRenderer, &info); | ||||||||||||||||||||
| SDL_GetRendererInfo(sdlCtx->mRenderer, &info); | ||||||||||||||||||||
| printDriverInfo(info); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -178,15 +179,17 @@ | |||||||||||||||||||
| delete ctx.mDefaultFont; | ||||||||||||||||||||
| ctx.mDefaultFont = nullptr; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| IMG_Quit(); | ||||||||||||||||||||
| if (ctx.mSDLContext == nullptr) { | ||||||||||||||||||||
| if (ctx.mBackendCtx == nullptr) { | ||||||||||||||||||||
| SDL_Quit(); | ||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if (ctx.mSDLContext->mRenderer != nullptr) { | ||||||||||||||||||||
| SDL_DestroyRenderer(ctx.mSDLContext->mRenderer); | ||||||||||||||||||||
| ctx.mSDLContext->mRenderer = nullptr; | ||||||||||||||||||||
| SDLContext *sdlCtx = getBackendContext(ctx); | ||||||||||||||||||||
| if (sdlCtx->mRenderer != nullptr) { | ||||||||||||||||||||
|
Check warning on line 190 in src/backends/sdl2_renderer.cpp
|
||||||||||||||||||||
| SDL_DestroyRenderer(sdlCtx->mRenderer); | ||||||||||||||||||||
| sdlCtx->mRenderer = nullptr; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| SDL_Quit(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -220,15 +223,16 @@ | |||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| SDL_Texture *messageTexture = SDL_CreateTextureFromSurface(ctx.mSDLContext->mRenderer, surfaceMessage); | ||||||||||||||||||||
| SDLContext *sdlCtx = getBackendContext(ctx); | ||||||||||||||||||||
| SDL_Texture *messageTexture = SDL_CreateTextureFromSurface(sdlCtx->mRenderer, surfaceMessage); | ||||||||||||||||||||
| if (messageTexture == nullptr) { | ||||||||||||||||||||
| const std::string msg = "Cannot create texture: " + std::string(SDL_GetError()) + "."; | ||||||||||||||||||||
| ctx.mLogger(LogSeverity::Error, msg.c_str()); | ||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| int32_t margin = ctx.mStyle.mMargin; | ||||||||||||||||||||
| SDL_Rect Message_rect = {}; | ||||||||||||||||||||
| int32_t margin{ctx.mStyle.mMargin}; | ||||||||||||||||||||
| SDL_Rect Message_rect{}; | ||||||||||||||||||||
| switch (alignment) { | ||||||||||||||||||||
| case Alignment::Left: | ||||||||||||||||||||
| Message_rect.x = r.top.x + margin; | ||||||||||||||||||||
|
|
@@ -239,18 +243,18 @@ | |||||||||||||||||||
| case Alignment::Center: | ||||||||||||||||||||
| Message_rect.x = r.top.x + 2 * margin + surfaceMessage->clip_rect.w / 2; | ||||||||||||||||||||
| Message_rect.y = r.top.y + margin; | ||||||||||||||||||||
| Message_rect.w = font->mSize; | ||||||||||||||||||||
| Message_rect.h = font->mSize; | ||||||||||||||||||||
| Message_rect.w = font->mSize * static_cast<int>(strlen(string)); | ||||||||||||||||||||
| Message_rect.h = font->mSize + margin * 2; | ||||||||||||||||||||
| break; | ||||||||||||||||||||
| case Alignment::Right: | ||||||||||||||||||||
| Message_rect.x = r.top.x + surfaceMessage->clip_rect.w - font->mSize * static_cast<int>(strlen(string)); | ||||||||||||||||||||
| Message_rect.y = r.top.y + margin; | ||||||||||||||||||||
| Message_rect.w = font->mSize; | ||||||||||||||||||||
| Message_rect.h = font->mSize; | ||||||||||||||||||||
| Message_rect.w = font->mSize * static_cast<int>(strlen(string)); | ||||||||||||||||||||
| Message_rect.h = font->mSize + margin * 2; | ||||||||||||||||||||
| break; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| SDL_RenderCopy(ctx.mSDLContext->mRenderer, messageTexture, nullptr, &Message_rect); | ||||||||||||||||||||
| SDL_RenderCopy(sdlCtx->mRenderer, messageTexture, nullptr, &Message_rect); | ||||||||||||||||||||
| SDL_FreeSurface(surfaceMessage); | ||||||||||||||||||||
| SDL_DestroyTexture(messageTexture); | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -259,15 +263,18 @@ | |||||||||||||||||||
|
|
||||||||||||||||||||
| ret_code Renderer::initScreen(Context &ctx, int32_t x, int32_t y, int32_t w, int32_t h) { | ||||||||||||||||||||
| if (!ctx.mCreated) { | ||||||||||||||||||||
| ctx.mLogger(LogSeverity::Error, "Not initialzed."); | ||||||||||||||||||||
| ctx.mLogger(LogSeverity::Error, "Not initialized."); | ||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| ctx.mSDLContext = SDLContext::create(); | ||||||||||||||||||||
| if (ctx.mSDLContext->mWindow != nullptr) { | ||||||||||||||||||||
| SDLContext *sdlCtx = SDLContext::create(); | ||||||||||||||||||||
| if (sdlCtx->mWindow != nullptr) { | ||||||||||||||||||||
| ctx.mLogger(LogSeverity::Error, "Already created."); | ||||||||||||||||||||
|
Check failure on line 271 in src/backends/sdl2_renderer.cpp
|
||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ctx.mBackendCtx = new BackendContext; | ||||||||||||||||||||
|
Check failure on line 275 in src/backends/sdl2_renderer.cpp
|
||||||||||||||||||||
| ctx.mBackendCtx->mHandle = (void*) sdlCtx; | ||||||||||||||||||||
|
Comment on lines
+275
to
+276
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # First, check if the file exists and get its size
wc -l src/backends/sdl2_renderer.cppRepository: kimkulling/tiny_ui Length of output: 96 🏁 Script executed: # Read the context around lines 275-330 to see all early returns
sed -n '270,330p' src/backends/sdl2_renderer.cpp | cat -nRepository: kimkulling/tiny_ui Length of output: 2490 🏁 Script executed: # Look for the function signature and overall structure
grep -n "BackendContext\|SDLContext\|SDL2Renderer\|init\|Create" src/backends/sdl2_renderer.cpp | head -30Repository: kimkulling/tiny_ui Length of output: 1877 🏁 Script executed: # Get the complete initScreen function starting from line 264
sed -n '264,327p' src/backends/sdl2_renderer.cpp | cat -nRepository: kimkulling/tiny_ui Length of output: 2591 🏁 Script executed: # Search for SDLContext definition and destroy method
grep -n "struct SDLContext\|class SDLContext\|SDLContext::create\|SDLContext::destroy\|mBackendCtx" src/backends/sdl2_renderer.cppRepository: kimkulling/tiny_ui Length of output: 312 🏁 Script executed: # Find BackendContext definition
grep -rn "struct BackendContext\|class BackendContext" src/Repository: kimkulling/tiny_ui Length of output: 102 🏁 Script executed: # Check if there's a destructor or cleanup helper
grep -n "~SDLContext\|~BackendContext\|destroy\|cleanup" src/backends/sdl2_renderer.cppRepository: kimkulling/tiny_ui Length of output: 44 🏁 Script executed: # Check BackendContext definition in tinyui.h
sed -n '384,400p' src/tinyui.h | cat -nRepository: kimkulling/tiny_ui Length of output: 1166 🏁 Script executed: # Search for SDLContext definition
find src -type f \( -name "*.h" -o -name "*.hpp" -o -name "*.cpp" \) -exec grep -l "struct SDLContext\|class SDLContext" {} \;Repository: kimkulling/tiny_ui Length of output: 103 🏁 Script executed: # Search for SDLContext::create and destroy implementations
grep -rn "SDLContext::create\|SDLContext::destroy\|~SDLContext" src/Repository: kimkulling/tiny_ui Length of output: 143 🏁 Script executed: # Look for SDLContext definition
grep -B5 -A10 "struct SDLContext\|class SDLContext" src/backends/sdl2_renderer.cppRepository: kimkulling/tiny_ui Length of output: 44 🏁 Script executed: # Search for SDLContext in header files
cat src/backends/sdl2_renderer.hRepository: kimkulling/tiny_ui Length of output: 4264 🏁 Script executed: # Search more broadly for SDLContext
rg "SDLContext" src/ -A 5 -B 5 | head -100Repository: kimkulling/tiny_ui Length of output: 5407 🏁 Script executed: # Check the other initScreen overload (context version)
sed -n '328,365p' src/backends/sdl2_renderer.cpp | cat -nRepository: kimkulling/tiny_ui Length of output: 1306 🏁 Script executed: # Check releaseScreen to see cleanup patterns
grep -A 20 "releaseScreen" src/backends/sdl2_renderer.cpp | head -40Repository: kimkulling/tiny_ui Length of output: 672 🏁 Script executed: # Check if mOwner is set anywhere in the codebase
grep -n "mOwner" src/backends/sdl2_renderer.cppRepository: kimkulling/tiny_ui Length of output: 93 🏁 Script executed: # Verify mOwner usage in initScreen more carefully
sed -n '264,325p' src/backends/sdl2_renderer.cpp | grep -n "mOwner"Repository: kimkulling/tiny_ui Length of output: 44 🏁 Script executed: # Check how destroy() is actually used
grep -n "\.destroy()" src/backends/sdl2_renderer.cppRepository: kimkulling/tiny_ui Length of output: 44 Fix memory leaks on early returns in initScreen initialization. After line 275-276 allocate Implement a cleanup path that deletes Lines affected by this issue
🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| if (TTF_Init() == -1) { | ||||||||||||||||||||
| ctx.mLogger(LogSeverity::Error, "TTF init failed."); | ||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||
|
|
@@ -283,8 +290,8 @@ | |||||||||||||||||||
| title = "TinyUI Window"; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ctx.mSDLContext->mWindow = SDL_CreateWindow(title, x, y, w, h, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); | ||||||||||||||||||||
| if (ctx.mSDLContext->mWindow == nullptr) { | ||||||||||||||||||||
| sdlCtx->mWindow = SDL_CreateWindow(title, x, y, w, h, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); | ||||||||||||||||||||
| if (sdlCtx->mWindow == nullptr) { | ||||||||||||||||||||
| const std::string msg = "Error while SDL_CreateWindow: " + std::string(SDL_GetError()) + "."; | ||||||||||||||||||||
| ctx.mLogger(LogSeverity::Error, msg.c_str()); | ||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||
|
|
@@ -296,8 +303,8 @@ | |||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ctx.mSDLContext->mRenderer = SDL_CreateRenderer(ctx.mSDLContext->mWindow, driverIndex, SDL_RENDERER_ACCELERATED); | ||||||||||||||||||||
| if (nullptr == ctx.mSDLContext->mRenderer) { | ||||||||||||||||||||
| sdlCtx->mRenderer = SDL_CreateRenderer(sdlCtx->mWindow, driverIndex, SDL_RENDERER_ACCELERATED); | ||||||||||||||||||||
| if (nullptr == sdlCtx->mRenderer) { | ||||||||||||||||||||
| const std::string msg = "Error while SDL_CreateRenderer: " + std::string(SDL_GetError()) + "."; | ||||||||||||||||||||
| ctx.mLogger(LogSeverity::Error, msg.c_str()); | ||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||
|
|
@@ -309,8 +316,8 @@ | |||||||||||||||||||
|
|
||||||||||||||||||||
| showDriverInUse(ctx); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ctx.mSDLContext->mSurface = SDL_GetWindowSurface(ctx.mSDLContext->mWindow); | ||||||||||||||||||||
| if (ctx.mSDLContext->mSurface == nullptr) { | ||||||||||||||||||||
| sdlCtx->mSurface = SDL_GetWindowSurface(sdlCtx->mWindow); | ||||||||||||||||||||
| if (sdlCtx->mSurface == nullptr) { | ||||||||||||||||||||
| ctx.mLogger(LogSeverity::Error, "Surface pointer from window is nullptr."); | ||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
@@ -320,7 +327,7 @@ | |||||||||||||||||||
|
|
||||||||||||||||||||
| ret_code Renderer::initScreen(Context &ctx, SDL_Window *window, SDL_Renderer *renderer) { | ||||||||||||||||||||
| if (!ctx.mCreated) { | ||||||||||||||||||||
| ctx.mLogger(LogSeverity::Error, "Not initialzed."); | ||||||||||||||||||||
| ctx.mLogger(LogSeverity::Error, "TinyUI context is not initialzed."); | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: "initialzed" should be "initialized". The error message contains a spelling error. ✏️ Proposed fix- ctx.mLogger(LogSeverity::Error, "TinyUI context is not initialzed.");
+ ctx.mLogger(LogSeverity::Error, "TinyUI context is not initialized.");Note: Line 361 has the same typo: 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -331,13 +338,19 @@ | |||||||||||||||||||
|
|
||||||||||||||||||||
| TTF_Init(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ctx.mSDLContext->mRenderer = renderer; | ||||||||||||||||||||
| ctx.mSDLContext->mWindow = window; | ||||||||||||||||||||
| SDLContext *sdlCtx = getBackendContext(ctx); | ||||||||||||||||||||
| if (sdlCtx == nullptr) { | ||||||||||||||||||||
| ctx.mLogger(LogSeverity::Error, "Invalid sdl context detected."); | ||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| sdlCtx->mRenderer = renderer; | ||||||||||||||||||||
| sdlCtx->mWindow = window; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| showDriverInUse(ctx); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ctx.mSDLContext->mSurface = SDL_GetWindowSurface(ctx.mSDLContext->mWindow); | ||||||||||||||||||||
| ctx.mSDLContext->mOwner = false; | ||||||||||||||||||||
| sdlCtx->mSurface = SDL_GetWindowSurface(sdlCtx->mWindow); | ||||||||||||||||||||
| sdlCtx->mOwner = false; | ||||||||||||||||||||
|
kimkulling marked this conversation as resolved.
|
||||||||||||||||||||
| ctx.mCreated = true; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||
|
|
@@ -349,8 +362,9 @@ | |||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| SDL_DestroyWindow(ctx.mSDLContext->mWindow); | ||||||||||||||||||||
| ctx.mSDLContext->mWindow = nullptr; | ||||||||||||||||||||
| SDLContext *sdlCtx = getBackendContext(ctx); | ||||||||||||||||||||
| SDL_DestroyWindow(sdlCtx->mWindow); | ||||||||||||||||||||
| sdlCtx->mWindow = nullptr; | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
Comment on lines
+365
to
368
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing null check on If 🛡️ Proposed fix SDLContext *sdlCtx = getBackendContext(ctx);
+ if (sdlCtx == nullptr) {
+ return ErrorCode;
+ }
SDL_DestroyWindow(sdlCtx->mWindow);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
@@ -362,19 +376,21 @@ | |||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const SDL_Color sdl_bg = getSDLColor(bg); | ||||||||||||||||||||
| SDL_SetRenderDrawColor(ctx.mSDLContext->mRenderer, sdl_bg.r, sdl_bg.g, sdl_bg.b, sdl_bg.a); | ||||||||||||||||||||
| SDL_RenderClear(ctx.mSDLContext->mRenderer); | ||||||||||||||||||||
| SDLContext *sdlCtx = getBackendContext(ctx); | ||||||||||||||||||||
| SDL_SetRenderDrawColor(sdlCtx->mRenderer, sdl_bg.r, sdl_bg.g, sdl_bg.b, sdl_bg.a); | ||||||||||||||||||||
| SDL_RenderClear(sdlCtx->mRenderer); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ret_code Renderer::drawRect(Context &ctx, int32_t x, int32_t y, int32_t w, int32_t h, bool filled, Color4 fg) { | ||||||||||||||||||||
| SDL_Rect r = {x, y, w, h}; | ||||||||||||||||||||
| SDL_SetRenderDrawColor(ctx.mSDLContext->mRenderer, fg.r, fg.g, fg.b, fg.a); | ||||||||||||||||||||
| SDLContext *sdlCtx = getBackendContext(ctx); | ||||||||||||||||||||
| SDL_SetRenderDrawColor(sdlCtx->mRenderer, fg.r, fg.g, fg.b, fg.a); | ||||||||||||||||||||
| if (filled) { | ||||||||||||||||||||
| SDL_RenderFillRect(ctx.mSDLContext->mRenderer, &r); | ||||||||||||||||||||
| SDL_RenderFillRect(sdlCtx->mRenderer, &r); | ||||||||||||||||||||
| } else { | ||||||||||||||||||||
| SDL_RenderDrawRect(ctx.mSDLContext->mRenderer, &r); | ||||||||||||||||||||
| SDL_RenderDrawRect(sdlCtx->mRenderer, &r); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
386
to
394
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing initialization and null checks. Unlike other render functions, 🛡️ Proposed fix ret_code Renderer::drawRect(Context &ctx, int32_t x, int32_t y, int32_t w, int32_t h, bool filled, Color4 fg) {
+ if (!ctx.mCreated) {
+ return ErrorCode;
+ }
SDL_Rect r = {x, y, w, h};
SDLContext *sdlCtx = getBackendContext(ctx);
+ if (sdlCtx == nullptr) {
+ return ErrorCode;
+ }
SDL_SetRenderDrawColor(sdlCtx->mRenderer, fg.r, fg.g, fg.b, fg.a);🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||
|
|
@@ -386,28 +402,31 @@ | |||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| SDL_Rect imageRect = {x, y, w, h}; | ||||||||||||||||||||
| SDL_Texture *tex = SDL_CreateTextureFromSurface(ctx.mSDLContext->mRenderer, image->mSurfaceImpl->mSurface); | ||||||||||||||||||||
| SDL_RenderCopy(ctx.mSDLContext->mRenderer, tex, nullptr, &imageRect); | ||||||||||||||||||||
| SDLContext *sdlCtx = getBackendContext(ctx); | ||||||||||||||||||||
| SDL_Texture *tex = SDL_CreateTextureFromSurface(sdlCtx->mRenderer, image->mSurfaceImpl->mSurface); | ||||||||||||||||||||
| SDL_RenderCopy(sdlCtx->mRenderer, tex, nullptr, &imageRect); | ||||||||||||||||||||
| SDL_DestroyTexture(tex); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ret_code Renderer::closeScreen(Context &ctx) { | ||||||||||||||||||||
| if (ctx.mSDLContext->mWindow == nullptr) { | ||||||||||||||||||||
| SDLContext *sdlCtx = getBackendContext(ctx); | ||||||||||||||||||||
| if (sdlCtx->mWindow == nullptr) { | ||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| TTF_Quit(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| SDL_DestroyWindow(ctx.mSDLContext->mWindow); | ||||||||||||||||||||
| ctx.mSDLContext->mWindow = nullptr; | ||||||||||||||||||||
| SDL_DestroyWindow(sdlCtx->mWindow); | ||||||||||||||||||||
| sdlCtx->mWindow = nullptr; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
413
to
425
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resource leak: The function destroys
As noted in the context snippets, 🛡️ Proposed fix: Complete cleanup ret_code Renderer::closeScreen(Context &ctx) {
SDLContext *sdlCtx = getBackendContext(ctx);
+ if (sdlCtx == nullptr) {
+ return ErrorCode;
+ }
if (sdlCtx->mWindow == nullptr) {
return ErrorCode;
}
TTF_Quit();
- SDL_DestroyWindow(sdlCtx->mWindow);
- sdlCtx->mWindow = nullptr;
+ sdlCtx->mOwner = true; // Take ownership for cleanup
+ sdlCtx->destroy(); // Destroys renderer, window, and frees SDLContext
+
+ delete ctx.mBackendCtx;
+ ctx.mBackendCtx = nullptr;
return ResultOk;
}🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| ret_code Renderer::endRender(Context &ctx) { | ||||||||||||||||||||
| SDL_RenderPresent(ctx.mSDLContext->mRenderer); | ||||||||||||||||||||
| SDLContext *sdlCtx = getBackendContext(ctx); | ||||||||||||||||||||
| SDL_RenderPresent(sdlCtx->mRenderer); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
@@ -416,7 +435,8 @@ | |||||||||||||||||||
| if (texture == nullptr) { | ||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| *texture = SDL_CreateTexture(ctx.mSDLContext->mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h); | ||||||||||||||||||||
| SDLContext *sdlCtx = getBackendContext(ctx); | ||||||||||||||||||||
| *texture = SDL_CreateTexture(sdlCtx->mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
@@ -499,4 +519,16 @@ | |||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ret_code Renderer::getSurfaceInfo(Context &ctx, int32_t &w, int32_t &h) { | ||||||||||||||||||||
| SDLContext *sdlCtx = (SDLContext *) ctx.mBackendCtx; | ||||||||||||||||||||
|
Check warning on line 523 in src/backends/sdl2_renderer.cpp
|
||||||||||||||||||||
| if (sdlCtx->mSurface == nullptr) { | ||||||||||||||||||||
| return ErrorCode; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| w = sdlCtx->mSurface->w; | ||||||||||||||||||||
| h = sdlCtx->mSurface->h; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return ResultOk; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+522
to
+532
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical bug: Incorrect cast in Line 517 casts This will cause undefined behavior when accessing 🐛 Proposed fix ret_code Renderer::getSurfaceInfo(Context &ctx, int32_t &w, int32_t &h) {
- SDLContext *sdlCtx = (SDLContext *) ctx.mBackendCtx;
+ if (ctx.mBackendCtx == nullptr) {
+ return ErrorCode;
+ }
+ const auto *sdlCtx = static_cast<const SDLContext*>(ctx.mBackendCtx->mHandle);
if (sdlCtx->mSurface == nullptr) {
return ErrorCode;
}🧰 Tools🪛 GitHub Check: SonarCloud Code Analysis[warning] 517-517: Replace the redundant type with "auto". [warning] 517-517: Make the type of this variable a pointer-to-const. The current type of "sdlCtx" is "struct tinyui::SDLContext *". 🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| } // namespace tinyui | ||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,6 +100,10 @@ | |
| } | ||
| }; | ||
|
|
||
| inline SDLContext *getBackendContext(Context &ctx) { | ||
| SDLContext *sdlCtx = (SDLContext*) ctx.mBackendCtx->mHandle; | ||
|
Check warning on line 104 in src/backends/sdl2_renderer.h
|
||
| return sdlCtx; | ||
| } | ||
|
Comment on lines
+103
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This helper dereferences Additionally, prefer 🛡️ Proposed fix with null checks and proper cast inline SDLContext *getBackendContext(Context &ctx) {
- SDLContext *sdlCtx = (SDLContext*) ctx.mBackendCtx->mHandle;
- return sdlCtx;
+ if (ctx.mBackendCtx == nullptr) {
+ return nullptr;
+ }
+ return static_cast<SDLContext*>(ctx.mBackendCtx->mHandle);
}Note: All callers of 🧰 Tools🪛 GitHub Check: SonarCloud Code Analysis[warning] 104-104: Replace the redundant type with "auto". 🤖 Prompt for AI Agents |
||
|
|
||
| /// @brief The renderer implementation using the SDL2 library. | ||
| struct Renderer { | ||
|
|
@@ -118,6 +122,7 @@ | |
| static bool update(Context &ctx); | ||
| static SurfaceImpl *createSurfaceImpl(unsigned char *data, int w, int h, int bytesPerPixel, int pitch); | ||
| static void releaseSurfaceImpl(SurfaceImpl *surfaceImpl); | ||
| static ret_code getSurfaceInfo(Context &ctx, int32_t &w, int32_t &h); | ||
| }; | ||
|
|
||
| } // namespace tinyui | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing null check on
sdlCtxbefore dereferencing.If
getBackendContext(ctx)returnsnullptr(e.g., backend not initialized), line 227 and 257 will dereference a null pointer.🛡️ Proposed fix
🤖 Prompt for AI Agents