Sign in to follow this  
Ubermeowmix

Anyone got any ideas where 4k a sec is coming from?

Recommended Posts

I've got a basic program running SDL2 that loads a surface and a class wrapper that handles object creation/texture loading. Before I even start to generate objects SDL is streaming extra memory at around 4k a second.

 

Is this a known issue or could I have created some weird loop. I don't call any new objects in my code apart from the class to hold the objects that will be created:

 

pAsset_explosions= new GeneralObject("disposable_explosion.png", screenCoords);
pAsset_explosions->fLoadImages(mpRenderer);

Share this post


Link to post
Share on other sites

Yes when the program closes I release it with delete.

 

It's not that (i assume) that's causing it it's happening when the program is running and before anything else has been created using new.

 

How do I capture a problem like this in code blocks?

Share this post


Link to post
Share on other sites

Okay code time...

#include "CApp.h"
 
int main(int argc, char* argv[])
{
return CApp::GetInstance()->OnExecute(argc, argv);
}
#ifndef CAPP_H_INCLUDED
#define CAPP_H_INCLUDED
 
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
 
#include <sstream>
#include <iostream>
#include <stdio.h>
#include <string>
 
#include <memory>
 
#include "Texture.h"
#include "TextureText.h"
#include "LoadMedia.h"
 
class Texture;
class LoadMedia;
 
class CApp
{
//private:
    protected:
        static const int WindowWidth = 1024;
        static const int WindowHeight = 768;
 
        SDL_Window* Window = NULL;
        SDL_Renderer* Renderer = NULL;
 
        bool bLoadMedia= false;
 
    private:
        static CApp Instance;
        bool Running = true;
 
    public:
        CApp() { }
        ~CApp() { }
 
        bool OnInit();
 
        bool fLoadMedia()
        {
            WindowSize.w = SDL_GetWindowSurface(Window)->w;
            WindowSize.h = SDL_GetWindowSurface(Window)->h;
            SDL_Rect screenCoords= {0, 0, WindowSize.w, WindowSize.h};
 
            if(bLoadMedia)
            {
                mediaAssets= new LoadMedia(Renderer, MainTTfont, screenCoords);
                if(mediaAssets->fInitializeMedia())
            }
            return true;
        }
 
        void OnEvent(SDL_Event* Event)
        {
            if(Event->type == SDL_QUIT) Running = false;
            if(Event->type == SDL_KEYDOWN)
            {
                switch(Event->key.keysym.sym)
                {
                    case SDLK_ESCAPE:
                        Running = false;
                    break;
                }
            }
 
            if(bLoadMedia)
                mediaAssets->fOnMediaEvent(Event);
        }
 
        void OnLoop()
        {
            ///UPDATE ASSETS
            if(bLoadMedia)
                mediaAssets->fCheckMediaAssets();
        }
 
        void OnRender()
        {
            SDL_SetRenderDrawColor(Renderer, 100, 0x00, 200, 0xFF);
            SDL_RenderClear(Renderer);
 
            if(bLoadMedia)
                mediaAssets->fRenderAssets();
 
            SDL_RenderPresent(Renderer); //Use this function to update the screen with rendering performed.
        }
 
        void OnCleanup()
        {
            if(bLoadMedia)
                mediaAssets->fCleanupAssets();
 
            if(Renderer)
            {
                SDL_DestroyRenderer(Renderer);
                Renderer = NULL;
            }
 
            if(Window)//All screen surfaces are de-constructed by SDL_DestroyWindow
            {
                SDL_DestroyWindow(Window);
                Window = NULL;
            }
 
            IMG_Quit();
            SDL_Quit();
        }
 
    public:
int OnExecute(int argc, char* argv[]);
 
public:
static CApp* GetInstance();
        int intAval = 0;
 
    public:
        SDL_Rect WindowSize;
 
        LoadMedia* mediaAssets= NULL;
        TTF_Font* MainTTfont= NULL;
        SDL_Surface* LoadSurface= NULL;
};
 
#endif // CAPP_H_INCLUDED
Edited by Ubermeowmix

Share this post


Link to post
Share on other sites

 
#include "capp.h"
 
CApp CApp::Instance;
 
int CApp::OnExecute(int argc, char* argv[])
{
    if(!OnInit()) return 0;
 
SDL_Event Event;
 
while(Running) {
while(SDL_PollEvent(&Event) != 0) {
OnEvent(&Event);
}
 
OnLoop();
OnRender();
 
SDL_Delay( 60 );
}
 
OnCleanup();
 
return 1;
}
 
CApp* CApp::GetInstance() { return &CApp::Instance; }

Share this post


Link to post
Share on other sites

#include "capp.h"
 
