Questions tagged [resource-management]
142 questions
120
votes
10 answers
Understanding the meaning of the term and the concept - RAII (Resource Acquisition is Initialization)
Could you C++ developers please give us a good description of what RAII is, why it is important, and whether or not it might have any relevance to other languages?
I do know a little bit. I believe it stands for "Resource Acquisition is…

Charlie Flowers
- 17,338
- 10
- 71
- 88
105
votes
10 answers
What Automatic Resource Management alternatives exist for Scala?
I have seen many examples of ARM (automatic resource management) on the web for Scala. It seems to be a rite-of-passage to write one, though most look pretty much like one another. I did see a pretty cool example using continuations, though.
At any…

Daniel C. Sobral
- 295,120
- 86
- 501
- 681
52
votes
12 answers
Are Locks AutoCloseable?
Are Locks auto-closeable? That is, instead of:
Lock someLock = new ReentrantLock();
someLock.lock();
try
{
// ...
}
finally
{
someLock.unlock();
}
...can I say:
try (Lock someLock = new ReentrantLock())
{
someLock.lock();
//…

fredoverflow
- 256,549
- 94
- 388
- 662
46
votes
13 answers
Where to close java PreparedStatements and ResultSets?
Consider the code:
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.createStatement(myQueryString);
rs = ps.executeQuery();
// process the results...
} catch (java.sql.SQLException e) {
log.error("an error!", e);
throw new…

MCS
- 22,113
- 20
- 62
- 76
41
votes
5 answers
Is the Rule of 5 (for constructors and destructors) outdated?
The rule of 5 states that if a class has a user-declared destructor, copy constructor, copy assignment constructor, move constructor, or move assignment constructor, then it must have the other 4.
But today it dawned on me: when do you ever need a…

SomeProgrammer
- 1,134
- 1
- 6
- 12
27
votes
18 answers
Why free resources if the program is already quitting?
Many libraries like SDL, etc, etc have in their tutorials method calls that free resources right before quitting the program, but as far as I know, most OSes free all memory from the processes when they quit, why do I need to bother to free them if…
user216441
20
votes
6 answers
What happens if delete[] p fails?
Suppose I have a pointer to a dynamically allocated array of 10 elements:
T* p = new T[10];
Later, I want to release that array:
delete[] p;
What happens if one of the T destructors throws an exception? Do the other elements still get destructed?…

fredoverflow
- 256,549
- 94
- 388
- 662
20
votes
9 answers
Simple Scala pattern for "using/try-with-resources" (Automatic Resource Management)
C# has using with the IDisposable interface. Java 7+ has identical functionality with try and the AutoCloseable interface. Scala lets you choose your own implementation to this issue.
scala-arm seems to be the popular choice, and is maintained by…

clay
- 18,138
- 28
- 107
- 192
19
votes
4 answers
Why is use better than using?
According to the last sentence on this MSDN page use is to be preferred over using. I've heard it elsewhere (this answer, for example). Why is this? I realize use was added later. But what's the difference? On the surface, using seems more useful…

Daniel
- 47,404
- 11
- 101
- 179
18
votes
5 answers
Is it possible to prevent DoSing on Google App Engine?
I'm considering developing an app for Google App Engine, which should not get too much traffic. I'd really rather not pay to exceed the free quotas. However, it seems like it would be quite easy to cause a denial of service attack by overloading the…

Zifre
- 26,504
- 11
- 85
- 105
17
votes
8 answers
What wrapper class in C++ should I use for automated resource management?
I'm a C++ amateur. I'm writing some Win32 API code and there are handles and weirdly compositely allocated objects aplenty. So I was wondering - is there some wrapper class that would make resource management easier?
For example, when I want to load…

Vilx-
- 104,512
- 87
- 279
- 422
17
votes
5 answers
RAII in Java... is resource disposal always so ugly?
I just played with Java file system API, and came down with the following function, used to copy binary files. The original source came from the Web, but I added try/catch/finally clauses to be sure that, should something wrong happen, the Buffer…

paercebal
- 81,378
- 38
- 130
- 159
16
votes
3 answers
What does "opening a connection" actually mean?
I was trying to explain to someone why database connections implement IDisposable, when I realized I don't really know what "opening a connection" actually mean.
So my question is - What does c# practically do when it opens a connection?
Thank…

Oren A
- 5,870
- 6
- 43
- 64
15
votes
5 answers
Closing nested Reader
When reading from a text file, one typically creates a FileReader and then nests that in a BufferedReader. Which of the two readers should I close when I'm done reading? Does it matter?
FileReader fr = null;
BufferedReader br = null;
try
{
fr =…

fredoverflow
- 256,549
- 94
- 388
- 662
14
votes
9 answers
Handling IO exceptions in Java
Basically, I want to open a file, read some bytes, and then close the file. This is what I came up with:
try
{
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
try
{
// ...
…

fredoverflow
- 256,549
- 94
- 388
- 662