Page 1 of 1

4.20 AllocateMemory and invalid memory access

Posted: Tue May 27, 2008 10:22 pm
by Marc
I'm working on a IDE project using Scintilla
The following code report very often a Invalid Memory Access on the AllocateMemory function.
I also noticed:
- Same with 4.10
- No problem without the debugger

Any suggestion ?

Code: Select all

ProcedureDLL SciLexer_LoadFile(SciGadget.l, MyFile.s) ; Load the file MyFile

  Protected MyHandle.l
  Protected MyLen.l
  Protected *Mem.b

  Debug "Load " + MyFile

  SciLexer_ActionStatus = 1
  If FileSize(MyFile) > -1
    MyHandle = ReadFile(#PB_Any, MyFile)
    If MyHandle
      MyLen = Lof(MyHandle)
      Debug MyLen
      If MyLen > 0
        *Mem = AllocateMemory(MyLen + 1) ;Error at this line
        If *Mem
          ReadData(MyHandle, *Mem, MyLen)
          ScintillaSendMessage(SciGadget, #SCI_SETTEXT, 0,*Mem)
          FreeMemory(*Mem)         
        EndIf
      Else
        ScintillaSendMessage(SciGadget, #SCI_CLEARALL, 0, 0)
      EndIf
      CloseFile(MyHandle)      
    EndIf
  EndIf
  SciLexer_ActionStatus = 2

EndProcedure

Many thanks

Posted: Tue May 27, 2008 10:34 pm
by freak
AllocateMemory() is not the problem (its a direct API call).
You have a heap corruption somewhere in your code.

Stick the _validate macro found in this post in your code in various places to narrow down where the error is:
http://www.purebasic.fr/english/viewtop ... 460#203460