SDL2 |包装器 class 访问冲突

SDL2 | Wrapper class access violation

我一直在围绕一些 SDL2 功能制作一些非常简单的包装器。我创建了一个包裹 SDL_Window* 的 class 和一个包裹 SDL_Surface*.

的 class

在我的 SDL_Surface 包装器 (SDL2::Surface) 中,构造函数接收 SDL2:::Window 包装器并关联 SDL_Window*(通过 getter 调用)到 SDL2::Surface.

中的 SDL_Surface* 成员变量

然后,我有 SDL2::Surface::FillRect() 调用:

SDL_FillRect(Surface, NULL, SDL_MapRGB(Surface->format, 0xFF, 0x00, 0xFF));

在此构造下,我在调用 SDL_FillRect 时遇到访问冲突异常。但是,如果我将 SDL_Window*SDL_Surface 包装到一个包装器 class 中,那么从 class 调用 SDL_FillRect 就可以正常工作。

这两种方法之间可能有什么区别?

// Surface.h
namespace SDL2 {
    class Window {
        private:
            SDL_Window* mWindow;
            int ScreenWidth = 640;
            int ScreenHeight = 480;

        public:
            Window();
            ~Window();
            SDL_Window* GetWindow();

    }
}

// Surface.cpp
SDL2::Window::Window() {
    mWindow = SDL_CreateWindow("Breakout", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, ScreenWidth, ScreenHeight, SDL_WINDOW_SHOWN);
}

SDL2::Window::~Window() {
    SDL_DestroyWindow(mWindow);
{        

// Surface.h
namespace SDL2 {
    class Surface {
        private:
            SDL_Surface* Surface;

        public:
            Surface(SDL2::Window window);
            ~Surface();
            void FillRect();
            void Update(SDL2::Window window);
    }
}

// Wrapper.cpp
SDL2::Surface::Surface(SDL2::Window window) {
    Surface = SDL_GetWindowSurface(window.GetWindow());
    }

void SDL2::Surface::FillRect() {
        SDL_FillRect(Surface, NULL, SDL_MapRGB(Surface->format, 0xFF, 0x00, 0xFF)); // Access violation when this is called
        }

void SDL2::Surface::Update(SDL2::Window window) {
    SDL_UpdateWindowSurface(window.GetWindow());
}

// main.cpp
#include <SDL.h>
#include <stdio.h>
#include "Wrapper.h"
#include "Window.h"

int main() {
    //Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        return 1;
    }

    SDL2::Window window = SDL2::Window();

    SDL2::Surface surface = SDL2::Surface(window);

    surface.FillRect();

    SDL_Delay(2000);

    return 0;
}
Surface(SDL2::Window window)
                    ^ missing &

您正在按值传递 Windows,导致 ~Window()Surface 构造函数 returns 后立即对基础 SDL_Window 进行核对,使 SDL_GetWindowSurface().

返回的表面无效

改为通过引用传递它们:

#include <SDL.h>
#include <stdio.h>

namespace SDL2
{
class Window
{
private:
    SDL_Window* mWindow;
    int ScreenWidth = 640;
    int ScreenHeight = 480;

public:
    Window()
    {
        mWindow = SDL_CreateWindow( "Breakout", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, ScreenWidth, ScreenHeight, SDL_WINDOW_SHOWN );
    }

    ~Window()
    {
        SDL_DestroyWindow( mWindow );
    }

    SDL_Window* GetWindow()
    {
        return mWindow;
    }
};

class Surface
{
private:
    SDL_Surface* mSurface;

public:
    Surface( SDL2::Window& window )
    {
        mSurface = SDL_GetWindowSurface( window.GetWindow() );
    }

    void FillRect()
    {
        SDL_FillRect( mSurface, NULL, SDL_MapRGB( mSurface->format, 0xFF, 0x00, 0xFF ) );
    }

    void Update( SDL2::Window& window )
    {
        SDL_UpdateWindowSurface( window.GetWindow() );
    }
};
}

int main( int argc, char** argv )
{
    //Initialize SDL
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
        return 1;
    }

    SDL2::Window window = SDL2::Window();

    SDL2::Surface surface = SDL2::Surface( window );

    surface.FillRect();

    surface.Update( window );

    SDL_Delay( 2000 );

    return 0;
}