Archived

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

Difference between memory allocation in Debug/Release

This topic is 5113 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I used this code: (simplified)
VER2D *fv;
VER2D *sv;
VER2D *tv;

// fv part

// fv blabla

fv=(VER2D *)malloc(sizeof(VER2D)*nf);
if(!fv){MsgBoxE("Unable to allocate VERT2D *FV!");};
// fv blabla


// sv part

// sv blabla

sv=(VER2D *)malloc(sizeof(VER2D)*ns);
if(!sv){MsgBoxE("Unable to allocate VERT2D *SV!");};
// sv blabla


// tv part

// tv blabla

tv=(VER2D *)malloc(sizeof(VER2D)*nt);
if(!tv){MsgBoxE("Unable to allocate VERT2D *TV!");};
// tv blabla

This is a problem I only get in Release mode, but not in Debug mode: After allocating sv, (!sv) seems to be true so I get the error message. I tried switching the fv and sv part and it resulted again in an error at the second part (so this time the fv part). I also tried allocating the variables with new, that gave the same result. Is there something I did wrong or is there another way to do this? Thanks in advance!

Share this post


Link to post
Share on other sites
Check to make sure that ns isn''t some bizarre huge value at the time malloc is called.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
It sounds to me like somehow in the way you set the value ns you are not intializing a value. In debug mode all uninitialized values are set to 0, in release mode, no such luck.

I recently ran into a problem where I forgot to set the value of a bool flag variable to false, no problems in debug mode because it set it to 0, but when run in release mode the value would be true - not what my program was ready to see.

Just ensure every variable that you use to calculate that has been set.

If you tried to alloc zero memory it makes sense to me that it would fail.

[edited by - pjcast on December 15, 2003 2:42:42 PM]

Share this post


Link to post
Share on other sites
I always set variables to 0/NULL/false when I declare them so that cannot be the problem.
It''s like this:

ifstream front(f,ios::binary);
for(x=0;x<OBJW;x++){
for(y=0;y<OBJH;y++){
front.seekg(x*OBJH+y,ios::beg);
front>>c;
if(c==255) nf++;
if(c==127) nif++;
};
};
fv=(VER2D *)malloc(sizeof(VER2D)*nf);
if(!fv){MsgBoxE("Unable to allocate VERT2D *FV!");};
for(x=0;x<OBJW;x++){
for(y=0;y<OBJH;y++){
front.seekg(x*OBJH+y,ios::beg);
front>>c;
if(c==255){
fv[cn].x=(float)x-((float)OBJW/2.0f);
fv[cn].y=(float)y-((float)OBJH/2.0f);
cn++;
};
};
};
front.close();
wsprintf(txt,"%u + %u = %u",nf,nif,nf+nif);
MsgBoxI(txt);
cn=0;

And then exactly the same routine but then:
front=side, f=s, nf=ns, nif=nis and fv=sv
And then the same for top.

All set to the same value.
As you see I get a message with the value of nf, nif and nf+nif.
The info I get is exactly the same for front as side as top, which is what it should be. But when tried to allocate sv, sv seemed to be NULL. When I placed the side code first, followed by front, I got the error when allocating fv.

Share this post


Link to post
Share on other sites
I cannot see any problems with your posted code. May I suggest you run your release build through the debugger, it is pretty easy to do that with MSVC++. You will have to change the active configuration, but to be able to step through c source, you will have to change the project settings to produce debug info, and no optimizations.

Though, putting in the debug info will probably cause it to work like it was the debug build. You could omit the debug info and just want be able to step through the C code, will have to look at the assembly. I''m pretty sure that''s the case anyway.

Share this post


