libsdl-org / SDL

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

SDL_MapRGB and SDL_MapRGBA problem for external langauges #7662

Closed DeafMan1983 closed 1 year ago

DeafMan1983 commented 1 year ago

Hello everyone,

I have made test app ( test.cpp ) and it works fine for testing. But in C# crashes as SDL_MapRGB_REAL or SDL_MapRGBA_REAL shows in gdb debbugging crash message.

#include <SDL3/SDL.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window *window = SDL_CreateWindow("Hello Window!", 600, 400, 0);

    SDL_Surface *surface = SDL_GetWindowSurface(window);

    SDL_Event event;
    bool isRunning = true;
    while (isRunning)
    {
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_EVENT_QUIT)
            {
                SDL_Quit();
                return 1;
            }
        }

        SDL_FillSurfaceRect(surface, nullptr, SDL_MapRGB(surface->format, 40, 40, 40));
        SDL_UpdateWindowSurface(window);
    }

    SDL_DestroySurface(surface);
    SDL_DestroyWindow(window);
    return 0;
}

And I use g++ -o test_app test.cpp -lSDL3 and run with ./test_app The result looks great. image

And I try with C# in DeafMan1983.Interop.SDL3 ( I forget to add defines... I will add soon... ) But I am working hard with SDL3 in C# I have checked SDL_MapRGB() and SDL_MapRGBA() with valid p/invoke via DllImport() I have verified with ClangSharpPInvokeGenerator. And they are valid. I have checked with objdump and objdump shows valid public methods but why do they need to use SDL_MapRGB_REAL or SDL_MapRGBA_REAL image I don't understand why does SDL shared has 2 REAL methods from SDL_MapRGB() or SDL_MapRGBA()

I try out in C#

namespace Tutorial;

using System;

using static DeafMan1983.ConvFunctions;

using DeafMan1983.Interop.SDL3;
using static DeafMan1983.Interop.SDL3.SDL3;

unsafe class SDL3Program
{
    static void Main(string[] args)
    {
        if (SDL_Init(SDL_INIT_VIDEO))
        {
            Console.WriteLine("Error!");
            Environment.Exit(1);
        }

        SDL_Window *window = SDL_CreateWindow(StringFromHeap("Hello Window!"), 600, 400, 0);
        if (window == null)
        {
            Console.WriteLine("Error!");
            Environment.Exit(1);
        }

        SDL_Surface *surface = SDL_GetWindowSurface(window);

        SDL_Event evt;
        bool isRunning = true;
        while (isRunning)
        {
            while (SDL_PollEvent(&evt))
            {
                if (evt.type == SDL_EVENT_QUIT)
                {
                    isRunning = false;
                }

                if (evt.window.type == SDL_EVENT_WINDOW_RESIZED)
                {
                    SDL_SetWindowSize(window, evt.window.data1, evt.window.data2);
                }
            }

            SDL_Rect rect = new()
            {
                w = 600, h = 400
            };
            SDL_FillSurfaceRect(surface, &rect, SDL_MapRGBA(surface->format, 40, 40, 40, 255));
            SDL_UpdateWindowSurface(window);
        }

        SDL_DestroySurface(surface);
        SDL_DestroyWindow(window);

        SDL_Quit();
    }
}

I prompt with dotnet: dotnet build -c Release and dotnet run -c Release And dotnet app runs out cause it has segment core dump I have verified gdb with dotnet app and It shows.

gdb ./01_HelloWindow
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./01_HelloWindow...
(No debugging symbols found in ./01_HelloWindow)
(gdb) 
(gdb) run
Starting program: /home/jens/Documents/SDL3Examples/01_HelloWindow/bin/Release/net7.0/01_HelloWindow 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7fff7800f640 (LWP 219792)]
[New Thread 0x7fff7780e640 (LWP 219793)]
[New Thread 0x7fff7700d640 (LWP 219794)]
[New Thread 0x7fff7680c640 (LWP 219795)]
[New Thread 0x7fff7600b640 (LWP 219796)]
[New Thread 0x7fbecb4d6640 (LWP 219797)]
[New Thread 0x7fbebedff640 (LWP 219798)]
[New Thread 0x7fbebe5fe640 (LWP 219799)]
[New Thread 0x7fbebddfd640 (LWP 219800)]
[New Thread 0x7fbebd5fc640 (LWP 219801)]
[New Thread 0x7fbebcdfb640 (LWP 219802)]
[New Thread 0x7fbea7fff640 (LWP 219803)]
[Thread 0x7fbea7fff640 (LWP 219803) exited]
[Thread 0x7fbebcdfb640 (LWP 219802) exited]
[New Thread 0x7fbebcdfb640 (LWP 219804)]
[New Thread 0x7fbea7fff640 (LWP 219805)]

Thread 1 "01_HelloWindow" received signal SIGSEGV, Segmentation fault.
0x00007fff7453b651 in SDL_MapRGBA_REAL () from /lib/x86_64-linux-gnu/libSDL3.so
(gdb) quit
A debugging session is active.

    Inferior 1 [process 219789] will be killed.

Quit anyway? (y or n) c
Please answer y or n.
A debugging session is active.

    Inferior 1 [process 219789] will be killed.

Quit anyway? (y or n) y

Or I have tried without gdb: ./01_HelloWindow then it shows crash message. image

That is crash because segment fault has thrown.

Please fix both SDL_MapRGB() and SDL_MapRGBA()

madebr commented 1 year ago

Please provide a full backtrace, such that we can see the values of various symbols. Make sure to build with debug information, and install the debug symbols of at least SDL2. That way, gdb is able to show symbol and function names instead of hexadecimal values.

DeafMan1983 commented 1 year ago

I found bug cause SDL_Surface shouldn't get private void *value with operator implicit and explicit. It works fine now. It was gone crash dumped. I have checked SDL2-CS by @flibitijibibo = OK! Now I need update my DeafMan1983.Interop.SDL3

Thanks for helping me! I expect @flibitijibibo is great C# developer for SDL2 and I am also C# developer for SDL3 :)