# Buggy code
def on_received_metainfo(self, response):
    if not response or not self:
        return

    if "error" in response:
        if response["error"] == "metainfo error":
            # If it failed to load metainfo for max number of times, show an error message in red.
            if self.metainfo_retries > METAINFO_MAX_RETRIES:
                self.dialog_widget.loading_files_label.setStyleSheet("color:#ff0000;")
                self.dialog_widget.loading_files_label.setText(
                    "Failed to load files. Click to retry again."
                )
                return
            self.perform_files_request()

        elif "code" in response["error"] and response["error"]["code"] == "IOError":
            self.dialog_widget.loading_files_label.setText(
                "Unable to read torrent file data"
            )
        else:
            self.dialog_widget.loading_files_label.setText(
                "Error: %s" % response["error"]
            )
        return

    metainfo = json.loads(unhexlify(response["metainfo"]))
    if "files" in metainfo["info"]:  # Multi-file torrent
        files = metainfo["info"]["files"]
    else:
        files = [
            {"path": [metainfo["info"]["name"]], "length": metainfo["info"]["length"]}
        ]

    # Show if the torrent already exists in the downloads
    if response.get("download_exists"):
        self.dialog_widget.existing_download_info_label.setText(
            "Note: this torrent already exists in the Downloads"
        )
    else:
        self.dialog_widget.existing_download_info_label.setText("")

    self.dialog_widget.files_list_view.clear()
    for filename in files:
        item = DownloadFileTreeWidgetItem(self.dialog_widget.files_list_view)
        item.setText(0, "/".join(filename["path"]))
        item.setText(1, format_size(float(filename["length"])))
        item.setData(0, Qt.UserRole, filename)
        item.setCheckState(2, Qt.Checked)
        self.dialog_widget.files_list_view.addTopLevelItem(item)

    self.has_metainfo = True
    self.dialog_widget.loading_files_label.setHidden(True)
    self.dialog_widget.download_files_container.setHidden(False)
    self.dialog_widget.files_list_view.setHidden(False)
    self.dialog_widget.adjustSize()
    self.on_main_window_resize()

    self.received_metainfo.emit(metainfo)


# Traceback (RuntimeError)
Traceback (most recent call last):
File "/home/<user>/src/tribler/src/run_tribler.py", line 154, in <module>
sys.exit(app.exec_())
File "/home/<user>/src/tribler/src/tribler-gui/tribler_gui/utilities.py", line 382, in trackback_wrapper
callback(*args, **kwargs)
File "/home/<user>/src/tribler/src/tribler-gui/tribler_gui/tribler_window.py", line 796, in on_add_torrent_browse_file
self.process_uri_request()
File "/home/<user>/src/tribler/src/tribler-gui/tribler_gui/tribler_window.py", line 521, in process_uri_request
self.start_download_from_uri(uri)
File "/home/<user>/src/tribler/src/tribler-gui/tribler_gui/tribler_window.py", line 822, in start_download_from_uri
self.dialog = StartDownloadDialog(self, self.download_uri)
File "/home/<user>/src/tribler/src/tribler-gui/tribler_gui/dialogs/startdownloaddialog.py", line 124, in __init__
self.perform_files_request()
File "/home/<user>/src/tribler/src/tribler-gui/tribler_gui/dialogs/startdownloaddialog.py", line 162, in perform_files_request
self.rest_request = TriblerNetworkRequest(request, self.on_received_metainfo, capture_core_errors=False)
File "/home/<user>/src/tribler/src/tribler-gui/tribler_gui/tribler_request_manager.py", line 176, in __init__
connect(self.received_json, self.reply_callback)
tribler_gui.utilities.CreationTraceback
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/<user>/src/tribler/src/tribler-gui/tribler_gui/utilities.py", line 385, in trackback_wrapper
raise exc from source_exc
File "/home/<user>/src/tribler/src/tribler-gui/tribler_gui/utilities.py", line 382, in trackback_wrapper
callback(*args, **kwargs)
File "/home/<user>/src/tribler/src/tribler-gui/tribler_gui/dialogs/startdownloaddialog.py", line 212, in on_received_metainfo
self.dialog_widget.existing_download_info_label.setText("")
RuntimeError: wrapped C/C++ object of type QToolButton has been deleted

# Fixed code
def on_received_metainfo(self, response):
    if not response or not self or self.closed:
        return

    if "error" in response:
        if response["error"] == "metainfo error":
            # If it failed to load metainfo for max number of times, show an error message in red.
            if self.metainfo_retries > METAINFO_MAX_RETRIES:
                self.dialog_widget.loading_files_label.setStyleSheet("color:#ff0000;")
                self.dialog_widget.loading_files_label.setText(
                    "Failed to load files. Click to retry again."
                )
                return
            self.perform_files_request()

        elif "code" in response["error"] and response["error"]["code"] == "IOError":
            self.dialog_widget.loading_files_label.setText(
                "Unable to read torrent file data"
            )
        else:
            self.dialog_widget.loading_files_label.setText(
                "Error: %s" % response["error"]
            )
        return

    metainfo = json.loads(unhexlify(response["metainfo"]))
    if "files" in metainfo["info"]:  # Multi-file torrent
        files = metainfo["info"]["files"]
    else:
        files = [
            {"path": [metainfo["info"]["name"]], "length": metainfo["info"]["length"]}
        ]

    # Show if the torrent already exists in the downloads
    if response.get("download_exists"):
        self.dialog_widget.existing_download_info_label.setText(
            "Note: this torrent already exists in the Downloads"
        )
    else:
        self.dialog_widget.existing_download_info_label.setText("")

    self.dialog_widget.files_list_view.clear()
    for filename in files:
        item = DownloadFileTreeWidgetItem(self.dialog_widget.files_list_view)
        item.setText(0, "/".join(filename["path"]))
        item.setText(1, format_size(float(filename["length"])))
        item.setData(0, Qt.UserRole, filename)
        item.setCheckState(2, Qt.Checked)
        self.dialog_widget.files_list_view.addTopLevelItem(item)

    self.has_metainfo = True
    self.dialog_widget.loading_files_label.setHidden(True)
    self.dialog_widget.download_files_container.setHidden(False)
    self.dialog_widget.files_list_view.setHidden(False)
    self.dialog_widget.adjustSize()
    self.on_main_window_resize()

    self.received_metainfo.emit(metainfo)