Microsoft Exception C++: _com_error at memory location - Stack Overflow

admin2025-04-17  3

I'm starting to learn SDL3 and I'm getting this weird error while trying to render a texture using SDL_RenderTexture

The error messages are:

Exception produced at 0x00007FFCA9067788 in A Fairy Tail.exe: Microsoft C++ exception: _com_error in memory location 0x000000F86C0FED60

And the SDL_GetError() one:

FUNCTION, ID3D11Device1::CreatePixelShader: Parameter is not correct

Apparently they only pop up when I use the function SDL_GetError() to check if the texture has been loaded propperly, specifically these lines of code:

if(SDL_RenderTexture(renderer, fairy, NULL, NULL)

{

  SDL_Log("IMG Error: %s", SDL_GetError());

}

Strangely, they don't pop up when I check the same condition but for a false value, only for true values as in the lines above, meaning (I guess) the texture is being rendered, but it simply doesn't show up at the window and indstead the console sends these error messages.

Here's the complete code for better understanding:

//Header file mygame.h

#pragma once

#include <iostream>
#include "SDL3/SDL.h"
#include "SDL_image.h"

class Game
{
public:
    Game();
    ~Game();

    void init(const char*, int xpos, int ypos, int width, int height, bool fullscreen);
    void handle_events();
    void update();
    void render();
    void clean();

    bool running();

private:
    bool is_running = NULL;
    SDL_Window* window = NULL;
    SDL_Renderer* renderer = NULL;

//Source File for the function definitions of the above header file mygame.cpp

#include "mygame.h"

SDL_Texture* fairy = NULL;

Game::Game() {}
Game::~Game() {}

//Initialization function

void Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
{
    int flags = 0;
    if (fullscreen)
    {
        flags = SDL_WINDOW_FULLSCREEN;
    }
    if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        std::cout << "Error: Couldn't initialize SDL!" << std::endl;
        is_running = false;
        return;
    }

//Creating Window

    window = SDL_CreateWindow(title, width, height, flags);
    if(window)
    {
        std::cout << "Window Created!";
    }

//Creating Renderer

    renderer = SDL_CreateRenderer(window, NULL);
    if(renderer)
    {
        std::cout << "Renderer Created" << std::endl;
    }

is_running = true;

fairy = IMG_LoadTexture(renderer, "assets/hadita.png");
} 

void Game::handle_events()
{
    SDL_Event event;
    SDL_PollEvent(&event);
    switch(event.type)
    {
         case SDL_EVENT_QUIT:
            is_running = false;

         default:
             break;
    }
}

void Game::update(){}

void Game::render()
{
    SDL_RenderClear(renderer);
    if(SDL_RenderTexture(renderer, fairy))
    {
        SDL_Log("IMG Render Error: %s", SDL_GetError());
    }
    SDL_RenderPresent(renderer);
}

void Game:clean()
{
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyTexture(fairy);
    SDL_Quit();
    std::cout << "Game cleaned";
}

bool Game::running()
{
    return is_running;
}

//Now finally the main.cpp file where the game loop is

#include "mygame.h"

Game* game = nullptr;

int main(int argc, const char* argv[])
{
    game = new Game();
    game->init("A Fairy Tale", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);
    while (game->running())
    {
        game->handle_events();
        game->update();
        game->render();
    }

game -> clean();

return 0;
}

I'm starting to learn SDL3 and I'm getting this weird error while trying to render a texture using SDL_RenderTexture

The error messages are:

Exception produced at 0x00007FFCA9067788 in A Fairy Tail.exe: Microsoft C++ exception: _com_error in memory location 0x000000F86C0FED60

And the SDL_GetError() one:

FUNCTION, ID3D11Device1::CreatePixelShader: Parameter is not correct

Apparently they only pop up when I use the function SDL_GetError() to check if the texture has been loaded propperly, specifically these lines of code:

if(SDL_RenderTexture(renderer, fairy, NULL, NULL)

{

  SDL_Log("IMG Error: %s", SDL_GetError());

}

Strangely, they don't pop up when I check the same condition but for a false value, only for true values as in the lines above, meaning (I guess) the texture is being rendered, but it simply doesn't show up at the window and indstead the console sends these error messages.

Here's the complete code for better understanding:

//Header file mygame.h

#pragma once

#include <iostream>
#include "SDL3/SDL.h"
#include "SDL_image.h"

class Game
{
public:
    Game();
    ~Game();

    void init(const char*, int xpos, int ypos, int width, int height, bool fullscreen);
    void handle_events();
    void update();
    void render();
    void clean();

    bool running();

private:
    bool is_running = NULL;
    SDL_Window* window = NULL;
    SDL_Renderer* renderer = NULL;

//Source File for the function definitions of the above header file mygame.cpp

#include "mygame.h"

SDL_Texture* fairy = NULL;

Game::Game() {}
Game::~Game() {}

//Initialization function

void Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
{
    int flags = 0;
    if (fullscreen)
    {
        flags = SDL_WINDOW_FULLSCREEN;
    }
    if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        std::cout << "Error: Couldn't initialize SDL!" << std::endl;
        is_running = false;
        return;
    }

//Creating Window

    window = SDL_CreateWindow(title, width, height, flags);
    if(window)
    {
        std::cout << "Window Created!";
    }

//Creating Renderer

    renderer = SDL_CreateRenderer(window, NULL);
    if(renderer)
    {
        std::cout << "Renderer Created" << std::endl;
    }

is_running = true;

fairy = IMG_LoadTexture(renderer, "assets/hadita.png");
} 

void Game::handle_events()
{
    SDL_Event event;
    SDL_PollEvent(&event);
    switch(event.type)
    {
         case SDL_EVENT_QUIT:
            is_running = false;

         default:
             break;
    }
}

void Game::update(){}

void Game::render()
{
    SDL_RenderClear(renderer);
    if(SDL_RenderTexture(renderer, fairy))
    {
        SDL_Log("IMG Render Error: %s", SDL_GetError());
    }
    SDL_RenderPresent(renderer);
}

void Game:clean()
{
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyTexture(fairy);
    SDL_Quit();
    std::cout << "Game cleaned";
}

bool Game::running()
{
    return is_running;
}

//Now finally the main.cpp file where the game loop is

#include "mygame.h"

Game* game = nullptr;

int main(int argc, const char* argv[])
{
    game = new Game();
    game->init("A Fairy Tale", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, false);
    while (game->running())
    {
        game->handle_events();
        game->update();
        game->render();
    }

game -> clean();

return 0;
}
Share Improve this question edited Jan 31 at 12:11 user29427142 asked Jan 31 at 11:27 user29427142user29427142 12 bronze badges 10
  • Does you render method really destroy all the SDL3 state? Your call to SDL_RenderTexture is nowhere to be seen... I think you screwed up copy pasting. – Botje Commented Jan 31 at 12:03
  • 1 Is your copy-and-paste broken? That Game::render looks like it should be Game::clean, and there's booll... Please post real code. – molbdnilo Commented Jan 31 at 12:05
  • You're not checking whether the texture loading succeeded. – molbdnilo Commented Jan 31 at 12:12
  • 1 SDL_RenderTexture returns false on failure, not true... Likewise, IMG_LoadTexture returns NULL on failure. Check both calls please. – Botje Commented Jan 31 at 12:24
  • 2 Calling GetError after a successful operation is misleading. It is entirely possible that the function you called tried a few different approaches and the last call was succesful. Only call GetError after you have received an error from SDL. – Botje Commented Jan 31 at 12:43
 |  Show 5 more comments

1 Answer 1

Reset to default 0

Always use error checks only for getting a error means when something is not returning correctly.

if (!SDL_RenderTexture(renderer, fairy)) { }

Just think like that if SDL_RenderTexture() is returning correctly why would there be an error. SDL_GetError() gives you the latest error at that point.

转载请注明原文地址:http://anycun.com/QandA/1744865118a88710.html