Memory allocation (randomly?) fails

Just starting out? Need help? Post your questions and find answers here.
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Memory allocation (randomly?) fails

Post by firace »

I'm struggling to understand why the *Y=AllocateMemory statement in the below example sometimes fails (Invalid memory access). It crashes about 50% of the time for me.

Code: Select all


Procedure Test(A, FindString$)
  
  Buffer_Len=StringByteLength(FindString$)
  *X=AllocateMemory(Buffer_Len)
  PokeS(*X,FindString$,-1)   ;;; this line apparently interferes? <-
  
  *Y=AllocateMemory(Buffer_Len)    ;;;;;;;;;;;;    why sometimes fails?
  
EndProcedure

Test(123,"info")

User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Memory allocation (randomly?) fails

Post by Shield »

You forgot to allocate space for the null terminator (two bytes in unicode).
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
firace
Addict
Addict
Posts: 903
Joined: Wed Nov 09, 2011 8:58 am

Re: Memory allocation (randomly?) fails

Post by firace »

Shield wrote:You forgot to allocate space for the null terminator (two bytes in unicode).
That was it!! Thank you very much.
Post Reply