0 votes
by Daniel Lee (630 points)
edited by Daniel Lee

I am trying to make a simple http request using Libcurl version 7.85.0 with Vfx Forth version 5.11 (Win32).  I have set up a callback function for libcurl to send me the http data.  The callback works as expected (it prints the webpage), except it crashes on return.

**Edit:  This code is working on Vfx Forth 5.20 (64-bit) on Linux and Windows.

Here is the relevant code:

Library: libcurl.so.4
Extern: char * "C" curl_version( );
Extern: void * "C" curl_easy_init( );
Extern: long "C" curl_easy_setopt( void * handle, long option, void * param );
Extern: long "C" curl_easy_perform( void * handle );
Extern: char * "C" curl_easy_strerror( long code );
Extern: void "C" curl_easy_cleanup( void * handle );
 
curl_version zcount type
 
: show-error curl_easy_strerror zcount type ;
: check-errors dup 0<> if show-error else drop then ;
 
98 constant CURLOPT_BUFFERSIZE
10001 constant CURLOPT_WRITEDATA
10002 constant CURLOPT_URL
20011 constant CURLOPT_WRITEFUNCTION
 
100 KB Buffer: myBuf
 
variable curl_handle
curl_easy_init curl_handle !
 
curl_handle @ CURLOPT_BUFFERSIZE 100 KB curl_easy_setopt check-errors
curl_handle @ CURLOPT_WRITEDATA myBuf curl_easy_setopt check-errors
curl_handle @ CURLOPT_URL z" http://www.forth.org/faq.html" curl_easy_setopt check-errors
 
: (WriteFunc) \ cstring size byteCount userData -- bytesProcessed
.s
drop \ don't need userData
swap drop \ don't need size (always 1 according to docs)
over over \ stack: address len
type \ or dump
swap drop \ leave only return value on stack
.s
." Made it to the end. "
;
 
4 1 CallBack: &WriteFunc
assign (WriteFunc) to-callback &WriteFunc
 
curl_handle @ CURLOPT_WRITEFUNCTION &WriteFunc curl_easy_setopt check-errors
curl_handle @ curl_easy_perform
 
curl_handle @ curl_easy_cleanup

 

The callback's c function signature is:

size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata);

Any idea what I am doing wrong?

2 Answers

+1 vote
by Stephen Pelc (3.4k points)
selected by Daniel Lee
 
Best answer
As a quick guess, size_t is 64 bits on your 32 bit box. We always end up writing a test file for each O/S to see what they really do and the C compiler really does.
0 votes
by Stephen Pelc (3.4k points)
Your two examples are incomplete when displayed on my Mac. Please provide complete examples - I can't see what happens when the callback executes.

Does VFX give you a crash report?

Stephen
by Daniel Lee (630 points)
I've updated the original post with full source code.  It is working on Vfx Forth version 5.20 (64-bit) on Linux and Windows.  I am relatively new to forth, so I thought that this was a programming error and not necessarily a bug in Vfx Forth.

VFX opened up a "Windows Exception Interception" window with the option to go into the debugger.

Thanks for your help, Stephen.  This is no longer an issue for me since the code is working in  64-bit VFX.
...