如果 window 创建失败,我应该调用 SDL_DestroyWindow 吗?

Should I call SDL_DestroyWindow if window creation fails?

如果 Window 创建失败,我应该调用 SDL_DestroyWindow 吗?我有以下代码:

if(this->Window == NULL)
{
    std::cout << "Error: Can't create the SDL Window \n" << SDL_GetError() << "\n";
    SDL_DestroyWindow(this->Window);

    std::exit(EXIT_FAILURE);        
}

有错吗?

来自 the SDL wiki :

If window is NULL, this function will return immediately after setting the SDL error message to "Invalid window"

如果您一开始就没有 window,则不必调用 SDL_DestroyWindow:它不会执行任何操作(除了设置错误消息)。

你可以把它想象成 C 中的 free 或 C++ 中的 delete。如果你给他们 NULLnullptr(分别),他们什么都不做。