Help with my DownloadTask::Listener?

Hi there Jucers,

I’m just getting started with JUCE (and C++ more generally) and I was hoping that one of you might be able to help with my DownloadTask::Listener.

I’ve written subclassed DownloadTask::Listener with the code below.

class Mylistener : public juce::URL::DownloadTask::Listener
{
    public:
        void finished (juce::URL::DownloadTask *task,bool success)
    { std::cout << "Download finished!" <<std::endl; }

    void progress (juce::URL::DownloadTask *task,
                   int64     bytesDownloaded,
                   int64     totalLength )
{ std::cout<< "downloaded: " << bytesDownloaded << ' of ' << totalLength << std::endl; }

};

I’m using it like this:

        Mylistener ml;
        url.downloadToFile(file,"",&ml);

When I run my download code without the listener, the url downloads fine. When I include the listener argument to url.downloadToFile, though, as above, I get a EXC_BAD_ACCESS error at line 62 of juce_URL.cpp ( listener->progress ( this , downloaded, contentLength);).

DownloadTask thread (30): EXC_BAD_ACCESS (code=1, address=0x18)

Does anyone know what I’m doing wrong?

Your Mylistener ml; is getting created on the stack so as soon as the function it’s in finishes, it no longer exists. You need to ensure your listener still exists during the entire download process.

The more standard way to do it would be to make whatever class is starting the download and expecting the results to be the DownloadTask::Listener

Ah, yes, that’s exactly what was happening. I’d much rather do things the standard way. Thank you!!