-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathAV Rule 79.cpp
More file actions
54 lines (51 loc) · 1.13 KB
/
AV Rule 79.cpp
File metadata and controls
54 lines (51 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// This class opens a file but never closes it. Even its clients
// cannot close the file
class ResourceLeak {
private:
int sockfd;
FILE* file;
public:
C() {
sockfd = socket(AF_INET, SOCK_STREAM, 0);
}
void f() {
file = fopen("foo.txt", "r");
...
}
};
// This class relies on its client to release any stream it
// allocates. Note that this means the client must have
// intimate knowledge of the implementation of the class to
// decide whether it is safe to release the stream.
class StreamPool {
private:
Stream *instance;
public:
Stream *createStream(char *name) {
if (!instance)
instance = new Stream(name);
return instance;
}
}
// This class handles its resources, but does not do that in
// the constructor/destructor. It can be rewritten easily to
// be safer to use.
class StreamHandler {
private:
char *_name;
Stream *stream;
public:
C(char *name) {
_name = strdup(name):
}
void open() {
stream = new Stream();
}
void close() {
delete stream;
}
~StreamHandler() {
free(_name);
// stream should be deleted here, not in close()
}
}