Jump to content
  • Advertisement

Archived

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

Metal Typhoon

why am i getting memory leaks when i use this ?

This topic is 5674 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


  
ifstream File (Filename,ios::binary);

if (!File)
  return false;
	
m_Data = new unsigned char [m_Width * m_Height];
for (unsigned int Read = 0; Read < m_Height * m_Width; Read++)
  File.read (reinterpret_cast <char *> (m_Data),m_Width * m_Height);	

File.close ();
  
instead of this
  

FILE	*File;
File = fopen (Filename,"rb");

if (!File)
 return false;
	
m_Data = new unsigned char [m_Width * m_Height];

fread (m_Data,1,m_Width*m_Height,File);
fclose (File);
  
all i''m reading is from a raw file. I dont get it thought can someone explain me ?

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Metal Typhoon
if (!File)
return false;



(NOTE: The following statements assume that ifstream doesn''t overload operator! in a weird way)

Also, that has no meaning at all. File is a local instance of ifstream, so that statement will always evaluate to true. Use a function that tests the stream status like fail() or bad().

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

Share this post


Link to post
Share on other sites
quote:
Original post by ZealousElixir
[quote]Original post by Metal Typhoon
if (!File)
return false;



(NOTE: The following statements assume that ifstream doesn''t overload operator! in a weird way)

Also, that has no meaning at all. File is a local instance of ifstream, so that statement will always evaluate to true. Use a function that tests the stream status like fail() or bad().

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links


ok i''ll use .fail () now.. if i dont delete [] m_data which is a class member variable it wont give me the memory leak still.. but the other verion above will do it.. i guess it''s something about File.read ???

Share this post


Link to post
Share on other sites
quote:
Original post by ZealousElixir
File is a local instance of ifstream, so that statement will always evaluate to true.
Huh? I don''t follow this reasoning.

Anyway, ifstream''s operator! returns ifstream::fail(), so what MT is doing is alright... Although I don''t understand why he reads m_Height * m_Width bytes so many times. Okay, it''s obviously a bug but it shouldn''t lead to memory leaks either.

Share this post


Link to post
Share on other sites
quote:
Original post by civguy
[quote]Original post by ZealousElixir
File is a local instance of ifstream, so that statement will always evaluate to true.
Huh? I don''t follow this reasoning.

Anyway, ifstream''s operator! returns ifstream::fail(), so what MT is doing is alright... Although I don''t understand why he reads m_Height * m_Width bytes so many times. Okay, it''s obviously a bug but it shouldn''t lead to memory leaks either.

if a map is 1028 by 1028 then i need to read all of it.. that''s why i have m_Width*m_Height got it ? but i dont know why i got a memory leak i really need help i wnt to use c++ not c

Share this post


Link to post
Share on other sites
I didn''t look at your code, but the C++ file stream classes are known to produce false memory leaks because they allocate static memory either pre-main() or free it post main() -- I forget which it is.

I think I remember just adding the follow include:
#include <fstream> // or was it fstream.h?
produced false memory leaks if your memory leak checker was set up in a certain way.

Share this post


Link to post
Share on other sites
quote:
Original post by Metal Typhoon
if a map is 1028 by 1028 then i need to read all of it.. that''s why i have m_Width*m_Height got it ?
No, I don''t quite get it:

  
for (unsigned int Read = 0; Read < m_Height * m_Width; Read++)
File.read (reinterpret_cast <char *> (m_Data),m_Width * m_Height);

You''re reading m_Height * m_Width bytes m_Height * m_Width times. If your map is 1024 * 1024, it''ll try to read a terabyte. I doubt that''s your intent.

Share this post


Link to post
Share on other sites
quote:
Original post by civguy
[quote]Original post by Metal Typhoon
if a map is 1028 by 1028 then i need to read all of it.. that's why i have m_Width*m_Height got it ?
No, I don't quite get it:

      
for (unsigned int Read = 0; Read < m_Height * m_Width; Read++)
File.read (reinterpret_cast <char *> (m_Data),m_Width * m_Height);

You're reading m_Height * m_Width bytes m_Height * m_Width times. If your map is 1024 * 1024, it'll try to read a terabyte. I doubt that's your intent.

Holy crap i see what you mean so i think i should do this...

for (unsigned int Read = 0; Read < m_Height ; Read++)
File.read (reinterpret_cast (m_Data),m_Width);

After i changed it it still gave me memory leaks.. i dont know what's wrong with it..


{55} normal block at 0x07961A00, 512 bytes long.
Data: < > 20 00 20 00 20 00 20 00 20 00 20 00 20 00 20 00
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {43} normal block at 0x07961D80, 16 bytes long.
Data: < F > B4 F4 46 00 01 00 00 00 00 00 00 00 00 00 00 00
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {42} normal block at 0x07960030, 33 bytes long.
Data: < C > 00 43 00 CD CD CD CD CD CD CD CD CD CD CD CD CD
c:\program files\microsoft visual studio\vc98\include\crtdbg.h(552) : {41} normal block at 0x07961DC0, 40 bytes long.
Data: < F > 08 F4 46 00 02 00 00 00 00 00 00 00 00 00 00 00
Object dump complete.


[edited by - Metal Typhoon on February 2, 2003 1:02:08 AM]

Share this post


Link to post
Share on other sites
Well, what Z01 said could well be correct. You don''t have any memory leaks but your mem-leak reporter just reports them incorrectly.

Also

  
for (unsigned int Read = 0; Read < m_Height ; Read++)
File.read (reinterpret_cast<char*>(m_Data),m_Width);

Will read m_Width worth of bytes at the beginning of m_Data over and over again. The whole m_Data array won''t get filled. I suggest you do like you did in the C version:

  
File.read (reinterpret_cast <char *> (m_Data),m_Width * m_Height);

That is, scrap the for loop.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!