@@ -62,26 +62,30 @@ size_t File::Size() {
62
62
63
63
bool File::Flush () { return fflush (f_) == 0 ; }
64
64
65
+ // Deletes "this" on closing.
65
66
bool File::Close () {
66
67
if (f_ == nullptr ) {
67
68
return true ;
68
69
}
69
70
70
71
if (fclose (f_) == 0 ) {
71
72
f_ = nullptr ;
73
+ delete this ;
72
74
return true ;
73
75
} else {
74
76
return false ;
75
77
}
76
78
}
77
79
80
+ // Deletes "this" on closing.
78
81
absl::Status File::Close (int flags) {
79
82
if (f_ == nullptr ) {
80
83
return absl::Status ();
81
84
}
82
85
83
86
if (fclose (f_) == 0 ) {
84
87
f_ = nullptr ;
88
+ delete this ;
85
89
return absl::Status ();
86
90
} else {
87
91
const std::string msg = absl::StrCat (" Could not close file '" , name_, " '" );
@@ -191,27 +195,23 @@ absl::Status GetContents(absl::string_view filename, std::string* output,
191
195
const int64_t size = file->Size ();
192
196
if (file->ReadToString (output, size) == size) {
193
197
status.Update (file->Close (flags));
194
- delete file;
195
198
return status;
196
199
}
197
200
#if defined(_MSC_VER)
198
201
// On windows, binary files needs to be opened with the "rb" flags.
199
202
file->Close ();
200
- delete file;
201
203
// Retry in binary mode.
202
204
status = file::Open (filename, " rb" , &file, flags);
203
205
if (!status.ok ()) return status;
204
206
205
207
const int64_t b_size = file->Size ();
206
208
if (file->ReadToString (output, b_size) == b_size) {
207
209
status.Update (file->Close (flags));
208
- delete file;
209
210
return status;
210
211
}
211
212
#endif // _MSC_VER
212
213
213
214
file->Close (flags).IgnoreError (); // Even if ReadToString() fails!
214
- delete file;
215
215
return absl::Status (absl::StatusCode::kInvalidArgument ,
216
216
absl::StrCat (" Could not read from '" , filename, " '." ));
217
217
}
@@ -233,7 +233,6 @@ absl::Status SetContents(absl::string_view filename, absl::string_view contents,
233
233
if (!status.ok ()) return status;
234
234
status = file::WriteString (file, contents, flags);
235
235
status.Update (file->Close (flags)); // Even if WriteString() fails!
236
- delete file;
237
236
return status;
238
237
}
239
238
0 commit comments