properly freeing memory?

Everything else that doesn't fall into one of the other PB categories.
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

properly freeing memory?

Post by Dreglor »

Code: Select all

Procedure.s getmd5(id,file.s)
  OpenFile(id,file)
  *buf=AllocateMemory(Lof())
  For i=0 To Lof()
    PokeB(*buf+i,ReadByte())
  Next i
  key.s=MD5Fingerprint(*buf,Lof())
  FreeMemory(*buf)
  CloseFile(id)
  Debug key
  ProcedureReturn key
EndProcedure
i am trying to create a packer program (working on corruption detection) and md5filefingerprint() only checks the full path and file name not the data in the file which i require because unpacking into a diffrent directory changes teh fingerprint and ALL files are considered corrupted by my program (which there not) but with code it checks the file data and i seam to free the data and it doesn't seam to free becasue it crashes in middle of packing (right on allocate memory()) which makes me think that i not properly freeing the memory and i am running out of memory and i am not using big files i am testing it on my work folder filled with pb files and some images and sound nothing huge ( >1meg )

so anyone can help me with this?
~Dreglor
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Re: properly freeing memory?

Post by freedimension »

Dreglor wrote:

Code: Select all

Procedure.s getmd5(id,file.s)
  OpenFile(id,file)
  *buf=AllocateMemory(Lof())
  For i=0 To Lof()
    PokeB(*buf+i,ReadByte())
  Next i
  key.s=MD5Fingerprint(*buf,Lof())
  FreeMemory(*buf)
  CloseFile(id)
  Debug key
  ProcedureReturn key
EndProcedure
You allocate Lof() Bytes of memory, but assign Lof()+1 Bytes a value (For i=0 to Lof() ).
freak
PureBasic Team
PureBasic Team
Posts: 5947
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

MD5FileFingerPrint() ONLY analyzes the data. no filename or directory.

You must have another bug in your program that tells you thate the files
are corrupted.
MD5FileFingerPrint() works perfectly.

Timo
quidquid Latine dictum sit altum videtur
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

Post by Dreglor »

hmm it doesn't seam to sample the whole file or somthing because a simple test shows that there values for the same file
~Dreglor
freak
PureBasic Team
PureBasic Team
Posts: 5947
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Dunno what your 'simple test' is, but it works perfectly here.

Timo
quidquid Latine dictum sit altum videtur
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

Post by Dreglor »

here it is just replace "file"

Code: Select all

Debug MD5FileFingerprint(file)
OpenFile(0,file)
*buffer=AllocateMemory(Lof()+1)
For i=0 To Lof()
  PokeB(*buffer,ReadByte())
next i
Debug MD5Fingerprint(*buffer,Lof())
~Dreglor
freak
PureBasic Team
PureBasic Team
Posts: 5947
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Well, you are making several mistakes there:

First, you are always poking to the *Buffer position. You never increase
that pointer. So you are basically writing the while file data into the
first byte of your buffer, with each new byte overwriting the old one.
The rest of the buffer is empty. No wonder the fingerprints don't match.

Second, as pointed out by freedimension allready, you are processing
one byte more than the real filesize. This has no real effect here, as
you specify the right size (lof()) at the MD5FingerPrint() command,
but it is still no good thing to do.

Here's a working code:

Code: Select all

Debug MD5FileFingerprint(file)
OpenFile(0,file)
*buffer=AllocateMemory(Lof())
For i=0 To Lof()-1
  PokeB(*buffer+i,ReadByte())
Next i
Debug MD5Fingerprint(*buffer,Lof()) 
voila... the fingerprints match. So it's only file data, nothing else.

Timo
quidquid Latine dictum sit altum videtur
Dreglor
Enthusiast
Enthusiast
Posts: 759
Joined: Sat Aug 02, 2003 11:22 pm
Location: OR, USA

Post by Dreglor »

it was a quick whip up test i didn't have time to look into the code deeply
thank :D
~Dreglor
Post Reply