🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Memory Allocation Help vc++ 6.0

Started by
6 comments, last by EB 24 years, 5 months ago
I''m having a bit of a problem with this code. Its roundabout because I''ve been trying to see whats going on, and this pretty much well guarantees that there is something I don''t know about. You can assume that Model->Tri was previously allocated. The program crashes on the last line: This is VC++ 6.0 from Visual Studio 98. The NON Bug fixed version. TriType * NewTri; NewTri=(TriType*)malloc(sizeof(TriType)*Model->NumTri); memcpy((void *)NewTri, (const void *)Model->Tri, sizeof(TriType)*Model->NumTriAlloc); free((void *)Model->Tri); Model->Tri=(TriType *)malloc(sizeof(TriType)*Amount); memcpy((void *)Model->Tri, (const void *)NewTri, sizeof(TriType)*Model->NumTriAllocated); free((void *)NewTri); //Crashes on this line Notice that I just allocated the memory for NewTri and now I''m trying to release it. Like I said I know this is pretty silly but if this would work I could fix it properly. Assume all variables are what they appear to be.
Advertisement
Um, when I tried running it, it worked fine.
Something really wierd is going on. This morning its crashing someplace else. I guess I''ll just have to fiddle around with it for awhile.
If you're using C++, why not use the new and delete operators:

// new knows the memory size
TriType * NewTri;
NewTri = new TriType;

// delete automatically knows the size too
delete NewTri;


Or, is NewTri supposed to be pointing to an array? Try this:

// Create an array; NumTri is the number of TriType(s)
TriType * NewTri;
NewTri = new TriType[Model->NumTri];

// Copy the data to the new array.
memcpy((void*)NewTri, (const void*)Model->Tri, sizeof(TriType) * Model->NumTri);

// Delete the old array.
delete[] Model->Tri;

// Create another new array.
Model->Tri = new TriType[Model->NumTri];

// Copy the data back into it.
memcpy((void*)NewTri, (const void*)Model->Tri, sizeof(TriType) * Model->NumTri);

// Delete the new array; size is dynamically calculated
delete[] NewTri;


Either new/delete or malloc/free works fine (unless you're using classes), but I think new and delete make it a LOT easier to read.

- null_pointer


Edited by - null_pointer on 1/10/00 2:58:59 PM
I tried the new and delete operators and it is once again crashing on the last line.
it now reads:

NewTri=new TriangleType[Model->NumTri];
memcpy((void *)NewTri, (const void *)Model->Tri, sizeof(TriangleType)*Model->NumTriAllocated);
delete[] Model->Tri;
Model->Tri= new TriangleType[Amount];
memcpy((void *)Model->Tri, (const void *)NewTri, sizeof(TriangleType)*Model->NumTriAllocated);
delete[] NewTri; //It is once again crashing here

here is TriangleType:
typedef struct{
int Tri[3];
float TriTexVerts[3][2];
TextureType * TriTexturePointer;
}TriangleType;

Could it possible have to do with the way my program is set up to compile. Possibly not set up to allocate as much memory as it is allocating?
I can't see any possible reason for it crashing on that last line.
I've also checked to see what Amount and NumTriAllocated equal and they are:
Amount = 389
NumTriaAllocated=380

Keep in mind that it is crashing on the "delete[] NewTri;" line and NewTri was just allocated 5 lines before that.


Edited by - EB on 1/10/00 5:49:55 PM
Hi !!

I really can''t find something wrong in your code. Perhaps it''s a problem with your compiler settings. Try the release build (because VC uses debug memory allocation routines which assert and crash all the time). Be sure to find the error before proceeding. In my last game project I had something similar. I played around (with compiler settings and code) until it worked fine for months. A few days before release (why not a moth before, no, of course just a few days !!!) the game crashed all the time and it took me a week to play around again until everything was ok again.

So please, be very carefully with silly crashes, because they come back )

Phillip
yea, ive had similar problems
i wrote a queue class optimised for a specific purpose and it used new and delete
the delete crashed and i screwed around with it for about 18 straight hours and then went to bed, and fixed it when i woke up
the moral of the story: i hate annoying crashes, and never work until you almost fall asleep on your mousepad
-PoesRaven
You probably have some other part of your code corrupting memory elsewhere.
--Shannon Schlomer, BLAZE Technologies, Inc.

This topic is closed to new replies.

Advertisement