8

I use libcurl for FTP works and it works fine but if left idle for some time it just crashes. Here is the backtrace which despite reading it for some time I cannot make sense of what is wrong. The trace does not show where in my functions crash originates and so am left orphan here by debugger. I use threads if that add value Compiler is GCC 4.7 on Linux

0  0x00007fff8e09b524  addbyter  /home/stefano/Desktop/myproject/curl-7.33.0/lib/mprintf.c  914
1  0x00007fff8e09a32f  dprintf_formatf  /home/stefano/Desktop/myproject/curl-7.33.0/lib/mprintf.c  572
2  0x00007fff8e09b5a4  curl_mvsnprintf  /home/stefano/Desktop/myproject/curl-7.33.0/lib/mprintf.c  932
3  0x00007fff8e089510  Curl_failf  /home/stefano/Desktop/myproject/curl-7.33.0/lib/sendf.c  152
4  0x00007fff8e07dbf4  Curl_resolv_timeout  /home/stefano/Desktop/myproject/curl-7.33.0/lib/hostip.c  618
5  0x00007fff78012bf8  ??    
6  0x000000c300000016  ??    
7  0x00007fff8e0d3604  ??    
8  0x0000000000000002  ??    
9  0x00000000001b7740  ??    
10  0x0000000000000000  ??    

UPDATE 1 Run it again under debugger and met a crash at the line

FILE *fd; 
fd = fopen(files[i].c_str(), "rb"); //<---here goes the crash!

files[i].c_str() is supposed to give const* char from wxString The new BT is

0  0x00007fff8e08952a  Curl_failf  /home/stefano/Desktop/myproject/curl-7.33.0/lib/sendf.c  154
1  0x00007fff8e07dbf4  Curl_resolv_timeout  /home/stefano/Desktop/myproject/curl-7.33.0/lib/hostip.c  618
2  0x00007fff780158c8  ??    
3  0x00000000001b7730  ??    
4  0x00007fff78009808  ??    
5  0x00007fff78015e79  ??    
6  0x00007fff78009808  ??    
7  0x00007fff8c8a04a0  ??    
8  0x00007fff8e0c84ca  ftp_multi_statemach  /home/stefano/Desktop/myproject/curl-7.33.0/lib/ftp.c  3113
Stefano Mtangoo
  • 6,017
  • 6
  • 47
  • 93
  • 4
    This seems to me as if you've corrupted the internal malloc() structures somewhere in your code, which is most probably caused by allocating not enough space somewhere. Compile your code with valgrind and check if it throws any errors. – Guntram Blohm Dec 30 '13 at 07:26
  • I use C++ and does not do any allocation. Am trying to probe and see where ths is rising from. I will try valgrind option – Stefano Mtangoo Dec 30 '13 at 07:34
  • Sorry, didnt see the c++ tag. Still, it's easy to write to a memory location that shouldn't be written to in c++ as well. In my experience, crashes in libraries that normally work well are mostly triggered by that kind of error, that's why i suggested it. – Guntram Blohm Dec 30 '13 at 07:39
  • @GuntramBlohm, I have updated my question and added some information. Can you give any direction from there? Am very shallow in Cand try to avoid C libraries but this time I cannot – Stefano Mtangoo Dec 30 '13 at 07:40
  • Still, use [valgrind](http://valgrind.org/) on your C++ program. With [GCC 4.8](http://gcc.gnu.org/4.8/) use also `-g -Wall -fsanitize=address` when compiling your program. And even if you don't do any explicit allocation, as soon as you use STL, some implicit allocation is done inside. And use the debug variants of your libraries (e.g. install `libcurl-dbg` package on Debian). – Basile Starynkevitch Dec 30 '13 at 07:42
  • 1
    When you're at the point of the crash in the debugger, have the debugger print i. Print files[i]. Print files[i].c_str(). Do they all look ok to you? If `i` is out of the array range, this would explain the crash very well. – Guntram Blohm Dec 30 '13 at 07:44
  • @BasileStarynkevitch, am using 4.7 so I cannot use fsanitize. I use debug version of libcurl compiled by myself. So I have to do valgrind thing. – Stefano Mtangoo Dec 30 '13 at 07:49
  • @GuntramBlohm , let me check it now – Stefano Mtangoo Dec 30 '13 at 07:49
  • @GuntramBlohm, index i is not out of range and it gives valid path. I'm starting to get feeling that crash is random caused by something global. may be time outs or something like that! – Stefano Mtangoo Dec 30 '13 at 07:54

1 Answers1

17

Such error can be caused if you're using curl in non-main thread. When curl can't resolve dns entry, it sends a signal (by default) to interrupt a thread by timeout. Signals are not thread safe and can cause a crash. You should compile libcurl with --enable-threaded-resolver or with support of c-ares.

Also for me it was useful to disable signals at all

curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1)
Ivan Fateev
  • 1,032
  • 1
  • 10
  • 26