SDL3 C#: SDL_DestroyWindow() gives ‘Invalid window’ error despite valid window creation

I am writing a program to create an SDL3 window in C#.

I have compiled SDL3 2.30.5 code from the following repository: https://github.com/libsdl-org/SDL under Win x64 using Rider 2024.1.2.

Here is the code I’m trying to run:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using SDL3;
namespace SDL3Test
{
class Program
{
private static bool _loop;
static void Main(string[] args)
{
if (SDL.Init(SDL.InitFlags.Video) < 0)
{
Console.WriteLine($"SDL could not initialize! SDL_Error: {SDL.GetError()}");
return;
}
var window = SDL.CreateWindow("SDL Create Window", 800, 600, SDL.WindowFlags.OpenGL);
if (window == IntPtr.Zero)
{
Console.WriteLine($"Window could not be created! SDL_Error: {SDL.GetError()}");
return;
}
_loop = true;
while (_loop)
{
while (SDL.PollEvent(out var sdlEvent) != 0)
{
if (sdlEvent == SDL.EventType.Quit)
{
_loop = false;
}
}
}
SDL.DestroyWindow(window);
Console.WriteLine($"SDL_Error: {SDL.GetError()}");
SDL.Quit();
}
}
}
</code>
<code>using SDL3; namespace SDL3Test { class Program { private static bool _loop; static void Main(string[] args) { if (SDL.Init(SDL.InitFlags.Video) < 0) { Console.WriteLine($"SDL could not initialize! SDL_Error: {SDL.GetError()}"); return; } var window = SDL.CreateWindow("SDL Create Window", 800, 600, SDL.WindowFlags.OpenGL); if (window == IntPtr.Zero) { Console.WriteLine($"Window could not be created! SDL_Error: {SDL.GetError()}"); return; } _loop = true; while (_loop) { while (SDL.PollEvent(out var sdlEvent) != 0) { if (sdlEvent == SDL.EventType.Quit) { _loop = false; } } } SDL.DestroyWindow(window); Console.WriteLine($"SDL_Error: {SDL.GetError()}"); SDL.Quit(); } } } </code>
using SDL3;

namespace SDL3Test
{
    class Program
    {
        private static bool _loop;

        static void Main(string[] args)
        {
            if (SDL.Init(SDL.InitFlags.Video) < 0)
            {
                Console.WriteLine($"SDL could not initialize! SDL_Error: {SDL.GetError()}");
                return;
            }

            var window = SDL.CreateWindow("SDL Create Window", 800, 600, SDL.WindowFlags.OpenGL);

            if (window == IntPtr.Zero)
            {
                Console.WriteLine($"Window could not be created! SDL_Error: {SDL.GetError()}");
                return;
            }

            _loop = true;

            while (_loop)
            {
                while (SDL.PollEvent(out var sdlEvent) != 0)
                {
                    if (sdlEvent == SDL.EventType.Quit)
                    {
                        _loop = false;
                    }
                }
            }

            SDL.DestroyWindow(window);

            Console.WriteLine($"SDL_Error: {SDL.GetError()}");

            SDL.Quit();
        }
    }
}

Here is the CreateWindow function:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[LibraryImport(SDLLibrary)]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
private static unsafe partial IntPtr SDL_CreateWindow(byte* title, int w, int h, WindowFlags flags);
public static unsafe IntPtr CreateWindow(string? title, int w, int h, WindowFlags flags)
{
var utf8TitleBufSize = Utf8Size(title);
var utf8Title = stackalloc byte[utf8TitleBufSize];
return SDL_CreateWindow(Utf8Encode(title, utf8Title, utf8TitleBufSize), w, h, flags);
}
</code>
<code>[LibraryImport(SDLLibrary)] [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] private static unsafe partial IntPtr SDL_CreateWindow(byte* title, int w, int h, WindowFlags flags); public static unsafe IntPtr CreateWindow(string? title, int w, int h, WindowFlags flags) { var utf8TitleBufSize = Utf8Size(title); var utf8Title = stackalloc byte[utf8TitleBufSize]; return SDL_CreateWindow(Utf8Encode(title, utf8Title, utf8TitleBufSize), w, h, flags); } </code>
[LibraryImport(SDLLibrary)]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
private static unsafe partial IntPtr SDL_CreateWindow(byte* title, int w, int h, WindowFlags flags);

