Skip to main content
gamedev.net
🔒 Locked

odd structure problem

Started by CProgrammer 10 replies 400+ views
Original Post
CProgrammer
CProgrammer
I have a structure that is defined as folows:
typedef struct node
{
    int *polys;
    int numpolys;
    
    int ID;
    int ParentID;
    Hardbox hbox;
    int BranchID[4];
    DWORD Type;
}NODE;
 
This is my node structure used by the Quad-Tree. Now when I add a pointer to the structure the application crashes, no matter whether i use this pointer or not. Just by adding it:
typedef struct node
{
    int *polys;
    int *objs; //causes Crash
    int numpolys;
    
    int ID;
    int ParentID;
    Hardbox hbox;
    int BranchID[4];
    DWORD Type;
}NODE;
 
Any ideas?
Like2Byte
Like2Byte
That''s a little obscure. Could you possibly show us a snippit of its implimentation?

I''d love to tear this one apart but I just can''t extract anything from this writeup.
Advice, eh? Well, besides working on your swing...you know, besides that...I'd have to think.
CProgrammer
CProgrammer
quote:
Original post by Like2Byte
That's a little obscure. Could you possibly show us a snippit of its implimentation?

I'd love to tear this one apart but I just can't extract anything from this writeup.


Sure heres the locations using an array of nodes from the NODE structure:

NODE *nodes;


//Allocate Node Memory
nodes = (NODE*)malloc((numnodes+numleaves)*sizeof(NODE));


and the creation of the tree:


  
void QuadTree::CreateNode(Hardbox hbox, int Parent, int ID)
{
//CreateNode loops itself creating the desired

//nodes in our tree. Each loop stops at a leaf.

//The first call to CreateNode should have the

//world bounds for hbox and Parent:0 ID:0


NODE *node = &nodes[ID];

//Check to see whether Bounding Box

//has desired leaf width

if(hbox.xlength <= lengthleaf)
{
node->Type = TYPE_LEAF;
}
else
{
node->Type = TYPE_NODE;
}

node->ID = ID;
node->ParentID = Parent;
node->hbox = hbox;

if(node->Type == TYPE_LEAF)
{
int i;
int sumnumpolys = 0;
for(i = 0; i < numpolys; i++)
{
if(node->hbox.hit(Polys[i].center.x,
Polys[i].center.y,
Polys[i].center.z))
{
BackBuffer[sumnumpolys] = i;
sumnumpolys++;
}
}

//Allocate memory for nodes polygons

node->polys = (int*)malloc(sumnumpolys*sizeof(int));
node->numpolys = sumnumpolys;
for(i = 0; i < sumnumpolys; i++)
{
node->polys[i] = BackBuffer[i];
}

return;
}
else
{
numid++;
node->BranchID[0] = numid;
//Upper left corner

Hardbox bounds;
bounds.Set(hbox.x, hbox.y, hbox.z+(hbox.zlength/2),
hbox.xlength/2, hbox.ylength, hbox.zlength/2, FALSE);
CreateNode(bounds, ID, numid);

numid++;
node->BranchID[1] = numid;
//Upper right corner

bounds.Set(hbox.x+(hbox.xlength/2), hbox.y, hbox.z+(hbox.zlength/2),
hbox.xlength/2, hbox.ylength, hbox.zlength/2, FALSE);
CreateNode(bounds, ID, numid);

numid++;
node->BranchID[2] = numid;
//Lower left corner

bounds.Set(hbox.x, hbox.y, hbox.z,
hbox.xlength/2, hbox.ylength, hbox.zlength/2, FALSE);
CreateNode(bounds, ID, numid);

numid++;
node->BranchID[3] = numid;
//Lower right corner

bounds.Set(hbox.x+(hbox.xlength/2), hbox.y, hbox.z,
hbox.xlength/2, hbox.ylength, hbox.zlength/2, FALSE);
CreateNode(bounds, ID, numid);
}

return;
}


The app crashes inside the CreateNode loop. When i take out the
int *objs; pointer(which is never used nor initialized)it runs.
Adding the pointer causesthe crash.

