Skip to content

Commit 0d7a96f

Browse files
committed
base: Sync File with google3 implem
1 parent 87626e2 commit 0d7a96f

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

ortools/base/file.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,30 @@ size_t File::Size() {
6262

6363
bool File::Flush() { return fflush(f_) == 0; }
6464

65+
// Deletes "this" on closing.
6566
bool File::Close() {
6667
if (f_ == nullptr) {
6768
return true;
6869
}
6970

7071
if (fclose(f_) == 0) {
7172
f_ = nullptr;
73+
delete this;
7274
return true;
7375
} else {
7476
return false;
7577
}
7678
}
7779

80+
// Deletes "this" on closing.
7881
absl::Status File::Close(int flags) {
7982
if (f_ == nullptr) {
8083
return absl::Status();
8184
}
8285

8386
if (fclose(f_) == 0) {
8487
f_ = nullptr;
88+
delete this;
8589
return absl::Status();
8690
} else {
8791
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,
191195
const int64_t size = file->Size();
192196
if (file->ReadToString(output, size) == size) {
193197
status.Update(file->Close(flags));
194-
delete file;
195198
return status;
196199
}
197200
#if defined(_MSC_VER)
198201
// On windows, binary files needs to be opened with the "rb" flags.
199202
file->Close();
200-
delete file;
201203
// Retry in binary mode.
202204
status = file::Open(filename, "rb", &file, flags);
203205
if (!status.ok()) return status;
204206

205207
const int64_t b_size = file->Size();
206208
if (file->ReadToString(output, b_size) == b_size) {
207209
status.Update(file->Close(flags));
208-
delete file;
209210
return status;
210211
}
211212
#endif // _MSC_VER
212213

213214
file->Close(flags).IgnoreError(); // Even if ReadToString() fails!
214-
delete file;
215215
return absl::Status(absl::StatusCode::kInvalidArgument,
216216
absl::StrCat("Could not read from '", filename, "'."));
217217
}
@@ -233,7 +233,6 @@ absl::Status SetContents(absl::string_view filename, absl::string_view contents,
233233
if (!status.ok()) return status;
234234
status = file::WriteString(file, contents, flags);
235235
status.Update(file->Close(flags)); // Even if WriteString() fails!
236-
delete file;
237236
return status;
238237
}
239238

ortools/util/filelineiter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ class FileLines {
161161
~FileLines() {
162162
if (file_ != nullptr) {
163163
file_->Close(file::Defaults()).IgnoreError();
164-
delete file_;
165164
file_ = nullptr;
166165
}
167166
}

0 commit comments

Comments
 (0)