5 Replies - 270 Views - Last Post: 01 December 2014 - 01:15 PM Rate Topic: -----

#1 Nicky24  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 78
  • Joined: 23-December 13

Memory leak when switching game state

Posted 29 November 2014 - 12:40 PM

Hello everyone.
I am having a memory leak problem when I am switching my game states (for example, from Map to Menu).
I have noticed that the problem is when I am creating the windows on the Init() method I have on each state, like so:

void Menu_State::Init(Engine *game)
{
	win_base = new GUI::Window_Selectable(10, 10, 130, 220, win_base->CENTRE);

	string s[6] = {"Status", "Quests", "Equipment", "Inventory", "Save", "Exit"};

	for(int i = 0; i < (sizeof(s)/sizeof(*s)); i++)
			win_base->Add_Field(s[i]);

	win_base->Active(true);
}



The win base object is giving me trouble, every time that I switch the state from Map_State to Menu_State, the RAM used by my application is growing about 200kb each switch, until I reach around 100mb of Ram, the application stops working and trows an error.
I have tried to improve my Change_State() method as follows:

void Engine::Change_State(IState *state)
{
	//Clean the current state
	if(!states.empty())
	{
		states.back()->Cleanup();
		states.pop_back();
	}

	//Store and init the new state
	states.push_back(state);
	states.back()->Init(this);
}


And the Cleanup() method does this:
void Menu_State::Cleanup()
{
    delete win_base;
}



But anyway I am having the same problem...I really don't know what to do.