bool CApp::OnInit() {
    if(SDL_Init(SDL_INIT_VIDEO) < 0) {
     printf("Unable to Init SDL: %s", SDL_GetError());
     return false;
    }
 
    if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) {
        printf("Unable to Init hinting: %s", SDL_GetError());
    }
 
    if((Window = SDL_CreateWindow(
     "My SDL Game",
     SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
WindowWidth, WindowHeight, SDL_WINDOW_SHOWN)
    ) == NULL) {
     return false;
    }
 
    LoadSurface = SDL_GetWindowSurface(Window);
 
    if((Renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC )) == NULL) {
        return false;
    }
 
// Initialize image loading for PNGs
if(!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG))
    {
return false;
}
 
if(TTF_Init()==-1)
    {
        dMSG("SDL_ttf could not initialize! SDL_ttf Error: ", 0);
    }
 
 
    if(!fLoadMedia())
    {
        return false;
    }
 
    return true;
}
 

Share this post


Link to post
Share on other sites

Just noticed that this is happening even when I stop the media (images and classes to create them) loading, is this the application instance or a windows thing?

 

4788k

4792k

4804k

4808k

 

it does this in task manager over the course of about 10-20 seconds and keeps going indefinitely, puzzled by this.

Share this post


Link to post
Share on other sites

Even when it's sat dormant doing nothing?

 

not loading anything and no input from keyboard mouse or anything:

At start of timer:

4792k

after five mins of inactivity:

4832k

 

Initializing media by loading one image into a class, but not activating any objects using new.

At start:

6920k

after five mins of inactivity:

175,036k

 

That's more than a 2 hour soundtrack movie on youtube!!!

Share this post


Link to post
Share on other sites

This may or may not be related but, I spotted this leak:

 

        bool fLoadMedia()
        {
            WindowSize.w = SDL_GetWindowSurface(Window)->w;
            WindowSize.h = SDL_GetWindowSurface(Window)->h;
            SDL_Rect screenCoords= {0, 0, WindowSize.w, WindowSize.h};
 
            if(bLoadMedia)
            {
                mediaAssets= new LoadMedia(Renderer, MainTTfont, screenCoords);
                if(mediaAssets->fInitializeMedia())
            }
            return true;
        }

 

 

Then in the cleanUp function...

 

        void OnCleanup()
        {
            if(bLoadMedia)
                mediaAssets->fCleanupAssets();
...

 

You are not deleting the allocated memory pointed at by mediaAssets, unless you are calling `delete this;` in fCleanupAssets(); It should be

 

if(bLoadMedia)
{ 
     mediaAssets->fCleanupAssets();
     delete mediaAssets;
}

Share this post


Link to post
Share on other sites

Okay new problem, probably related tbh.

 

When a linked list has the following:

 

class MyObject
{
    int count;
    int iCurrentThingy;
 
public:
    MyObject()
    {
        dMSG("MyObject constructor called");
    }
 
    MyObject(int i) : iCurrentThingy(i)
    {
        dMSG("MyObject overloaded constructor called");
    }
 
    ~MyObject()
    {
        dMSG("MyObject destructor called");
    }
 
    int fReturnThingy()
    {
        return iCurrentThingy;
    }
};
 
LinkedList<MyObject> EntityList;

 

And I call the following on adding to said list

 

void fInsertAtHead(ListItem* listItem)
    {
        Node* pTemp= new Node;
        pTemp->current= listItem;
        pTemp->next= head;
        head= pTemp;
        count++;
    }

Share this post


Link to post
Share on other sites

Then call this to clear the list:

 

void fClearAll()
    {
        while(head!=NULL)
        {
            Node* pTBDel= head;
            delete pTBDel->current;
            head= head->next;
            count--;
        }
    }

Share this post


Link to post
Share on other sites
Surely if I have a newly created object to Node and a newly created object referenced by the node I have to delete both.
 
So why when I clear all with the destructor does it crash?
 


Node* pTBDel= head;
            delete pTBDel->current;///DELETE THE ITEM REFERENCE
            delete pTBDel;///DELETE THE POINTER
            head= head->next;
            count--;

Share this post


Link to post
Share on other sites

DUH!!! Don't delete the reference to head before moving it forwards, I put the delete before changing head's pointer reference.

 

            head= head->next; ///UPDATE THE SMEGGING REFERENCE FIRST
            delete pTBDel->current;///DELETE THE ITEM REFERENCE
            delete pTBDel;///DELETE THE POINTER
            //head= head->next;

 

Sorry to be a pain, I do try and solve these things myself generally but this one was driving me mad.

Share this post


Link to post
Share on other sites

And strangely enough was the cause of the other memory leak as well, I have no idea how they were linked as it shouldn't have been calling until input was passed to the program through the keyboard.

 

As always I have a lot to learn and no time to learn it in lol.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this