LLL LLL
LLL LLL

Reputation: 23

Losing pointer values

I'm getting the same log every time I run my program.

There is a snippet from the main.cpp, where every variable is in local scope:

EDIT: you find at the bottom.

I made these functions for start the game, run the menu loop, and close the graphics, and give false value for gameNoError if something goes wrong. But the last function gives me the following log message:

"No present window or/and renderer to close! SDL possibly not started, closed already or wrong pointers used!"

(Note, that I wrote all log messages myself, because I want to use logging to.)

So, this means that I loose somewhere the value of pointers, but I can't find out why?

There is my close function too:

bool closeGraphics(FILE* logFile, SDL_Window* gameWindow, SDL_Renderer* gameRenderer)
{
    bool success = false;

    if (gameWindow == NULL or gameRenderer == NULL)
    {
        doLogging(logFile, "No present window or/and renderer to close! SDL possibly not started, closed already or wrong pointers used!");
        success = false;
    }
    else
    {
        SDL_DestroyRenderer(gameRenderer);
        gameRenderer = NULL;
        SDL_DestroyWindow(gameWindow);
        gameWindow = NULL;

        doLogging(logFile, "Renderer and window destroyed successfully!");
    }

    TTF_Quit();
    IMG_Quit();
    SDL_Quit();

    return success;
}

I only use the pointers for rendering in menu loop (if you need I will paste that too!) and the pointers are used in scope.

So what is the problem?

EDIT: I found something: the menu loop looses the pointers too! But SDL starts and I made this not first. So I really don't know what is the problem.

Files from my project:

main.cpp:

#include <stdio.h>
#include <string>

#include <SDL.h>

#include "initialization/utility.hpp"
#include "initialization/init_closeGraphics.hpp"
#include "menuLoop.hpp"

int main(int argc, char* argv[])
{
    bool gameNoError = true;
    SDL_Renderer* gameRenderer = NULL;
    SDL_Window* gameWindow = NULL;

    FILE* logFile = fopen("errorLog.txt", "w");
    doLogging(logFile, "Game started successfully!");

    gameNoError = initGraphics(logFile, gameWindow, gameRenderer, "This is a title!", 800, 600);

    if (gameNoError) gameNoError = doMenuLoop(logFile, gameWindow, gameRenderer, 800, 600);

    if (gameNoError)
    {
        gameNoError = closeGraphics(logFile, gameWindow, gameRenderer);
    }
    else
    {
        closeGraphics(logFile, gameWindow, gameRenderer);
    }

    if (!gameNoError)
    {
        doLogging(logFile, "Game stopped with error/exception/problem!");
    }
    else
    {
        doLogging(logFile, "Game stopped successfully!");
    }

    return 0;
}

utility.hpp contains only the logging function

init_close.cpp: (it has it's header)

#include "init_closeGraphics.hpp"

bool initGraphics(FILE* logFile,
                  SDL_Window* gameWindow,
                  SDL_Renderer* gameRenderer,
                  const char* gameTitle,
                  int gameWindowWidth,
                  int gameWindowHeight)
{
    bool success = true;
    if (gameWindowWidth <= 0 or gameWindowHeight <= 0)
    {
        doLogging(logFile, "Get less then or equal with 0 window dimensions at SDL initialization!");
        success = false;
    }
    else
    {
        if(SDL_Init(SDL_INIT_VIDEO) < 0)
        {
            doLogging(logFile, "Failed to initialize SDL!");
            doLogging(logFile, SDL_GetError());
            success = false;
        }
        else
        {
            gameWindow = SDL_CreateWindow(gameTitle,
                                          SDL_WINDOWPOS_UNDEFINED,
                                          SDL_WINDOWPOS_UNDEFINED,
                                          gameWindowWidth,
                                          gameWindowHeight,
                                          SDL_WINDOW_SHOWN);
            if (gameWindow == NULL)
            {
                doLogging(logFile, "Failed to create window!");
                doLogging(logFile, SDL_GetError());
                success = false;
            }
            else
            {
                gameRenderer = SDL_CreateRenderer(gameWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
                if (gameRenderer == NULL)
                {
                    doLogging(logFile, "Failed to create renderer!");
                    doLogging(logFile, SDL_GetError());
                    success = false;
                }
                else
                {
                    SDL_SetRenderDrawColor(gameRenderer, 0xFF, 0xFF, 0xFF, 0xFF);

                    int IMG_FLAG = IMG_INIT_PNG;
                    if (!(IMG_Init(IMG_FLAG) & IMG_FLAG))
                    {
                        doLogging(logFile, "Failed to initialize SDL_image!");
                        doLogging(logFile, IMG_GetError());
                        success = false;
                    }

                    if (TTF_Init() == -1)
                    {
                        doLogging(logFile, "Failed to initialize SDL_ttf!");
                        doLogging(logFile, TTF_GetError());
                        success = false;
                    }
                }
            }
        }
    }
    return success;
}

bool closeGraphics(FILE* logFile, SDL_Window* gameWindow, SDL_Renderer* gameRenderer)
{
    bool success = false;

    if (gameWindow == NULL or gameRenderer == NULL)
    {
        doLogging(logFile, "No present window or/and renderer to close! SDL possibly not started, closed already or wrong pointers used!");
        success = false;
    }
    else
    {
        SDL_DestroyRenderer(gameRenderer);
        gameRenderer = NULL;
        SDL_DestroyWindow(gameWindow);
        gameWindow = NULL;

        doLogging(logFile, "Renderer and window destroyed successfully!");
    }

    TTF_Quit();
    IMG_Quit();
    SDL_Quit();

    return success;
}

Upvotes: 0

Views: 138

Answers (1)

TartanLlama
TartanLlama

Reputation: 65630

You are passing your pointer variables to initGraphics by value, so your call site will not see any changes to them.

If you want the variables in main to be updated when you call initGraphics, take them by reference:

bool initGraphics(FILE* logFile,
                  SDL_Window*& gameWindow,
//                           ^
                  SDL_Renderer*& gameRenderer,
//                             ^
                  const char* gameTitle,
                  int gameWindowWidth,
                  int gameWindowHeight)

Upvotes: 3

Related Questions