raii Questions
18
Solved
Most people say never throw an exception out of a destructor - doing so results in undefined behavior. Stroustrup makes the point that "the vector destructor explicitly invokes the destructor for e...
Ageless asked 24/9, 2008 at 21:34
6
Solved
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 th...
Aborigine asked 21/2, 2011 at 20:50
2
Solved
I have a class called MutexLock, which does as it sounds : it locks a mutex on construction, and releases it upon destruction:
class OpenEXRMutexLock
{
#ifndef HAVE_PTHREADS
public:
OpenEXRMut...
Fever asked 25/3, 2015 at 9:23
5
Solved
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...
Puissant asked 10/3, 2010 at 18:53
2
Is there any RAII guard similar to std::lock_guard, std::scoped_lock or std::unique_lock provided by the standard which I can use in combination with std::binary_semaphore and std::counting_semapho...
3
Solved
I am new to Python. I come from C++.
In some code reviews, I've had several peers wanting me to move things from __init__ and __del__ to a start and stop method. Most of the time, this goes against...
4
Solved
I have a class that is using RAII for cleanup in case something goes wrong. This means the class contains a flag, that tells it whether the work has been completed, and if this flag is not set when...
Leper asked 1/4, 2011 at 9:34
5
Solved
Coming from a C++ background, I am a huge fan of the RAII pattern. I have used it extensively to handle memory management and lock management along with other use cases.
With Java 1.7 I see that i...
Jehoshaphat asked 18/4, 2015 at 2:54
4
Solved
I have a threaded class from which I would like to occasionally acquire a pointer an instance variable. I would like this access to be guarded by a mutex so that the thread is blocked from accessin...
Wrest asked 8/4, 2013 at 10:1
10
What is meant by Resource Acquisition is Initialization (RAII)?
9
Solved
General idea of Golang-style defer is explained here and here.
I wonder, does STL (of C++11, C++14, ...) or maybe Boost or maybe some other library contain implementation of such a class? So I cou...
4
Solved
I'm new to C++, I have a class hold some memory, the class looks like:
class MyClass
{
public:
MyClass (int s)
{
if (s <= 0) _size = 1;
else _size = s;
data = new int[_size]; // may throw ...
Amick asked 20/5, 2021 at 7:13
5
Solved
What is the OCaml counterpart to Python's "with"-statement?
with open('test.txt', 'r') as f:
# Do stuff with f
# At this point, f will always be closed, even in case of exceptions
That is: What...
Nostrum asked 11/6, 2018 at 9:3
1
Solved
It seems that in C++20 something called a "prospective destructor" was introduced.
In C++17 [class.dtor]:
In a declaration of a destructor, the declarator is a function declarator (11.3...
Cuckoopint asked 4/2, 2021 at 23:57
5
Solved
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.
...
Interferometer asked 26/12, 2020 at 10:29
8
Solved
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 lik...
Paronym asked 7/9, 2008 at 18:20
4
Solved
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 m...
5
Our Python codebase has metrics-related code that looks like this:
class Timer:
def __enter__(self, name):
self.name = name
self.start = time.time()
def __exit__(self):
elapsed = time.time()...
Precipitate asked 12/10, 2015 at 19:17
2
Solved
I wrote this code in order to loop recursively through a folder tree and list files with their size in bytes.
Since I am using winapi and there is a Handle that should be opened and closed, I shoul...
Tailspin asked 27/6, 2020 at 21:25
2
Solved
Consider the following program:
#include <functional>
#include <iostream>
class RvoObj {
public:
RvoObj(int x) : x_{x} {}
RvoObj(const RvoObj& obj) : x_{obj.x_} { std::co...
Thapsus asked 29/1, 2020 at 21:11
4
Solved
I have many LoadLibrary in my project, and need to call FreeLibrary manually for each LoadLibrary. I want to use the std::unique_ptr with specific deleter to make it auto release my dll resource.
...
4
Solved
My general question is what techniques can I use to ensure that resources are cleaned up/released in Javascript? Currently, I am taking the C (without goto) approach of finding every execution path...
Moralez asked 27/7, 2012 at 18:52
2
Solved
I've read What are non-lexical lifetimes?. With the non-lexical borrow checker, the following code compiles:
fn main() {
let mut scores = vec![1, 2, 3];
let score = &scores[0]; // borrows `s...
Garlic asked 12/8, 2019 at 19:59
8
Solved
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...
Algebra asked 12/3, 2010 at 14:11
3
C++ classes provide RAII idiom. Therefore you don't have to care about exceptions:
void function()
{
// The memory will be freed automatically on function exit
std::vector<int> vector(1000...
Simsar asked 17/2, 2018 at 15:17
1 Next >
© 2022 - 2024 — McMap. All rights reserved.