Archived

This topic is now archived and is closed to further replies.

Golden_Phoenix

bug crushed, but what went wrong?

Recommended Posts

I''m sorry this one is long. At the bottom is the short question? I recently had annoying memory crushing bug that drove me insane (more so than usual) for a few days. I think I got it taken care of... and I think I know what went wrong, but something doesnt sit well about my theory, so I thought Id post it- basically, I was trying to construct a pool based preallocation system... I wanted to avoid new and deleting every frame for particles in a particle engine... but I didnt want to use an array and do stupid searches thru it to find particles I could use. (IE dead or otherwise unused particles). The system I came up with worked off 3 basic types of pools, a simple array of particles - (Node *Array) a linked list of any unused particles of the array (Node *Free) and one or more ''in use'' lists. A manager class contained the first two lists. the second two lists both are offsets in the simple array, IE they are just linked lists with preallocated nodes. Whenever a new particle was needed by an emitter, it would just ask the manager for a node from the free list. If one was available... it was pulled out of the free list and placed in the emitters ''used'' list. I thought it was a nice way to use a pre allocated array but not do searches based on some alive or dead flag. Yeah right... anyway it bombed miserably... the reason I think so... is becuz I made the manager and the node generic... I tried to make them not care whether they held particles or bullets or monkeys... I just derived the particle class from em... basically my layoput looked like this class NODE { NODE *Next, *Prev; ... }; class PARTICLE : public NODE { // particle related stuff }; class NODEMANAGER { NODE *GlobalArray; NODE *FreeList; NODEMANAGER(NODE *NewGlobalArray) { GlobalArray = NewGlobalArray; } }; NODEMANAGER Bob((NODE*)new PARTICLE[5000]); all appeared fine for a few frames (once I took care of other housekeeping bugs) and often was fine in actually stepping thru the code except memory was getting mangled. the next and prev pointers specifically were getting odd values (I swear it once had a value of 0xbaadf00d) usually 0x4120000 though... over and over and over again. Nothing I could see was accessing this memory directly and unfortunatly the shear number of particles prevented very careful stepping thru... and wastching memory is... well a pain in the azz in vc... it doesnt work well if at all and runs very very slow. Eventually I decided it was becuz I was putting a derived class array in a base class pointer, but in all other aspects moving thru it like it was the base class... since the base class was much less memory, particle member data was getting written over particles next to it in memory. basically I was writing data for a long int pointer to a char pointer... the mem size difference caused it to go out of bounds. Something doesnt sound right about this, maybe Im missing a duh, but I stick child classes in base class pointers all the time, its the basis of virtual classes... is it just the fact I had no virtual keyword, or is it becuz the manager class was working like it had the base class and not the child? I appear to have solved it by using templates (my theory being that now the manager class is working with the child as it expects). But can someone confirm this? SHORT QUESTION! class BASE {...} class DERIVED : BASE {... and ... } BASE *ptr = DERIVED[5000]; Is this action gonna end in pain? If not, what mighta happened in my case?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yeah, that last line won''t make you happy. Basically, you can move up and down the tree with pointers, but not actual instances.

This is a weird thing about C++: it''s trying to be all high level and OOP, but the language is in fact very close to the metal. This means that there''s going to be a bunch of oddities and leaks in the abstraction it presents.

Share this post


Link to post
Share on other sites