SDL and Memory issues

I am trying to make a game engine from SDL but i am having trouble when i run a test program with my engine it freezes but only if i run it on release when i run it on debug it works good.
I get a warning that says text is uninitalized.
i think the problem is in the Load function in texture class.
Here is the header and cpp files.
By the way its only like %.5 done.
Thanks for your help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*AS.h*/
#include "SDL.h"
#include "SDL_image.h"
#include <string>
extern int AS_Error;
namespace AS
{
    /* types */

    /* Function */
    int AS_Init();
    void AS_Quit();
    /* Classes */
    class Texture{
    private:
    SDL_Texture *tex;
    public:
        int Load(std::string name,SDL_Renderer* render);
        SDL_Texture* getTex();
        void Destroy();
    };

}


#endif // AS_H_INCLUDED
/*AS.cpp*/
#include "AS.h"
int AS_Error;
int AS::AS_Init()
{
    return SDL_Init(SDL_INIT_EVERYTHING); // 1
}
void AS::AS_Quit()
{
    SDL_Quit(); // 2
}
void AS::Texture::Destroy()
{
    SDL_DestroyTexture(tex);
}
int AS::Texture::Load(std::string name,SDL_Renderer* render)
{
    SDL_Surface *temp;
    temp = IMG_Load(name.c_str());
    if(temp == nullptr)
    {
        AS_Error = 5; // cant load
        return -1;
    }
    tex = SDL_CreateTextureFromSurface(render,temp);
    SDL_FreeSurface(temp);
    return 0;
}
SDL_Texture* AS::Texture::getTex()
{
    return tex;
}
/*test.cpp*/
#include "AS.h"
#include <iostream>
using namespace AS;
int main(int argc, char* argv[])
{
    AS_Init();
    Texture *text;
    SDL_Renderer *screen;
    SDL_Window *win = SDL_CreateWindow("Engine",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,640,480,SDL_WINDOW_SHOWN);
    screen = SDL_CreateRenderer(win,-1,SDL_RENDERER_ACCELERATED);
    text->Load("background.png",screen);
    std::cout << AS_Error;
    SDL_RenderCopy(screen,text->getTex(),NULL,NULL);

    SDL_RenderPresent(screen);
    text->Destroy();
    SDL_Delay(2500);
    AS_Quit();

    return 0;
}
Line 70 should always crash your program since text is an uninitialized pointer. And it's probably better to check if screen and win are valid.

What does "freezes" mean exactly? I don't see a game loop so it should only render the background once and exit after a while.
Last edited on
But the Load funtion loads a png to the pointer thus initializing the pointer.
Texture::Load() valorizes Texture::tex, not text declared on line 66. To put it simply, you're trying to call a function of an object that doesn't exist (which also means that there isn't any tex to hold the texture).

You can either create a Texture object directly, or assign that pointer to a new Texture.

You should also write a ctor that at least initializes tex to nullptr, and a dtor that deletes it. Calling Texture::Destroy() when tex is null is ok, but if it's uninit'd you'll get a segfault. And Texture::Load() should probably check if tex already holds a valid value. If you happen to call it more than once for the same object you'll have a memory leak.
Last edited on
I get it Load() initializes tex but it doesnt inizialize the acutal class.So to fix it i make text a variable not a pointer and initialize tex.
Topic archived. No new replies allowed.