Questions tagged [raii]
Resource Acquisition Is Initialization (RAII) is a common idiom used in C++ to manage the lifetime of resources, including memory allocations, file handles or database connections. In brief, every resource should be wrapped in an owning class, whose lifetime controls the lifetime of the resource.
raii
609
questions
-2
votes
1
answer
37
views
Is it possible to implement RAII with Python in the context of ensuring that the close() function is called on opened files?
I asked a question earlier today, Is it important to call close() on an file opened with open() if flush() is called after each write() operation?
I asked this question while thinking about data loss, ...
0
votes
1
answer
38
views
Is it important to call `close()` on an file opened with `open()` if `flush()` is called after each write() operation?
I have read that it is important to call close() on a file which has been opened with open(filename, 'w'|'a') because otherwise changes made to the opened file may not be persisted. I believe this is ...
-1
votes
1
answer
67
views
White square instead of an actual image
I started to learn SFML recently through a book and came up with the problem that when you load an image you get a big white square instead.
I have class TextureHolder for managing game resources, ...
1
vote
2
answers
114
views
Is it smart to use RAII to manage files?
I wanted to create a class Channel that facilitates file writes in a special way. It was clear to me at first that I call open() on the file in the constructor and close() on the file descriptor in ...
0
votes
0
answers
32
views
How to examine whether ERESOURCE is null before deleting it?
When I want to create a C++ RAII wrappers for executive resources,I've come across this issue.
#include <ntddk.h>
class ExecutiveResource
{
public:
ExecutiveResource()
{
...
1
vote
0
answers
73
views
How to write a modern c++ program (with RAII) using WINAPI, of 4 threads with ids 1-4, looping 1000 times and printing theirs ids and iteration number
The task is to write a c++ program that creates 4 threads, that each of them loops a 1000 times, and each time prints his thread-id and the loop count. All 4 threads work separately.
This program ...
0
votes
0
answers
67
views
Proper way of handling c++ library resource/context management
In the context of cpp-code interacting with a c libraries, I come across the following types of functions.
AcquireResource, ReleaseResource
GetList, SubmitList
open, close
Initialize, Finalize
Or in ...
1
vote
1
answer
300
views
Defer a lambda in C++23
I like using the latest C++ for personal projects. I was wondering how I can defer lambdas (or use a scopeguard) in the latest C++
Below is a quick implementation. I like style 3 the best. Does the ...
0
votes
0
answers
70
views
RAII in pybind11 does not call destructor when added calllbacks
Here is the pybind11 code, which defined a Stream class and exported it to python3.
#include <functional>
#include <iostream>
#include <memory>
#include <pybind11/detail/common.h&...
0
votes
0
answers
40
views
How do I convert Python context manager to Rust? [duplicate]
I have this Python code from an existing project that I'm trying to port to Rust as a learning exercise (plus a thing I actually have a use for if I succeed):
class BaseKlazz:
# stuff
# from ...
2
votes
1
answer
173
views
Is Qt incompatible with RAII design patterns?
An important rule of thumb for good coding practices is
every new must be matched by a delete
discussed in this previous question. Usually, we encapsulate this rule in an RAII class like std::...
0
votes
2
answers
110
views
How to achieve RAII with #pragma's?
Currently, I have the following piece of code:
enum class Letters {
A,
B,
C
};
#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wswitch"
Letters e(Letters::A);...
3
votes
1
answer
88
views
Why adding a defaulted move assignment operator breaks compilation of standard swap function?
In the following code, if move assignment is uncommented the swap function stops compilation of the program. I observed this behavior on all 3 major compilers (GCC, Clang, MSVC).
#include <utility&...
-1
votes
1
answer
307
views
Vulkan-hpp m_dispatcher->getVkHeaderVersion() != VK_HEADER_VERSION
i'm trying to use vulkan raii and create an Instance (and in fact instantiation a vk::raii::Context)
i got this error
/usr/include/vulkan/vulkan_raii.hpp:2660: const vk::raii::ContextDispatcher* vk::...
1
vote
1
answer
54
views
RAII style guard with Rust gives invalid lifetime errors
I am writing a lexer and want to advance/backup a cursor using RAII helpers. I have a string that is indexed by the CharStreamGuard type that acts as the cursor. It is created at the top-level with ...