request assistance resolving a std.net.curl segmentation fault

Danny Arends Danny.Arends at gmail.com
Fri May 19 12:40:29 UTC 2023


On Friday, 19 May 2023 at 11:07:01 UTC, anonymouse wrote:
> What am I doing wrong here?
>
> ```D
> import std.net.curl: Curl, CurlOption, CurlException;
> import std.file: exists;
> import std.stdio: File, writefln;
> import core.thread: Thread;
>
> void downloadFile(string url, string filename)
> {
>     while (true) {
>         try {
>             File fp;
>             if (filename.exists())
>                 fp.open(filename, "a");
>             else
>                 fp.open(filename, "w");
>             Curl curl;
>             curl.initialize();
>             curl.onProgress = delegate int(size_t dltotal, 
> size_t dlnow, size_t ultotal, size_t ulnow)
>             {
>                 writefln("Progress: %s of %s", dlnow, dltotal);
>                 return 0;
>             };
>             curl.set(CurlOption.url, url~filename);
>             curl.set(CurlOption.resume_from_large, fp.size());
>
>             // Start the download
>             curl.set(CurlOption.writedata, &fp);
>             curl.perform();
>
>             // Close the file
>             fp.close();
>             writefln("Download as %s complete.", filename);
>             break;
>         } catch (CurlException e) {
>             writefln("Error while downloading: %s", e.msg);
>
>             // Wait for a bit before retrying
>             Thread.sleep(imported!"core.time".seconds(10));
>         }
>     }
> }
>
> void main()
> {
>     string url = 
> "https://downloads.dlang.org/releases/2.x/2.103.1/";
>     string filename = "dmd.2.103.1.dmg";
>
>     downloadFile(url, filename);
> }
> ```
> Output:
> ```
> ./download_file
> Progress: 0 of 0
> Progress: 0 of 0
> Progress: 0 of 0
> Progress: 0 of 0
> Progress: 0 of 0
> Progress: 0 of 0
> Progress: 0 of 0
> Progress: 0 of 0
> Progress: 0 of 0
> Progress: 0 of 0
> Progress: 0 of 0
> Progress: 0 of 0
> Progress: 0 of 0
> Progress: 0 of 0
> Progress: 0 of 0
> Progress: 0 of 0
> Progress: 0 of 0
> Progress: 0 of 0
> zsh: segmentation fault  ./download_file
> ```
>
> Thanks.
>
> --anonymouse

You're running the whole thing in a while(TRUE) loop, recreating 
the curl object re-initiating the transfer and file pointer, etc. 
furthermore, the              curl.set(CurlOption.writedata, 
&fp); doesn't work as you expect..

After fiddling a bit, this works:

```D
import std.net.curl: Curl, CurlOption, CurlException;
import std.file: exists;
import std.stdio: File, writefln;
import core.thread: Thread;

void downloadFile(string url, string filename){
   try {
     File fp;
     fp.open(filename, "w");
     Curl curl;
     curl.initialize();
     curl.onProgress = delegate int(size_t dltotal, size_t dlnow, 
size_t ultotal, size_t ulnow){
       writefln("Progress: %s of %s", dlnow, dltotal);
       return 0;
     };
     curl.onReceive = (ubyte[] data) { fp.rawWrite(data); return 
data.length;};

     curl.set(CurlOption.url, url~filename);
     // Start the download
     curl.perform();

     writefln("Download as %s complete.", filename);
   } catch (CurlException e) {
     writefln("Error while downloading: %s", e.msg);
   }
}

void main(){
   string url = 
"https://downloads.dlang.org/releases/2.x/2.103.1/";
   string filename = "dmd.2.103.1.dmg";
   downloadFile(url, filename);
}
```


More information about the Digitalmars-d-learn mailing list