Mem address error, whats wrong with this:

BlitzMax Forums/BlitzMax Beginners Area/Mem address error, whats wrong with this:

D4NM4N(Posted 2006) [#1]
I cannot work out why the line:
If hbuf GlobalFree( hbuf )
sometimes gives me a 'The instruction yadiyada referenced memory at 0x00000000'The memory could not be read

If i take the line out its fine ?! but i guess will leave big memory holes.

Any ideas?

Extern "Win32"
 Const GMEM_FIXED% = 0
 Const CF_TEXT%=1

 Function OpenClipboard(hwnd%)
 Function CloseClipboard()
 Function EmptyClipboard()
 Function SetClipboardData(format%, hMem:Byte Ptr)
 Function GetClipboardData:Byte Ptr( format )
 Function GlobalAlloc:Byte Ptr( uflags, bytes )
 Function GlobalFree( buf:Byte Ptr )

End Extern  '-----------------------------------

Function GlobalAllocString:Byte Ptr( txt$ )
	
	Local p:Byte Ptr= GlobalAlloc( GMEM_FIXED , Len(txt)+1 )
	
	For Local i=0 Until Len(txt)
		p[i]=txt[i]
	Next
	
	p[Len(txt)+1]=0
	
	Return p
	
EndFunction 

Function WriteClipboardText(txt$)
	'write to the clipboard
	
	Local hbuf:Byte Ptr

	If OpenClipboard(0)
		EmptyClipboard
		hbuf = GlobalAllocString( txt )
		SetClipboardData CF_TEXT, hbuf
		
		Print Int(hbuf)
		CloseClipboard()
		
		If hbuf GlobalFree( hbuf )
		
	Else
		RuntimeError "Failed to open Clipboard"
	EndIf

	
End Function