cavaliergopher / grab

A download manager package for Go
BSD 3-Clause "New" or "Revised" License
1.39k stars 150 forks source link

panic: runtime error: invalid memory address or nil pointer dereference #5

Closed Elite closed 8 years ago

Elite commented 8 years ago

I get the error below on running the example batchdl code:

package main

import (
    "fmt"
    "github.com/cavaliercoder/grab"
    "os"
    "time"
)

func main() {
    // get URL to download from command args
    if len(os.Args) < 2 {
        fmt.Fprintf(os.Stderr, "usage: %s url [url]...\n", os.Args[0])
        os.Exit(1)
    }

    urls := os.Args[1:]

    // start file downloads, 3 at a time
    fmt.Printf("Downloading %d files...\n", len(urls))
    respch, err := grab.GetBatch(3, ".", urls...)
    if err != nil {
        fmt.Fprintf(os.Stderr, "%v\n", err)
        os.Exit(1)
    }

    // start a ticker to update progress every 200ms
    t := time.NewTicker(200 * time.Millisecond)

    // monitor downloads
    completed := 0
    inProgress := 0
    responses := make([]*grab.Response, 0)
    for completed < len(urls) {
        select {
        case resp := <-respch:
            // a new response has been received and has started downloading
            // (nil is received once, when the channel is closed by grab)
            if resp != nil {
                responses = append(responses, resp)
            }

        case <-t.C:
            // clear lines
            if inProgress > 0 {
                fmt.Printf("\033[%dA\033[K", inProgress)
            }

            // update completed downloads
            for i, resp := range responses {
                if resp != nil && resp.IsComplete() {
                    // print final result
                    if resp.Error != nil {
                        fmt.Fprintf(os.Stderr, "Error downloading %s: %v\n", resp.Request.URL(), resp.Error)
                    } else {
                        fmt.Printf("Finished %s %d / %d bytes (%d%%)\n", resp.Filename, resp.BytesTransferred(), resp.Size, int(100*resp.Progress()))
                    }

                    // mark completed
                    responses[i] = nil
                    completed++
                }
            }

            // update downloads in progress
            inProgress = 0
            for _, resp := range responses {
                if resp != nil {
                    inProgress++
                    fmt.Printf("Downloading %s %d / %d bytes (%d%%)\033[K\n", resp.Filename, resp.BytesTransferred(), resp.Size, int(100*resp.Progress()))
                }
            }
        }
    }

    t.Stop()

    fmt.Printf("%d files successfully downloaded.\n", len(urls))
}
C:\GoCode\src>go run batchdl.go "https://www.google.com/images/branding/googlelo
go/1x/googlelogo_color_272x92dp.png"
Downloading 1 files...
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x52a9db]

goroutine 10 [running]:
panic(0x6735e0, 0x10c60030)
        C:/Go/src/runtime/panic.go:464 +0x326
sync/atomic.LoadUint64(0x10c8a594, 0x6abcc0, 0x4)
        C:/Go/src/sync/atomic/asm_386.s:159 +0xb
github.com/cavaliercoder/grab.(*Response).BytesTransferred(0x10c8a540, 0x4, 0x6a
a718)
        C:/GoCode/src/github.com/cavaliercoder/grab/response.go:81 +0x2b
github.com/cavaliercoder/grab.(*Client).do(0x10cc17e0, 0x10c502d0, 0x0, 0x0, 0x0
)
        C:/GoCode/src/github.com/cavaliercoder/grab/client.go:222 +0x3a2
github.com/cavaliercoder/grab.(*Client).DoAsync.func1(0x10cc17e0, 0x10c502d0, 0x
10c485c0)
        C:/GoCode/src/github.com/cavaliercoder/grab/client.go:94 +0x2d
created by github.com/cavaliercoder/grab.(*Client).DoAsync
        C:/GoCode/src/github.com/cavaliercoder/grab/client.go:102 +0x62
exit status 2

C:\GoCode\src>

Strangely, this code runs fine on my Windows 10 64bit setup???

cavaliercoder commented 8 years ago

Works fine on my Win10 x64 also. Which machine type does it fail on though?

Elite commented 8 years ago

Windows 7 32 bit

cavaliercoder commented 8 years ago

Thanks. I've replicated the issue on Win10 with Go v1.5 and v.16 by setting GOARCH=386. Closing as a duplicate of #4.