Help with a memory leak?

Started by
3 comments, last by Joe Bob 21 years ago
After debating between general programming and here, I decided to post in here, as I figure the answer has more to do with me being a noob than anything else. I''ve got the Data Structures for Game Programmers book, and I''m trying to change some of Rons SDLGUI around a little bit. I''ve ended up with a memory leak, and I don''t know why. He''s got a class SDLLabel (amongst others) derived from SDLGUIItem, and an SDLGUI that will use them all. In the SDLGUI constructor is m_items = new SDLGUIItem*[m_itemmax]; Basically, allowing you to have m_itemmax GUI constructs, like labels, buttons, etc. I''ve been trying to add something that will allow me to dynamically change the text on a label, and have come to the conclusion that it will be easier just to delete the existing pointer and create a new one. So I''ve added the function: void SDLGUI::RefreshLabel( int p_x, int p_y, int p_index, const char* p_text, int p_font, SDL_Color p_fore, SDL_Color p_back ) { delete m_items[p_index]; m_items[p_index] = new SDLLabel( p_x, p_y, p_text, p_fore, p_back, m_fonts[p_font] ); } and in my main loop I''ve got the following: //Update the Mouse Label strcpy(labeltext,"Mouse Position is\0"); SDL_GetMouseState( &m_x, &m_y); sprintf(m_xpos,"%d",m_x); sprintf(m_ypos,"%d",m_y); strcat(labeltext,": "); strcat(labeltext,m_xpos); strcat(labeltext,", "); strcat(labeltext,m_ypos); strcat(labeltext,"\0"); g_gui->RefreshLabel(10,10,labelindex,labeltext,ARIAL16,BLACK,WHITE); This has led to a memory leak that I can''t figure out. The label gets updated correctly, but my available memory is sinking fast. I though maybe it was because I was deleting a pointer to SDLGUIItem instead of SDLLabel, so I changed my delete to delete (SDLLabel*)m_items[p_index]; But I still have the leak. Any ideas on where I''m going wrong would be appreciated? Thanks,
Advertisement
delete [] m_items;
ya glass is right, always use [] unless you''re using basic types (char int float etc)
That''s just really wrong advice. Please don''t post when you don''t know what you''re talking about.

He''s deleting one item within the array, not the array itself. Only delete[] things allocated with new[]. delete things allocated with new.

The memory leak is not apparent from your code. Perhaps the SDLLabel class itself has a leak? Do all your base classes have virtual destructors?

Apply your favourite leak checking tool, and ask it where the memory is coming from.
FSMM tells me the leak is in the new function, which I''ve pretty much confirmed by compiling with and without the "//Update the Mouse Label" section in the main loop.

Neither SDLLabel nor SDLGUIItem have an explicit destructor (I''m fuzzy on the whole ''virtual'' thing - the price of being "self-taught"), but there''s not alot to clean up after anyway. They''re never instantiated with anything other than ''new'', it was my impression delete would take care of the cleanup.

This topic is closed to new replies.

Advertisement