This is really a bizarre error. I hope I can find help, I cant seem to find the problem.

[edited by - Stoffel on October 25, 2002 12:29:50 PM]
intellibug
intellibug
try rebuild-all, if the structure is in some header file. maybe the compiler thinks that in CreateNode the structure is 4bytes smaller than before adding the int* ...
Basiror
Basiror
i usually set all pointers to NULL before i allocate memory to them
http://www.8ung.at/basiror/theironcross.html
CProgrammer
CProgrammer
quote:
Original post by intellibug
try rebuild-all, if the structure is in some header file. maybe the compiler thinks that in CreateNode the structure is 4bytes smaller than before adding the int* ...


I did the rebuild all, but you mentioning that the compiler may allocate 4bytes too less per NODE led me to try a little test. I just allocate mem as follows

nodes = (NODE*)malloc((numnodes+numleaves)*(sizeof(NODE)+sizeof(int)));


After this ajustement the CreateNode function works(there must be a memory allocation error but i still dont know what), but the app crashes in my ''Sorting'' function.

quote:

i usually set all pointers to NULL before i allocate memory to them.



I usually do this too. I forget it in this case but setting it to NULL didnt change anything.

Thanks for your hints. I think i''m getting closer to the problem. Any more ideas would still be appreciated.
Thanks CProgrammer

CWizard
CWizard
You said it crashed in the loop. Which loop? Or, did you mean the function? If so, could you nail it down better?

Do a printf("%i", sizeof(NODE)); (or similar) to see how big it is, and count that it is correct.

You have a class and/or structure, Hardbox, inside this one. Could it be about it?

The reason it crashes when you added something to the struct, might be partly due to padding. VC most likly pad a structure like this to be 16 byte even (?). Therefor, the problem might be there before you added the pointer, you just didn''t noticed it because you had some margin (the padding) that saved you.

Then I would carefully check your initialization code. Make the structure so it will crash the program, then remove part-by-part of the code untill it do not crash.

---------------------------------------------
"VB is a disease"
Stoffel
Stoffel
See how many iterations you get through before crashing. Might you be running out of stack space?

PS: Should hardbox be passed by value everywhere?
stodge
stodge
Have you used the debugger to find the line that it crashes at? And what''s the error?
---------------------http://www.stodge.net
CWizard
CWizard
Stepping in the debugger isn''t trusty. I have had several access violation cases, where running the program (debug version) causes a crash, and when stepping in the debugger survives and doesn''t alert in any way. This is probably when there is a stack issue, as VC++ might be controlling the stack or something.

---------------------------------------------
"VB is a disease"
stodge
stodge
I disagree, but c''est la vie.

quote:
Original post by CWizard
Stepping in the debugger isn''t trusty. I have had several access violation cases, where running the program (debug version) causes a crash, and when stepping in the debugger survives and doesn''t alert in any way. This is probably when there is a stack issue, as VC++ might be controlling the stack or something.

---------------------------------------------
"VB is a disease"


---------------------http://www.stodge.net
CProgrammer
CProgrammer
UNBELIEVABLE

I did some of the suggested tests.
I let the program print out the size of NODE before and after the change.
Before: 64 bytes
after: 68 bytes

This seems to be OK.

But i am sure its a problem of malloc not getting enough memory.
The following test shows this:
i allocate nodes as follows:

nodes = (NODE*)malloc((numnodes+numleaves)*(sizeof(NODE)+sizeof(int*)+sizeof(int*)));

Voila now it works!
But why!!

quote:

You said it crashed in the loop. Which loop?



I mean the CreateNode function which calls itself creating multiple loops.

-CProgrammer

quote:

PS: Should hardbox be passed by value everywhere?



I'm not sure what you mean but Hardbox is a structure that every node needs, which means hardbox data is filled for each node.

[edited by - CProgrammer on October 25, 2002 11:10:44 PM]

[edited by - CProgrammer on October 25, 2002 11:14:07 PM]

Topic Locked

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

Sign in to reply to this topic.