Link to post
Share on other sites
Ok, I decided to post the whole function now. I now also get Access Violation in debug mode on the function's return point, no idea where that came from.
My function: (as you can see it doesn't use the object pointer yet)

void LoadObject(OBJ *o, char *f, char *s, char *t, unsigned int r){ // object, filename (front), filename (side), resolution (skip r points every time)

if(r==0){r=1;};
char txt[]="";
unsigned char c=0;
unsigned int nf=0; // border points

unsigned int nif=0; // inside points

unsigned int ns=0; // border points

unsigned int nis=0; // inside points

unsigned int nt=0; // border points

unsigned int nit=0; // inside points

unsigned int x=0;
unsigned int y=0;
unsigned int cn=0;
ifstream front(f,ios::binary);
for(x=0;x<OBJW;x++){
for(y=0;y<OBJH;y++){
front.seekg(x*OBJH+y,ios::beg);
front>>c;
if(c==255) nf++;
if(c==127) nif++;
};
};
fvn=nf;
fv=(VER2D *)malloc(sizeof(VER2D)*nf);
if(!fv){MsgBoxE("
Unable to allocate VERT2D *FV!");};
for(x=0;x<OBJW;x++){
for(y=0;y<OBJH;y++){
front.seekg(x*OBJH+y,ios::beg);
front>>c;
if(c==255){
fv[cn].x=(float)x-((float)OBJW/2.0f);
fv[cn].y=(float)y-((float)OBJH/2.0f);
cn++;
};
};
};
front.close();
wsprintf(txt,"
%u + %u = %u",nf,nif,nf+nif);
MsgBoxI(txt);
cn=0;
ifstream side(s,ios::binary);
for(x=0;x<OBJW;x++){
for(y=0;y<OBJH;y++){
side.seekg(x*OBJH+y,ios::beg);
side>>c;
if(c==255) ns++;
if(c==127) nis++;
};
};
svn=ns;
sv=(VER2D *)malloc(sizeof(VER2D)*ns);
if(!sv){MsgBoxE("
Unable to allocate VERT2D *SV!");};
for(x=0;x<OBJW;x++){
for(y=0;y<OBJH;y++){
side.seekg(x*OBJH+y,ios::beg);
side>>c;
if(c==255){
sv[cn].x=(float)x-((float)OBJH/2.0f);
sv[cn].y=(float)y-((float)OBJH/2.0f);
cn++;
};
};
};
side.close();
wsprintf(txt,"
%u + %u = %u",ns,nis,ns+nis);
MsgBoxI(txt);
cn=0;
ifstream top(t,ios::binary);
for(x=0;x<OBJW;x++){
for(y=0;y<OBJH;y++){
top.seekg(x*OBJH+y,ios::beg);
top>>c;
if(c==255) nt++;
if(c==127) nit++;
};
};
tvn=nt;
tv=(VER2D *)malloc(sizeof(VER2D)*nt);
if(!tv){MsgBoxE("
Unable to allocate VER2D *TV!");};
for(x=0;x<OBJW;x++){
for(y=0;y<OBJH;y++){
top.seekg(x*OBJH+y,ios::beg);
top>>c;
if(c==255){
tv[cn].x=(float)x-((float)OBJW/2.0f);
tv[cn].y=(float)y-((float)OBJH/2.0f);
cn++;
};
};
};
top.close();
wsprintf(txt,"
%u + %u = %u",nt,nit,nt+nit);
MsgBoxI(txt);
unsigned int nv=2*(nf+nif)+2*(ns+nis)+2*(nt+nit);
wsprintf(txt,"
Total vertices: %u",nv);
MsgBoxI(txt);

return;
};

That's it, hope anyone sees some stupid mistake or something, or knows some way to get rid of those annoying errors.

[edited by - Living Monstrosity on December 15, 2003 3:52:02 PM]

Share this post


Link to post
Share on other sites
I believe your access violation on return is caused by:

char txt[]="";

And then you put information into txt string and it was not big enough to hold it, which caused an access violation on the call stack during clean up. Either try using std::string or declare a larger array, say char txt[256] or so.

Still unsure as to why your pointers null though, maybe someone else can help there

Share this post


Link to post
Share on other sites