raii

Does Java support RAII/deterministic destruction?

天涯浪子 提交于 2019-11-28 04:39:55
It's been at least 5 years since I worked with Java, and back then, any time you wanted to allocate an object that needed cleaning up (e.g. sockets, DB handles), you had to remember to add a finally block and call the cleanup method in there. By contrast, in C++ (or other languages where object lifetimes are deterministic, e.g. Perl), the class implementor would define a destructor function that performs the cleanup whenever an object of that class goes out of scope. The advantage of this approach is that the user of the object can't forget to clean it up -- the destructor gets called

Does PHP support the RAII pattern? How?

痴心易碎 提交于 2019-11-28 03:02:08
问题 Most resources on PHP never touch memory management because the language itself is pretty good at doing this for you. However, in PHP you often end up dealing with external resources which aren't memory -- database handles, sessions, database transactions, etc. These external resources could be managed most cleanly using some form of RAII object. I initially thought that PHP used a garbage collection scheme similar to the JVM or the CLR, where the concept of a destructor does not exist.

Do boost asio sockets have proper RAII cleanup

喜你入骨 提交于 2019-11-28 00:44:23
问题 I tried looking through source but I cant navigate that much of a template code. Basically: this is what documentation says (for close() ): Remarks For portable behaviour with respect to graceful closure of a connected socket, call shutdown() before closing the socket. I can do that manually, but if possible it would be nice to rely on RAII. So if I have socket going out of scope do I need to call shutdown() and close() on it, or it will be done automatically? 回答1: One can rely on the socket

RAII in Java… is resource disposal always so ugly?

扶醉桌前 提交于 2019-11-27 20:40:53
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... { BufferedInputStream oSBuffer = new BufferedInputStream(oSStream, 4096); BufferedOutputStream oDBuffer = new

Using RAII to manage resources from a C-style API

社会主义新天地 提交于 2019-11-27 20:34:02
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 goes out of scope. I don't really need additional resource wrapping beyond that, and I'd like to minimize

RAII wrapper for OpenGL objects

断了今生、忘了曾经 提交于 2019-11-27 19:09:36
I want to write a simple RAII wrapper for OpenGL objects (textures, frame buffers, etc.) I have noticed, that all glGen* and glDelete* functions share the same signature, so my first attempt was like this: typedef void (__stdcall *GLGenFunction)(GLsizei, GLuint *); typedef void (__stdcall *GLDelFunction)(GLsizei, const GLuint *); template <GLGenFunction glGenFunction, GLDelFunction glDelFunction> class GLObject { GLuint m_name; public: GLObject() { glGenFunction(1, &m_name); } ~GLObject() { glDelFunction(1, &m_name); } GLuint getName() {return m_name;} }; typedef GLObject<glGenTextures,

Why is there no RAII in .NET?

坚强是说给别人听的谎言 提交于 2019-11-27 17:47:35
Being primarily a C++ developer the absence of RAII (Resource Acquisition Is Initialization) in Java and .NET has always bothered me. The fact that the onus of cleaning up is moved from the class writer to its consumer (by means of try finally or .NET's using construct ) seems to be markedly inferior. I see why in Java there is no support for RAII since all objects are located on the heap and the garbage collector inherently doesn't support deterministic destruction, but in .NET with the introduction of value-types ( struct ) we have the (seemingly) perfect candidate for RAII. A value type

Does ScopeGuard use really lead to better code?

半腔热情 提交于 2019-11-27 17:43:04
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? Konrad Rudolph It definitely improves your code. Your tentatively formulated claim, that it's obscure and that code would merit from a catch block is

Is there a proper 'ownership-in-a-package' for 'handles' available?

不问归期 提交于 2019-11-27 16:14:26
Handles have proper semantics other than pointers. So for me an example like this (extracted from the Rule of Zero ): class module { public: explicit module(std::wstring const& name) : handle { ::LoadLibrary(name.c_str()), &::FreeLibrary } {} // other module related functions go here private: using module_handle = std::unique_ptr<void, decltype(&::FreeLibrary)>; module_handle handle; }; using unique_ptr as an 'ownership-in-a-package' for handles is a bad example. First, it makes use of internal knowledge that the handle is a pointer type, and use this to make a unique_ptr to the basic type the

Any risk in a AutoCloseable wrapper for java.util.concurrent.locks.Lock?

╄→гoц情女王★ 提交于 2019-11-27 14:38:35
问题 With try-with-resource introduced in Java 7, I was surprised to see that that the Lock has not been retrofitted to be an AutoCloseable. It seemed fairly simple, so I have added it myself as follows: class Lock implements AutoCloseable { private final java.util.concurrent.locks.Lock _lock; Lock(java.util.concurrent.locks.Lock lock) { _lock = lock; _lock.lock(); } @Override public void close() { _lock.unlock(); } } This works with an AutoCloseableReentrantReadWiteLock class and usage is as