Edit
The window class is accessible to Lua, so I need a solution for both Lua and C++ :(/>

Thanks

This post has been edited by Nicky24: 29 November 2014 - 12:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Memory leak when switching game state

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: Memory leak when switching game state

Posted 29 November 2014 - 03:29 PM

Why do you create a window in each state? Shouldn't you just change what's drawn to the window?
Was This Post Helpful? 0
  • +
  • -

#3 Nicky24  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 78
  • Joined: 23-December 13

Re: Memory leak when switching game state

Posted 29 November 2014 - 05:57 PM

The window is an abstraction, in my game the window is a rectangle that contains elements like text, images, etc... So the Window is like the rectangles that appear in the game for showing containers
Was This Post Helpful? 0
  • +
  • -

#4 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 592
  • View blog
  • Posts: 1,322
  • Joined: 24-April 12

Re: Memory leak when switching game state

Posted 29 November 2014 - 09:22 PM

I'll be the first to admit I'm not that great with C++ pointers.

But that "new GUI::Window_Selectable()" looks awfully suspicious to me. So far in most of the C++ I've done with DirectX, I surprisingly enough have not had to really allocate any memory. Everything's done on the stack or through DX. So, I'm not getting a lot of practice with memory allocation/deallocation but I'm also avoiding problems like this.

I believe new does a memory allocation on the heap. So, I think its allocated until you actually do a delete to deallocate it. If you lose the pointer to it, you merely lose the pointer to it (memory leak), but its still allocated.

I'm mostly used to working in C# with managed memory and the garbage collector just handles that stuff. But I think in C++, you never let go of a pointer until the memory it references is deallocated.

I guess win_base is a global variable? If you do assign it once, and then assign it again, aren't you merely losing the object that win_base pointed to before that? Shouldn't win_base always be null when you do a new to allocate a new object? Otherwise, I would think it needs to pass the pointer off to storage elsewhere, like an array.

I've been working on a prototype for DX in C# here lately and haven't done any C++ in awhile. I had to go back and look at my code. I'm not using "new" or "malloc" anywhere. I think I had a problem where I was and some of the C++ guys told me that I was probably coding it wrong if I was and went back and figured out how to recode it without a memory allocation.

Generally, I have classes where I allocate the new classes as member variables of the first class. So my "Game" class might have members for any other class I want to use. Although, I'm using a void parameter list for the constructor on those classes.

Then they're just like any basic data type; I don't have to do a "new" allocation because they are part of the class. Now if you have several objects being created, that might be a bit different. I'm getting away with just having one instance of each object in my code so far. If you had an unknown variable number of objects being instantiated, then it seems to me you have to keep a pointer to every single one you allocate off the heap.

Anyway, it seems to me that if win_base is the only variable holding the pointer it would need to be null every time its assigned since you're overwriting it there and potentially losing the object that it pointed to into the abyss of a memory leak. If it's not null, it would seem to me like you would have to save its value somewhere so that you don't lose that pointer.

Seems to me like you might be allocating some memory with a pointer pointing to it, then overwriting that pointer to a new block of allocated memory without saving the old pointer value. Thus, the memory's still allocated, but you've got no way to tell it how to deallocate the memory you've just lost track of. I would think you need to be able to point to every single block of memory you allocate so that you could deallocate every block when its over.

This could probably be accomplished in several different ways. If it were a linked list, you would only need a pointer to the first record because the chain would lead you to the next block. But if they're just objects that don't keep track of one another, then you would have to do something like use an array of pointers or a linked list of pointers or something from STL (vectors I think they call them) to make a list of every pointer that's allocated.

Otherwise, you probably need to deallocate the memory before reassigning the pointer and permanently loosing the pointer. And its good practice to set the pointer back to null once it doesn't point to anything valid. Not sure if delete does that for you or not.

This post has been edited by BBeck: 29 November 2014 - 09:35 PM

Was This Post Helpful? 0
  • +
  • -

#5 Nicky24  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 78
  • Joined: 23-December 13

Re: Memory leak when switching game state

Posted 30 November 2014 - 08:50 AM

Hello BBeck, thanks for your answer.
The problem is that I must initialize it as a pointer because I am using it for my Lua scripting integration, so I am doing it like this:

int l_Window_Constructor(lua_State *L)
{
	int x1 = luaL_checkint(L, 1);
	int y1 = luaL_checkint(L, 2);
	int x2 = luaL_checkint(L, 3);
	int y2 = luaL_checkint(L, 4);

	GUI::Window ** udata = (GUI::Window **)lua_newuserdata(L, sizeof(GUI::Window *));
	*udata = new GUI::Window(x1, y1, x2, y2);

	luaL_getmetatable(L, "luaL_Window");
	lua_setmetatable(L, -2);

	return 1;
}

So in lua I do it like so:

Scene_Menu.win_base = window.new(10, 10, 130, 220)


But anyway I noticed that if I comment this line out, I don't have any Ram running out... I don't know how to fix that, I thought that lua could recycle all the unused memory :S
Was This Post Helpful? 0
  • +
  • -

#6 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 592
  • View blog
  • Posts: 1,322
  • Joined: 24-April 12

Re: Memory leak when switching game state

Posted 01 December 2014 - 01:15 PM

Now you're getting into dereferenced pointers and such. I'm out. I fold. ;-)

I've never been particularly good with pointers.

If I were doing something like that that involved pointers, I would work on making it a "strong point" instead of a weakness. I would concentrate on that and probably spend at least a good week just studying pointers. I'd probably read a book like "Understanding and Using C Pointers" by Richard Reese.

Fortunately, nothing I'm doing seems to involve pointers (except very consistant and well documented pointer syntax in DX11 where I don't really have to think about it), especially dereferenced pointers and pointers 2, 3, 4, 87 levels deep.

I don't know how everyone else feels about it, but I think you should ask the question over in the C++ forum. Those guys over there are really sharp; they run circles around me when it comes to knowing how C++ works and how it "should" work. I think most of them don't follow the game programming forum; I think they stick pretty close to the C++ forum.

Certainly, when I get stuck on a C++ issue (which is always going to be game programming related in my case, at least on some level), I never hesitate to ask my C++ questions over there. They may not be able to help you specifically with DX, OpenGL, some game framework, some game API, or even game programming specific stuff. But when it comes to things like memory allocations and pointers, that would actually be the first place I would go.

Sorry I can't be more helpful than that, but I'd probably have to spend a week just remembering enough about pointers and whatnot to even get up to speed to know what we're talking about, let alone help solve it. You'll probably have it figured out before I could even get up to speed on it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1