-3

i made a program to move a dot around on the screen. i was basically just recreating lazyfoo sdl tutorial. the problem is my program eats memory. its memory usage is slowly increasing at like 200kb/s rate. it grows bigger overtime.

i tested it using visual leak detector, it says no memory leak. so what did i do wrong?? please help me. it's driving me crazy.

here is the code: credit to lazyfoo. if the code is too long or unclear i will edit it as necessary. thank you.

//Main Stage
int main(int argc, char* args[])
{
    if (!init())
    {
        printf("failed to init! \n");
    }
    else
    {
        cTile *tileSet[TOTAL_TILES];

        if (!loadMedia(tileSet))
        {
            printf("failed to load media! \n");
        }
        else
        {
            bool running = true;
            SDL_Rect wall;
            wall.x = 200;
            wall.y = 200;
            wall.h = 150;
            wall.w = 70;
            cDot Dot1(1270, 720);
            cDot Dot2(SCREEN_HEIGHT / 4, SCREEN_WIDTH / 4);
            cTimer timer;
            SDL_Event e;
            SDL_Color textColor = { 0, 0, 120, 120 };

            std::vector<SDL_Rect> camera;
            camera.resize(1);
            camera[0] = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };

            Uint32 startTime = 0;
            std::stringstream timeText;
            int countedFrames = 0;
            timer.start();

            while (running)
            {
                while (SDL_PollEvent(&e) != 0)
                {
                    if (e.type == SDL_QUIT)
                    {
                        running = false;
                    }

                    Dot1.handleEvent(e);

                    for (int i = 0; i < TOTAL_BUTTONS; i++)
                    {
                        buttons[i].buttonEvent(&e);
                    }
                }

                float avgFPS = countedFrames / (timer.getTicks() / 1000.f);
                if (avgFPS > 2000)
                {
                    avgFPS = 0;
                }

                if (!textTexture.loadTextFromFile(timeText.str().c_str(), textColor))
                {
                    printf("unable to render text ypoooooo");
                }

                timeText.str("");
                timeText << "fps " << avgFPS;

                Dot1.move(tileSet);
                Dot1.setCamera(camera[0]);

                /*camera.x = (Dot1.getPosX() + cDot::DOT_WIDTH / 2) - SCREEN_WIDTH / 2;
                camera.y = (Dot1.getPosY() + cDot::DOT_HEIGHT / 2) - SCREEN_HEIGHT / 2;*/

                /*if (camera.x < 0){ camera.x = 0; }
                if (camera.y < 0){ camera.y = 0; }
                if (camera.x > LEVEL_WIDTH - camera.w){ camera.x = LEVEL_WIDTH - camera.w; }
                if (camera.y > LEVEL_HEIGHT - camera.h){ camera.y = LEVEL_HEIGHT - camera.h; }*/

                SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
                SDL_RenderClear(renderer);

                //SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
                //SDL_RenderDrawRect(renderer, &wall);
                for (int i = 0; i < TOTAL_TILES; ++i)
                {
                    tileSet[i]->render(camera);
                }
                //BGTexture.render(0, 0, &camera);
                Dot1.render(camera[0]);

                textTexture.render(250, 250);
                timeTextTexture.render(250, 250 + textTexture.getWidth());
                for (int i = 0; i < 2; i++)
                {
                    //buttons[i].render();
                }


                //Dot2.render(camera.x, camera.y);

                SDL_RenderPresent(renderer);
                if (timer.started())
                {
                    countedFrames++;
                }
                else if (!timer.started())
                {
                    countedFrames = 0;
                }
            }
        }
        close(tileSet);
    }
    return 0;
}

EDIT:

solution is so simple THANKS TO PAUL AND WEATHER VANE !!!! apparently i put these lines of code inside a loop. where its main function is basically creating texture. and it loads texture over and over again because it's inside a loop. i just need to delete it. these lines:

if (!textTexture.loadTextFromFile(timeText.str().c_str(), textColor))
                    {
                        printf("unable to render text ypoooooo");
                    }

THANK YOU GUYS!!! :) sorry i can't vote up, apparently it needs more reputation to vote an answer up. :|

GroniumArgor
  • 103
  • 1
  • 11
  • 11
    Things not on my to-do list today: Dig through 1000 lines of code to find someone's memory leaks. – Cory Kramer Feb 05 '15 at 18:43
  • 2
    Show where the allocations take place – James Moore Feb 05 '15 at 18:44
  • 3
    I think you need to identify what else you've done except run a memory profiler. In other words, identify places where you think you might have your issue. It probably has to do with allocating memory, but not freeing it, pointers to nothing, etc. Identify those spots in your code, unless you don't know how to figure that out at all. – zealoushacker Feb 05 '15 at 18:45
  • yeah, it seems that making someone dig through my code is too much. my bad. i'm sorry guys. i'll just delete my question. and thank you. – GroniumArgor Feb 05 '15 at 18:54
  • @user3189174 Something like valgrind may come in handy. – πάντα ῥεῖ Feb 05 '15 at 18:58
  • i use windows, valgrind runs on linux right? – GroniumArgor Feb 05 '15 at 18:59
  • @user3189174 Depends on your actual toolchain. With MSVC there are the [MS tools](http://stackoverflow.com/questions/4790564/finding-memory-leak-of-c-application-in-visual-studio), for others you might want to use [valgrind for windows](http://sourceforge.net/p/valgrind4win/wiki/DevelopmentEnvironment/) – πάντα ῥεῖ Feb 05 '15 at 19:02
  • Just a comment, but why the raw pointers? Have you considered a unique_ptr or shared_ptr so you don't have to go through this. – Beached Feb 05 '15 at 19:07
  • honestly, i'm fairly new to c++, i made this program 2 days ago. and still learning how pointer works. and apparently the tutorial is using raw pointers. i'll read some more about unique and shared pointers. thank you. – GroniumArgor Feb 05 '15 at 19:10
  • @user3189174 Concentrate on your `main()` function. What calls are you making that obviously allocate memory, whether you're doing it explicitly, or calling an `SDL_` function? You have a while() loop going, and very few of those functions actually do anything w.r.t memory allocation. It shouldn't be that difficult to pinpoint *where* the error occurs, even if you don't know *how* to fix the error. – PaulMcKenzie Feb 05 '15 at 19:15
  • @user3189174 In addition, since your classes do not follow the "rule of 3", please add unimplemented, user-defined copy constructor and assignment op (or if using C++ 11, `delete` them). This ensures that you're not copying your objects, thus causing a memory leak to potentially occur. – PaulMcKenzie Feb 05 '15 at 19:19
  • THANK YOU PAULMCKENZIE!!! i found it because of you. thank you so much!!! i'm happy. :D – GroniumArgor Feb 05 '15 at 19:25
  • 1
    Ah yeah. The general guideline is use smart pointers where you can. – Beached Feb 05 '15 at 21:03

1 Answers1

3

I suspect this which only deletes when NULL.

for (int i = 0; i < TOTAL_TILES; ++i)
{
    if (tiles[i] == NULL)
    {
        delete tiles[i];
        tiles[i] = NULL;
    }
}

EDIT:

Here is another shot: you call loadTextFromFile() in your main loop repetition, but only call SDL_DestroyTexture() in the final close().

And here is a similar question, Memory leak SDL while using SDL_CreateTextureFromSurface

Community
  • 1
  • 1
Weather Vane
  • 33,872
  • 7
  • 36
  • 56