Advertisement

memory leek problem?

Started by June 30, 2000 04:34 AM
4 comments, last by Great Milenko 24 years, 5 months ago
ok I''m getting an assert on a function... it does when I go to return from the function... if I comment out a class(and all places its used) I don''t get an assert... I use the class all over the place yet it only does it in this one function... I''ve looked both the function and the class all over for memory leeks but I haven''t found anyways where.... the class is of the same kind as the class the function belongs to... I''m starting to get mad... anyways heres the function... if you need to see more then let me know... bool CDrawSurface_0::Load(char*filename){ char id[3]; sdword bm_file_size; sdword bm_off; sword bbp; sdword width; sdword height; char*data; ifstream file; file.open(filename,ios::in/ios::binary); file.read(id,2); id[2]=NULL; if(strcmp(id,"BM")) return false; file.read((char*)&bm_file_size,sizeof(bm_file_size)); file.seekg(10,ios::beg); file.read((char*)&bm_off,sizeof(bm_off)); file.seekg(18,ios::beg); file.read((char*)&width,sizeof(width)); file.read((char*)&height,sizeof(height)); file.seekg(28,ios::beg); file.read((char*)&bbp,sizeof(bbp)); data=new char[bm_file_size-bm_off]; file.seekg(bm_off,ios::beg); file.read(data,bm_file_size-bm_off); file.close(); CDrawSurface tsurf; tsurf.Init(m_cdraw,width,height); sdword s=0; sdword ss=width*height*(bbp/8); char*temp=new char[bm_file_size-bm_off]; switch(bbp){ case 16: break; case 24: sdword s2=ss-3; for(sdword s1=0;s1Lock(NULL,&tsurf.m_ddsd,DDLOCK_WAIT / DDLOCK_SURFACEMEMORYPTR,NULL))) return false; memcpy(tsurf.m_ddsd.lpSurface,data,ss); if(Error(tsurf.m_surface->Unlock(NULL))) return false; if(!Blt(tsurf,0,0,m_width,m_height,0,0,width,height)) return false; tsurf.Release(); delete[]data; data=NULL; ClearError(); return true; } if I click ignore it gives me another assert click ignore again and everything works perfectly from there on out... Any Ideas? Great Milenko

Words Of Wisdom:
"Never Stick A Pretzel In Your Butt It Might Break Off In There."


http://www.crosswinds.net/~milenko
http://www.crosswinds.net/~pirotech

The Great Milenko"Don't stick a pretzel up your ass, it might get stuck in there.""Computer Programming is findding the right wrench to hammer in the correct screw."
So what line does the assert appear in the first time?

A good trick. Program defensively. I.e. put in your own assert() (or better yet MFCs ASSERT) calls to check that return values etc. are as they are supposed to before you continue.


Edited by - felonius on June 30, 2000 7:10:52 AM
Jacob Marner, M.Sc.Console Programmer, Deadline Games
Advertisement
The problem is you''re trying to delete memory from a pointer twice !!

You have this:

delete()data;data=temp; 


... just after your switch statment. This is all fine, it deletes the memory like its supposed to. But just towards the end of the function you have:

tsurf.Release();delete()data;     // ERROR !!!! 


... as you can see you''re trying to delete the memory again. Remove this line and see if that helps.

Regards
Michael
Hold on !!

I''ve just had another read and the problem is still the line I pointed out, but it occurs because of this:

memcpy(tsurf.m_ddsd.lpSurface,data,ss); 


... you''re trying to copy data, which is empty, into tsurf.m_ddsd.Surface and not the other way around.

memcpy(void* dest, const void* src, size_t );

Swap tsurf.m_ddsd.lpSurface with data and forget about:

delete()data;     // ERROR !!!! 


... bacuase if you fix memcpy by swapping the variables this should be OK !!

Regards
Michael
Um you might want to check your file open line. Last I heard the \ character doesn't perform the same operation the / character does. And then again it could've been some formatting problems

Woops, it was formatting trouble.

joeG

Edited by - joeG on June 30, 2000 11:44:07 AM
joeG
Ooops I forgot to tell ya what line the assert was on... its the last line
return true;

as far as the memcpy it should be data copied to lpsurface...
ummmm the first delete is inside the switch... gfx data is on the large side so instead of copying the data I free the old data then switch the pointer to the new data
so when it get to memcpy its pointing at the data that temp was pointing at(the new data).

Open file.... nope checked that... everything is fine...
I tried to see if its in a destructor but when I tried to step into it gave me the assert...

I''ve tried changing tsurf to a pointer but that doesn''t fix it eather...

Great Milenko

Words Of Wisdom:
"Never Stick A Pretzel In Your Butt It Might Break Off In There."


http://www.crosswinds.net/~milenko
http://www.crosswinds.net/~pirotech

The Great Milenko"Don't stick a pretzel up your ass, it might get stuck in there.""Computer Programming is findding the right wrench to hammer in the correct screw."

This topic is closed to new replies.

Advertisement