public static unsafe IntPtr CreateWindow(string? title, int w, int h, WindowFlags flags)
{
    var utf8TitleBufSize = Utf8Size(title);
    var utf8Title = stackalloc byte[utf8TitleBufSize];
    return SDL_CreateWindow(Utf8Encode(title, utf8Title, utf8TitleBufSize), w, h, flags);
}

And here’s the DestroyWindow function:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[LibraryImport(SDLLibrary, EntryPoint = "SDL_DestroyWindow")]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
public static unsafe partial void DestroyWindow(IntPtr window);
</code>
<code>[LibraryImport(SDLLibrary, EntryPoint = "SDL_DestroyWindow")] [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] public static unsafe partial void DestroyWindow(IntPtr window); </code>
[LibraryImport(SDLLibrary, EntryPoint = "SDL_DestroyWindow")]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
public static unsafe partial void DestroyWindow(IntPtr window);

Helper functions:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>private static int Utf8Size(string? str)
{
if (str == null)
{
return 0;
}
return str.Length * 4 + 1;
}
private static unsafe byte* Utf8Encode(string? str, byte* buffer, int bufferSize)
{
if (str == null)
{
return (byte*)0;
}
fixed (char* strPtr = str)
{
Encoding.UTF8.GetBytes(strPtr, str.Length + 1, buffer, bufferSize);
}
return buffer;
}
private static unsafe string? UTF8_ToManaged(IntPtr s, bool freePtr = false)
{
if (s == IntPtr.Zero) return null;
var ptr = (byte*)s;
while (*ptr != 0) ptr++;
var result = Encoding.UTF8.GetString((byte*)s, (int)(ptr - (byte*)s));
if (freePtr) Free(s);
return result;
}
</code>
<code>private static int Utf8Size(string? str) { if (str == null) { return 0; } return str.Length * 4 + 1; } private static unsafe byte* Utf8Encode(string? str, byte* buffer, int bufferSize) { if (str == null) { return (byte*)0; } fixed (char* strPtr = str) { Encoding.UTF8.GetBytes(strPtr, str.Length + 1, buffer, bufferSize); } return buffer; } private static unsafe string? UTF8_ToManaged(IntPtr s, bool freePtr = false) { if (s == IntPtr.Zero) return null; var ptr = (byte*)s; while (*ptr != 0) ptr++; var result = Encoding.UTF8.GetString((byte*)s, (int)(ptr - (byte*)s)); if (freePtr) Free(s); return result; } </code>
private static int Utf8Size(string? str)
{
    if (str == null)
    {
        return 0;
    }
    return str.Length * 4 + 1;
}

private static unsafe byte* Utf8Encode(string? str, byte* buffer, int bufferSize)
{
    if (str == null)
    {
        return (byte*)0;
    }
    fixed (char* strPtr = str)
    {
        Encoding.UTF8.GetBytes(strPtr, str.Length + 1, buffer, bufferSize);
    }
    return buffer;
}

private static unsafe string? UTF8_ToManaged(IntPtr s, bool freePtr = false)
{
    if (s == IntPtr.Zero) return null;

    var ptr = (byte*)s;
    while (*ptr != 0) ptr++;

    var result = Encoding.UTF8.GetString((byte*)s, (int)(ptr - (byte*)s));

    if (freePtr) Free(s);
    return result;
}

The problem is that when I call SDL.DestroyWindow(window);, I get the error SDL_Error: Invalid window through GetError.

What could be causing this issue?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật