comparison base/Exceptions.cpp @ 1848:98339fac0faf

Avoid returning expired pointers
author Chris Cannam
date Thu, 23 Apr 2020 15:39:08 +0100
parents cc27f35aa75c
children
comparison
equal deleted inserted replaced
1847:9762a7f084a0 1848:98339fac0faf
20 #include "Debug.h" 20 #include "Debug.h"
21 21
22 FileNotFound::FileNotFound(QString file) throw() : 22 FileNotFound::FileNotFound(QString file) throw() :
23 m_file(file) 23 m_file(file)
24 { 24 {
25 cerr << "ERROR: File not found: " 25 cerr << "ERROR: File not found: " << file << endl;
26 << file << endl;
27 } 26 }
28 27
29 const char * 28 const char *
30 FileNotFound::what() const throw() 29 FileNotFound::what() const throw()
31 { 30 {
32 return QString("File \"%1\" not found") 31 static QByteArray msg;
33 .arg(m_file).toLocal8Bit().data(); 32 msg = QString("File \"%1\" not found").arg(m_file).toLocal8Bit();
33 return msg.data();
34 } 34 }
35 35
36 FailedToOpenFile::FailedToOpenFile(QString file) throw() : 36 FailedToOpenFile::FailedToOpenFile(QString file) throw() :
37 m_file(file) 37 m_file(file)
38 { 38 {
41 } 41 }
42 42
43 const char * 43 const char *
44 FailedToOpenFile::what() const throw() 44 FailedToOpenFile::what() const throw()
45 { 45 {
46 return QString("Failed to open file \"%1\"") 46 static QByteArray msg;
47 .arg(m_file).toLocal8Bit().data(); 47 msg = QString("Failed to open file \"%1\"").arg(m_file).toLocal8Bit();
48 return msg.data();
48 } 49 }
49 50
50 DirectoryCreationFailed::DirectoryCreationFailed(QString directory) throw() : 51 DirectoryCreationFailed::DirectoryCreationFailed(QString directory) throw() :
51 m_directory(directory) 52 m_directory(directory)
52 { 53 {
53 cerr << "ERROR: Directory creation failed for directory: " 54 cerr << "ERROR: Directory creation failed for directory: "
54 << directory << endl; 55 << directory << endl;
55 } 56 }
56 57
57 const char * 58 const char *
58 DirectoryCreationFailed::what() const throw() 59 DirectoryCreationFailed::what() const throw()
59 { 60 {
60 return QString("Directory creation failed for \"%1\"") 61 static QByteArray msg;
61 .arg(m_directory).toLocal8Bit().data(); 62 msg = QString("Directory creation failed for \"%1\"").arg(m_directory)
63 .toLocal8Bit();
64 return msg.data();
62 } 65 }
63 66
64 FileReadFailed::FileReadFailed(QString file) throw() : 67 FileReadFailed::FileReadFailed(QString file) throw() :
65 m_file(file) 68 m_file(file)
66 { 69 {
67 cerr << "ERROR: File read failed for file: " 70 cerr << "ERROR: File read failed for file: " << file << endl;
68 << file << endl;
69 } 71 }
70 72
71 const char * 73 const char *
72 FileReadFailed::what() const throw() 74 FileReadFailed::what() const throw()
73 { 75 {
74 return QString("File read failed for \"%1\"") 76 static QByteArray msg;
75 .arg(m_file).toLocal8Bit().data(); 77 msg = QString("File read failed for \"%1\"").arg(m_file).toLocal8Bit();
78 return msg.data();
76 } 79 }
77 80
78 FileOperationFailed::FileOperationFailed(QString file, QString op) throw() : 81 FileOperationFailed::FileOperationFailed(QString file, QString op) throw() :
79 m_file(file), 82 m_file(file),
80 m_operation(op) 83 m_operation(op)
81 { 84 {
82 cerr << "ERROR: File " << op << " failed for file: " 85 cerr << "ERROR: File " << op << " failed for file: " << file << endl;
83 << file << endl;
84 } 86 }
85 87
86 const char * 88 const char *
87 FileOperationFailed::what() const throw() 89 FileOperationFailed::what() const throw()
88 { 90 {
89 return QString("File %1 failed for \"%2\"") 91 static QByteArray msg;
90 .arg(m_operation).arg(m_file).toLocal8Bit().data(); 92 msg = QString("File %1 failed for \"%2\"").arg(m_operation).arg(m_file)
93 .toLocal8Bit();
94 return msg.data();
91 } 95 }
92 96
93 InsufficientDiscSpace::InsufficientDiscSpace(QString directory, 97 InsufficientDiscSpace::InsufficientDiscSpace(QString directory,
94 size_t required, 98 size_t required,
95 size_t available) throw() : 99 size_t available) throw() :
96 m_directory(directory), 100 m_directory(directory),
97 m_required(required), 101 m_required(required),
98 m_available(available) 102 m_available(available)
99 { 103 {
100 cerr << "ERROR: Not enough disc space available in " 104 cerr << "ERROR: Not enough disc space available in "
101 << directory << ": need " << required 105 << directory << ": need " << required
102 << ", only have " << available << endl; 106 << ", only have " << available << endl;
103 } 107 }
104 108
105 InsufficientDiscSpace::InsufficientDiscSpace(QString directory) throw() : 109 InsufficientDiscSpace::InsufficientDiscSpace(QString directory) throw() :
106 m_directory(directory), 110 m_directory(directory),
107 m_required(0), 111 m_required(0),
108 m_available(0) 112 m_available(0)
109 { 113 {
110 cerr << "ERROR: Not enough disc space available in " 114 cerr << "ERROR: Not enough disc space available in " << directory << endl;
111 << directory << endl;
112 } 115 }
113 116
114 const char * 117 const char *
115 InsufficientDiscSpace::what() const throw() 118 InsufficientDiscSpace::what() const throw()
116 { 119 {
120 static QByteArray msg;
117 if (m_required > 0) { 121 if (m_required > 0) {
118 return QString("Not enough space available in \"%1\": need %2, have %3") 122 msg = QString("Not enough space available in \"%1\": need %2, have %3")
119 .arg(m_directory).arg(m_required).arg(m_available).toLocal8Bit().data(); 123 .arg(m_directory).arg(m_required).arg(m_available).toLocal8Bit();
120 } else { 124 } else {
121 return QString("Not enough space available in \"%1\"") 125 msg = QString("Not enough space available in \"%1\"")
122 .arg(m_directory).toLocal8Bit().data(); 126 .arg(m_directory).toLocal8Bit();
123 } 127 }
128 return msg.data();
124 } 129 }
125 130
126 AllocationFailed::AllocationFailed(QString purpose) throw() : 131 AllocationFailed::AllocationFailed(QString purpose) throw() :
127 m_purpose(purpose) 132 m_purpose(purpose)
128 { 133 {
129 cerr << "ERROR: Allocation failed: " << purpose 134 cerr << "ERROR: Allocation failed: " << purpose << endl;
130 << endl;
131 } 135 }
132 136
133 const char * 137 const char *
134 AllocationFailed::what() const throw() 138 AllocationFailed::what() const throw()
135 { 139 {
136 return QString("Allocation failed: %1") 140 static QByteArray msg;
137 .arg(m_purpose).toLocal8Bit().data(); 141 msg = QString("Allocation failed: %1").arg(m_purpose).toLocal8Bit();
142 return msg.data();
138 } 143 }
139 144
140 145