raii

RAII in Java… is resource disposal always so ugly?

不想你离开。 提交于 2019-11-26 20:28:10
问题 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 Streams would be closed (and thus, my OS ressources freed) before quiting the function. I trimmed down the function to show the pattern: public static void copyFile(FileOutputStream oDStream, FileInputStream oSStream) throw etc... {

Using RAII to manage resources from a C-style API

柔情痞子 提交于 2019-11-26 20:23:50
问题 Resource Acquisition is Initialization (RAII) is commonly used in C++ to manage the lifetimes of resources which require some manner of cleanup code at the end of their lifetime, from delete ing new ed pointers to releasing file handles. How do I quickly and easily use RAII to manage the lifetime of a resource I acquire from a C-style API? In my case, I want to use RAII to automatically execute a cleanup function from a C-style API when the variable holding the C-style resource it releases

Does ScopeGuard use really lead to better code?

和自甴很熟 提交于 2019-11-26 19:09:03
问题 I came across this article written by Andrei Alexandrescu and Petru Marginean many years ago, which presents and discusses a utility class called ScopeGuard for writing exception-safe code. I'd like to know if coding with these objects truly leads to better code or if it obfuscates error handling, in that perhaps the guard's callback would be better presented in a catch block? Does anyone have any experience using these in actual production code? 回答1: It definitely improves your code. Your

General guidelines to avoid memory leaks in C++ [closed]

我只是一个虾纸丫 提交于 2019-11-26 14:50:32
What are some general tips to make sure I don't leak memory in C++ programs? How do I figure out who should free memory that has been dynamically allocated? Andri Möll Instead of managing memory manually, try to use smart pointers where applicable. Take a look at the Boost lib , TR1 , and smart pointers . Also smart pointers are now a part of C++ standard called C++11 . I thoroughly endorse all the advice about RAII and smart pointers, but I'd also like to add a slightly higher-level tip: the easiest memory to manage is the memory you never allocated. Unlike languages like C# and Java, where

RAII in Python - automatic destruction when leaving a scope

纵饮孤独 提交于 2019-11-26 13:06:41
问题 I\'ve been trying to find RAII in Python. Resource Allocation Is Initialization is a pattern in C++ whereby an object is initialized as it is created. If it fails, then it throws an exception. In this way, the programmer knows that the object will never be left in a half-constructed state. Python can do this much. But RAII also works with the scoping rules of C++ to ensure the prompt destruction of the object. As soon as the variable pops off the stack it is destroyed. This may happen in

Do I need to manually close an ifstream?

ぃ、小莉子 提交于 2019-11-26 11:09:31
Do I need to manually call close() when I use a std::ifstream ? For example, in the code: std::string readContentsOfFile(std::string fileName) { std::ifstream file(fileName.c_str()); if (file.good()) { std::stringstream buffer; buffer << file.rdbuf(); file.close(); return buffer.str(); } throw std::runtime_exception("file not found"); } Do I need to call file.close() manually? Shouldn't ifstream make use of RAII for closing files? Eclipse NO This is what RAII is for, let the destructor do its job. There is no harm in closing it manually, but it's not the C++ way, it's programming in C with

C/C++ macro/template blackmagic to generate unique name

前提是你 提交于 2019-11-26 10:15:30
问题 Macros are fine. Templates are fine. Pretty much whatever it works is fine. The example is OpenGL; but the technique is C++ specific and relies on no knowledge of OpenGL. Precise problem: I want an expression E; where I do not have to specify a unique name; such that a constructor is called where E is defined, and a destructor is called where the block E is in ends. For example, consider: class GlTranslate { GLTranslate(float x, float y, float z); { glPushMatrix(); glTranslatef(x, y, z); }

Is it abusive to use IDisposable and “using” as a means for getting “scoped behavior” for exception safety?

老子叫甜甜 提交于 2019-11-26 06:54:19
Something I often used back in C++ was letting a class A handle a state entry and exit condition for another class B , via the A constructor and destructor, to make sure that if something in that scope threw an exception, then B would have a known state when the scope was exited. This isn't pure RAII as far as the acronym goes, but it's an established pattern nevertheless. In C#, I often want to do class FrobbleManager { ... private void FiddleTheFrobble() { this.Frobble.Unlock(); Foo(); // Can throw this.Frobble.Fiddle(); // Can throw Bar(); // Can throw this.Frobble.Lock(); } } Which needs

General guidelines to avoid memory leaks in C++ [closed]

二次信任 提交于 2019-11-26 04:03:53
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . What are some general tips to make sure I don\'t leak memory in C++ programs? How do I figure out who should free memory that has been

Do I need to manually close an ifstream?

时光毁灭记忆、已成空白 提交于 2019-11-26 02:17:44
问题 Do I need to manually call close() when I use a std::ifstream ? For example, in the code: std::string readContentsOfFile(std::string fileName) { std::ifstream file(fileName.c_str()); if (file.good()) { std::stringstream buffer; buffer << file.rdbuf(); file.close(); return buffer.str(); } throw std::runtime_exception(\"file not found\"); } Do I need to call file.close() manually? Shouldn\'t ifstream make use of RAII for closing files? 回答1: NO This is what RAII is for, let the destructor do its