Skip to main content
GameDev.net gamedev.net
🔒 Locked

Memory allocation problems

Started by Absolution Apr 15, 2001 at 2:34 PM 9 replies 350+ views
Original Post
Absolution
Absolution
I guess I don''t understand memory allocation in C++ as well as I though I did. Here is my problem. I am implementing a tile-based game in DirectX. I have a global map variable consisting of an array of node classes: Cnode *map; In my initialization I allocate memory like this: ... Allocate Surfaces for bitmaps ... map=new Cnode[mapx*mapy]; This was working until I started trying to allocate more surfaces for bitmaps. My program would no longer get past initialization unless I decreased the size of my map. This seemed strange to me since the game has nothing in it right now and the Cnode class consists of 1 variable and a constructor at this point. Also my computer has 256mb of ram so I know I haven''t used it up. Anyway, if I instead declare the map like this: Cnode map[SOME_CONSTANT] I have no problems. I should know this, but why is it giving me trouble when I try and allocate it dynamically during my initialization, but if I declare it with a constant globally everything is fine? Abs
LilBudyWizer
LilBudyWizer
Could you explain what you mean by "no longer get past initialization"?
Keys to success: Ability, ambition and opportunity.
Mikey
Mikey
Could it be that there''s a bug in your bitmap surface allocation causing it to trash memory somewhere? What happens if you do your CNode allocation before this?

Mike.
Absolution
Absolution
You are right. If I allocate the map before I read in my pcx images everything works fine. I don''t see how they are trashing memory though. I have a standard pcx image type defined :

typedef struct pcx_picture_typ {
pcx_header header;
UCHAR *buffer;
} pcx_picture, *pcx_picture_ptr;

As I load in the picture I do this in a separate function which I call once during initilization for each image I want loaded(which is before my PeekMessage loop).

-Read in header

-Allocate buffer memory (8bit):
image->buffer = new UCHAR[(image->header.width+1)*(image->header.height+1)];

- Read in RLE data
- Read in palette if necessary
- Create a direct draw surface of the same size
Mikey
Mikey
Difficult to say where it could be without seeing the code. But I''d say it''s most likely that you''re writing past the end of the allocated memory during the run length decoding. Also are you allocating memory for the palette if there is one?

Mike.

Absolution
Absolution
Well, Mikey you were correct, the problem was in my decoding. This was the line that caused me all of this trouble:

while(count<=((image->header.width+1)*(image->header.height+1)))
.. rle and increase count as needed

That should have been a < and not a <= This was causing me to go one UCHAR past the end of my allocated buffer. I guess in most of the cases this memory was not important and I had no problems, but in the last one I added this was writing over something protected. :/

Aren''t those the most difficult bugs to track down - those pointer bugs where it will still work most of the time. Anyway, thanks for the help. Between you and the error log I created while looking for this bug I tracked it down. Hopefully this log will make my job easier in the future.

Abs

p.s. I have another memory allocation question. I read somewhere that if you don''t delete your dynamically allocated memory (i.e. using the new keyword) at the end of your program that the memory is lost until you restart your computer (atleast under Win98). I thought that once your program terminates this memory will be returned to the free heap? In either case I always match new with delete, but I was just wondering for educational purposes.
LilBudyWizer
LilBudyWizer
I don''t believe so. Each process has its own virtual address space. When your process terminates that address space goes away along with all memory allocated by the process.
Keys to success: Ability, ambition and opportunity.
Nekosion
Nekosion
The memory returns to the heap properly in WinNT but in Win9x it seems to have problems with it _sometimes_.

At least that''s what I''ve found.

Regards,
Nekosion

Resist Windows XP''s Invasive Production Activation Technology!
Regards,Nekosion
Zipster
Zipster
quote:
p.s. I have another memory allocation question. I read somewhere that if you don''t delete your dynamically allocated memory (i.e. using the new keyword) at the end of your program that the memory is lost until you restart your computer (atleast under Win98). I thought that once your program terminates this memory will be returned to the free heap? In either case I always match new with delete, but I was just wondering for educational purposes.


It''s true. To test it out, create a program that does something like X = new char[1000]; and don''t delete it at the end. Run this program several times over, and you''ll notice that your system resources go down, and down, and down...

Then I hear some people talking about how Windows has some protection against this kind of thing and unallocates any allocated memory at the end of the program, but that''s whack, or maybe only partly true.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.