- size_t ret = m_file->Write(buffer, size);
- if (m_file->Error())
- m_lasterror = wxStream_WRITE_ERR;
- else
- m_lasterror = wxStream_NOERROR;
- return ret;
+ size_t ret = m_file->Write(buffer, size);
+ // It is not safe to call Error() if the file is not opened.
+ if (!m_file->IsOpened() || m_file->Error())
+ m_lasterror = wxSTREAM_WRITE_ERROR;
+ else
+ m_lasterror = wxSTREAM_NO_ERROR;
+ return ret;