mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
Calling delete on a FILE* created with fopen is ill-advised. Uses a
custom deleter.
This commit is contained in:
parent
ed59160a47
commit
a013348a7e
|
@ -13817,17 +13817,25 @@ That would be a leak.
|
|||
fclose(f);
|
||||
}
|
||||
|
||||
If `i == 0` the file handle for `a file` is leaked. On the other hand, the `ifstream` for `another file` will correctly close its file (upon destruction). If you must use an explicit pointer, rather than a resource handle with specific semantics, use a `unique_ptr` or a `shared_ptr`:
|
||||
If `i == 0` the file handle for `a file` is leaked. On the other hand, the `ifstream` for `another file` will correctly close its file (upon destruction). If you must use an explicit pointer, rather than a resource handle with specific semantics, use a `unique_ptr` or a `shared_ptr` with a custom deleter:
|
||||
|
||||
void f(int i)
|
||||
{
|
||||
unique_ptr<FILE> f = fopen("a file", "r");
|
||||
unique_ptr<FILE, int(*)(FILE*)> f(fopen("a file", "r"), fclose);
|
||||
// ...
|
||||
if (i == 0) return;
|
||||
// ...
|
||||
}
|
||||
|
||||
The code is simpler as well as correct.
|
||||
Better:
|
||||
|
||||
void f(int i)
|
||||
{
|
||||
ifstream input {"a file"};
|
||||
// ...
|
||||
if (i == 0) return;
|
||||
// ...
|
||||
}
|
||||
|
||||
##### Enforcement
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user