Skip to content

Commit

Permalink
Save a reference to messagebox in EditorStack to avoid memory to be f…
Browse files Browse the repository at this point in the history
…reed.

If the memory is freed, segfaults could happen when trying to access the C objects.
  • Loading branch information
rlaverde committed Jul 31, 2017
1 parent a88d914 commit baf4f87
Showing 1 changed file with 69 additions and 37 deletions.
106 changes: 69 additions & 37 deletions spyder/widgets/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,9 @@ def __init__(self, parent, actions):
#For opening last closed tabs
self.last_closed_files = []

# Reference to save msgbox and avoid memory to be freed.
self.msgbox = None

@Slot()
def show_in_external_file_explorer(self, fnames=None):
"""Show file in external file explorer"""
Expand Down Expand Up @@ -1424,11 +1427,17 @@ def save_if_changed(self, cancelable=False, index=None):
if not self.save():
return False
elif finfo.editor.document().isModified():
answer = QMessageBox.question(self, self.title,
_("<b>%s</b> has been modified."
"<br>Do you want to save changes?"
) % osp.basename(finfo.filename),
buttons)

self.msgbox = QMessageBox(
QMessageBox.Question,
self.title,
_("<b>{}</b> has been modified."
"<br>Do you want to save changes?").format(
osp.basename(finfo.filename)),
buttons,
parent=self)

answer = self.msgbox.exec()
if answer == QMessageBox.Yes:
if not self.save():
return False
Expand Down Expand Up @@ -1488,11 +1497,15 @@ def save(self, index=None, force=False):
self._refresh_outlineexplorer(index)
return True
except EnvironmentError as error:
QMessageBox.critical(self, _("Save"),
_("<b>Unable to save file '%s'</b>"
"<br><br>Error message:<br>%s"
) % (osp.basename(finfo.filename),
str(error)))
self.msgbox = QMessageBox(
QMessageBox.Critical,
_("Save"),
_("<b>Unable to save file '%s'</b>"
"<br><br>Error message:<br>%s"
) % (osp.basename(finfo.filename),
str(error)),
self)
self.msgbox.exec()
return False

def file_saved_in_other_editorstack(self, index, filename):
Expand Down Expand Up @@ -1577,11 +1590,15 @@ def save_copy_as(self, index=None):
self.plugin_load.emit(filename)
return True
except EnvironmentError as error:
QMessageBox.critical(self, _("Save"),
_("<b>Unable to save file '%s'</b>"
"<br><br>Error message:<br>%s"
) % (osp.basename(finfo.filename),
str(error)))
self.msgbox = QMessageBox(
QMessageBox.Critical,
_("Save"),
_("<b>Unable to save file '%s'</b>"
"<br><br>Error message:<br>%s"
) % (osp.basename(finfo.filename),
str(error)),
self)
self.msgbox.exec()
else:
return False

Expand Down Expand Up @@ -1750,12 +1767,16 @@ def __check_file_status(self, index):

elif not osp.isfile(finfo.filename):
# File doesn't exist (removed, moved or offline):
answer = QMessageBox.warning(self, self.title,
_("<b>%s</b> is unavailable "
"(this file may have been removed, moved "
"or renamed outside Spyder)."
"<br>Do you want to close it?") % name,
QMessageBox.Yes | QMessageBox.No)
self.msgbox = QMessageBox(
QMessageBox.Warning,
self.title,
_("<b>%s</b> is unavailable "
"(this file may have been removed, moved "
"or renamed outside Spyder)."
"<br>Do you want to close it?") % name,
QMessageBox.Yes | QMessageBox.No,
self)
answer = self.msgbox.exec()
if answer == QMessageBox.Yes:
self.close_file(index)
else:
Expand All @@ -1769,12 +1790,15 @@ def __check_file_status(self, index):
if to_text_string(lastm.toString()) \
!= to_text_string(finfo.lastmodified.toString()):
if finfo.editor.document().isModified():
answer = QMessageBox.question(self,
self.title,
_("<b>%s</b> has been modified outside Spyder."
"<br>Do you want to reload it and lose all "
"your changes?") % name,
QMessageBox.Yes | QMessageBox.No)
self.msgbox = QMessageBox(
QMessageBox.Question,
self.title,
_("<b>%s</b> has been modified outside Spyder."
"<br>Do you want to reload it and lose all "
"your changes?") % name,
QMessageBox.Yes | QMessageBox.No,
self)
answer = self.msgbox.exec()
if answer == QMessageBox.Yes:
self.reload(index)
else:
Expand Down Expand Up @@ -1876,11 +1900,15 @@ def revert(self):
finfo = self.data[index]
filename = finfo.filename
if finfo.editor.document().isModified():
answer = QMessageBox.warning(self, self.title,
_("All changes to <b>%s</b> will be lost."
"<br>Do you want to revert file from disk?"
) % osp.basename(filename),
QMessageBox.Yes|QMessageBox.No)
self.msgbox = QMessageBox(
QMessageBox.Warning,
self.title,
_("All changes to <b>%s</b> will be lost."
"<br>Do you want to revert file from disk?"
) % osp.basename(filename),
QMessageBox.Yes | QMessageBox.No,
self)
answer = self.msgbox.exec()
if answer != QMessageBox.Yes:
return
self.reload(index)
Expand Down Expand Up @@ -2032,11 +2060,15 @@ def load(self, filename, set_current=True):
if self.isVisible() and self.checkeolchars_enabled \
and sourcecode.has_mixed_eol_chars(text):
name = osp.basename(filename)
QMessageBox.warning(self, self.title,
_("<b>%s</b> contains mixed end-of-line "
"characters.<br>Spyder will fix this "
"automatically.") % name,
QMessageBox.Ok)
self.msgbox = QMessageBox(
QMessageBox.Warning,
self.title,
_("<b>%s</b> contains mixed end-of-line "
"characters.<br>Spyder will fix this "
"automatically.") % name,
QMessageBox.Ok,
self)
self.msgbox.exec()
self.set_os_eol_chars(index)
self.is_analysis_done = False
return finfo
Expand Down

0 comments on commit baf4f87

Please sign in to comment.