I am writing a C++ app that needd to download a file and show a download progress dialog. I am using cURL to complete the doenlaod request, however, for some reason, when I initiate the download by clicking the install button, the application immediately closes with no errors or other indication. Here is what I have so far:
#include <wx/wx.h>
#include <iostream>
#include <fstream>
#include <curl/curl.h>
#include <thread>
class FileDownloader {
public:
FileDownloader() = default;
~FileDownloader() = default;
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp)
{
size_t totalSize = size * nmemb;
wxThreadEvent* event = static_cast<wxThreadEvent*>(userp);
wxString data(static_cast<const char*>(contents), wxConvUTF8, totalSize);
event->SetString(data);
wxQueueEvent(wxTheApp->GetTopWindow(), event);
return totalSize;
}
bool DownloadFile(const std::string& url, const std::string& destination)
{
CURL* curl = curl_easy_init();
if (!curl)
return false;
FILE* file = fopen(destination.c_str(), "wb");
if (!file)
{
curl_easy_cleanup(curl);
return false;
}
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
CURLcode res = curl_easy_perform(curl);
fclose(file);
curl_easy_cleanup(curl);
return res == CURLE_OK;
}
};
class WindowsInstaller : public wxApp
{
public:
bool OnInit() override;
};
// This defines the equivalent of main() for the current platform.
wxIMPLEMENT_APP(WindowsInstaller);
class NewFrame : public wxFrame
{
public:
NewFrame();
private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnInstallButtonClick(wxCommandEvent& event);
void OnSetButtonClick(wxCommandEvent& event);
void OnCheckBoxClicked(wxCommandEvent& event);
wxButton* install_button;
wxButton* set_location;
wxTextCtrl* textCtrl;
wxCheckBox* accept_checkbox;
wxGauge* gauge;
};
bool WindowsInstaller::OnInit()
{
NewFrame* frame = new NewFrame();
frame->Show();
return true;
}
enum
{
ID_Hello = 1,
ID_Install_Button = 2,
ID_Set_Location = 3,
ID_Accept_CheckBox = 4,
ID_UPDATE_PROGRESS = 5,
ID_DOWNLOAD_COMPLETE = 6,
ID_DOWNLOAD_ERROR = 7,
ID_ENABLE_INSTALL_BUTTON = 8
};
NewFrame::NewFrame()
: wxFrame(nullptr, wxID_ANY, "Hello World", wxDefaultPosition, wxSize(700, 500), wxDEFAULT_FRAME_STYLE & ~(wxRESIZE_BORDER | wxMAXIMIZE_BOX))
{
wxMenu* menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl+H",
"Help string shown in status bar for this menu item");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu* menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar* menuBar = new wxMenuBar;
menuBar->Append(menuFile, "&File");
menuBar->Append(menuHelp, "&Help");
SetMenuBar(menuBar);
CreateStatusBar();
SetStatusText("Welcome to wxWidgets!");
wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
textCtrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxHSCROLL);
textCtrl->SetBackgroundColour(*wxWHITE);
textCtrl->SetForegroundColour(*wxBLACK);
textCtrl->SetFont(wxFont(wxFontInfo(10)));
wxString licenseText = "Copyright 2023 Author \n\n"
"Permission is hereby granted, free of charge, to any person obtaining a copy of this software "
"and \nassociated documentation files(the “Software”), to deal in the Software without restriction, "
"\nincluding without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, "
"\nand/or sell copies of the Software, and to permit persons to whom the Software is furnished to do \n"
"so, subject to the following conditions : \n"
"The above copyright notice and this permission notice shall be included in all copies or substantial \n"
"portions of the Software. \n\n"
"THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR \n"
"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS \n"
"FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS \n"
"OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, \n"
"WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN \n"
"CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.";
textCtrl->SetValue(licenseText);
mainSizer->Add(textCtrl, 1, wxEXPAND | wxALL, 20);
accept_checkbox = new wxCheckBox(this, ID_Accept_CheckBox, "I accept the license agreement");
mainSizer->Add(accept_checkbox, 0, wxALIGN_LEFT | wxALL, 10);
gauge = new wxGauge(this, wxID_ANY, 100, wxDefaultPosition, wxDefaultSize, wxGA_HORIZONTAL);
mainSizer->Add(gauge, 0, wxEXPAND | wxALL, 20);
install_button = new wxButton(this, ID_Install_Button, "Install", wxPoint(20, 20));
set_location = new wxButton(this, ID_Set_Location, "Set Location", wxPoint(20, 20));
install_button->Enable(false);
mainSizer->AddStretchSpacer();
mainSizer->Add(install_button, 0, wxALIGN_CENTER | wxALL, 10);
mainSizer->Add(set_location, 0, wxALIGN_CENTER | wxALL, 5);
SetSizerAndFit(mainSizer);
Bind(wxEVT_MENU, &NewFrame::OnHello, this, ID_Hello);
Bind(wxEVT_MENU, &NewFrame::OnAbout, this, wxID_ABOUT);
Bind(wxEVT_MENU, &NewFrame::OnExit, this, wxID_EXIT);
Bind(wxEVT_BUTTON, &NewFrame::OnInstallButtonClick, this, ID_Install_Button);
Bind(wxEVT_BUTTON, &NewFrame::OnSetButtonClick, this, ID_Set_Location);
Bind(wxEVT_CHECKBOX, &NewFrame::OnCheckBoxClicked, this, ID_Accept_CheckBox);
wxWindow::SetInitialSize(wxSize(700, 500));
}
void NewFrame::OnExit(wxCommandEvent& event)
{
Close(true);
}
void NewFrame::OnAbout(wxCommandEvent& event)
{
wxMessageBox("This is a wxWidgets Hello World example",
"About Hello World", wxOK | wxICON_INFORMATION);
}
void NewFrame::OnHello(wxCommandEvent& event)
{
wxLogMessage("Hello world from wxWidgets!");
}
void NewFrame::OnInstallButtonClick(wxCommandEvent& event)
{
if (!accept_checkbox->IsChecked())
{
wxMessageBox("Please accept the license agreement.", "Error", wxOK | wxICON_ERROR);
return;
}
// Test file download
FileDownloader downloader;
wxString url = "https://releases.ubuntu.com/22.04.2/ubuntu-22.04.2-desktop-amd64.iso";
wxString destination = "F:/ubuntu-22.04.2-desktop-amd64.iso";
install_button->Disable();
std::thread downloadThread([this, &downloader, url, destination]() {
wxThreadEvent* progressEvent = new wxThreadEvent(wxEVT_THREAD, ID_UPDATE_PROGRESS);
progressEvent->SetInt(0);
wxQueueEvent(wxTheApp->GetTopWindow(), progressEvent);
if (downloader.DownloadFile(url.ToStdString(), destination.ToStdString()))
{
wxThreadEvent* completeEvent = new wxThreadEvent(wxEVT_THREAD, ID_DOWNLOAD_COMPLETE);
wxQueueEvent(wxTheApp->GetTopWindow(), completeEvent);
}
else
{
wxThreadEvent* errorEvent = new wxThreadEvent(wxEVT_THREAD, ID_DOWNLOAD_ERROR);
wxQueueEvent(wxTheApp->GetTopWindow(), errorEvent);
}
wxThreadEvent* enableEvent = new wxThreadEvent(wxEVT_THREAD, ID_ENABLE_INSTALL_BUTTON);
wxQueueEvent(wxTheApp->GetTopWindow(), enableEvent);
});
downloadThread.detach();
}
void NewFrame::OnSetButtonClick(wxCommandEvent& event)
{
wxMessageBox("Button clicked!", "Button", wxOK | wxICON_INFORMATION);
}
void NewFrame::OnCheckBoxClicked(wxCommandEvent& event)
{
if (accept_checkbox->IsChecked())
install_button->Enable(true);
else
install_button->Enable(false);
}
This is the exception that the debugger captures:
Exception thrown at 0x00007FFC285301E8 (wxbase32ud_vc_custom.dll) in WindowsInstaller.exe: 0xC0000005: Access violation writing location 0x00000080FDFDFDFD.