Page 1 of 1

properly freeing memory?

Posted: Sat Mar 27, 2004 7:21 am
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?

Re: properly freeing memory?

Posted: Sat Mar 27, 2004 12:12 pm
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() ).

Posted: Sat Mar 27, 2004 2:12 pm
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

Posted: Sat Mar 27, 2004 3:48 pm
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

Posted: Sun Mar 28, 2004 11:05 am
by freak
Dunno what your 'simple test' is, but it works perfectly here.

Timo

Posted: Sun Mar 28, 2004 7:30 pm
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())

Posted: Sun Mar 28, 2004 8:06 pm
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

Posted: Sun Mar 28, 2004 9:22 pm
by Dreglor
it was a quick whip up test i didn't have time to look into the code deeply
thank :D