FreeMemory() dilema

Everything else that doesn't fall into one of the other PB categories.
PolyVector
Enthusiast
Enthusiast
Posts: 499
Joined: Wed Sep 17, 2003 9:17 pm
Location: Southern California
Contact:

FreeMemory() dilema

Post by PolyVector »

I'm writing a PureBasic JIT compiler (don't get your hopes up, i'm new to this sort of thing)
Well here's my problem... My program(and debugger) will randomly crash at this line:

Code: Select all

Procedure CompileJMP(Address.l,ToAddress.l)
  Protected Buffer.l
  If Address
    Buffer=AllocateMemory(5)
    ;/do stuff
    FreeMemory(Buffer);-<<<<<<<CRASHES HERE>>>>>>>
  EndIf
  ProcedureReturn 5
EndProcedure
If I don't free the buffer everything's fine (except the obvious memory leak)
I know that my Buffer is valid because of debugging... I simply can't figure out why it won't allow me to free it... Is this a PB bug? Are there any ways to avoid this crash? OnError hasn't been able to help me here either :(
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

works with no problems here:

Code: Select all

Procedure CompileJMP(Address.l,ToAddress.l)
  Protected Buffer.l
  If Address
    Buffer=AllocateMemory(5)
    ;/do stuff
    FreeMemory(Buffer);-<<<<<<<CRASHES HERE>>>>>>>
  EndIf
  ProcedureReturn 5
EndProcedure 

Result=CompileJMP(1024,1023)
Debug Result
maybe using threads ?
SPAMINATOR NR.1
PolyVector
Enthusiast
Enthusiast
Posts: 499
Joined: Wed Sep 17, 2003 9:17 pm
Location: Southern California
Contact:

Post by PolyVector »

it only randomly crashes inside a much larger program... I don't know if it's a windows thing or pb...
dige
Addict
Addict
Posts: 1416
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

Maybe that helps:

Code: Select all

Procedure CompileJMP(Address.l,ToAddress.l)
  Static Buffer.l
  If Address
    Buffer = ReAllocateMemory ( Buffer, 5 )
    ;/do stuff
  EndIf
  ProcedureReturn 5
EndProcedure 
cya dige
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Did you tried GlobalAlloc_()/GlobalFree_() ?
If it don't work either it's not a purebasic problem.
Last edited by gnozal on Fri Dec 10, 2004 4:55 pm, edited 1 time in total.
PolyVector
Enthusiast
Enthusiast
Posts: 499
Joined: Wed Sep 17, 2003 9:17 pm
Location: Southern California
Contact:

Post by PolyVector »

Good Idea...
PolyVector
Enthusiast
Enthusiast
Posts: 499
Joined: Wed Sep 17, 2003 9:17 pm
Location: Southern California
Contact:

Post by PolyVector »

Great!
If I replace GlobalAlloc_() and GlobalFree_() it fixes everything :D
Thanks for the advice gnozal 8)
Maybe I should post this in the bugs section?... Although I can't get any small piece of source to demonstrait it...
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

It probably moves the problem in another place, as the Allocate/Free commands are mostly an HeapAlloc/HeapFree wrapper.
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

PolyVector wrote:Great!
If I replace GlobalAlloc_() and GlobalFree_() it fixes everything :D
Thanks for the advice gnozal 8)
Fred may be right.
I had the same problem once and using GlobalAlloc_()/GlobalFree_() fixed it. I realized later that there was a bug in my code (I tried to work with a non existing listicon row if I remember correctly) but I don't know if there was a link between the two problems.
Post Reply