Skip to main content

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
-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, ...
FreelanceConsultant's user avatar
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 ...
FreelanceConsultant's user avatar
-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, ...
Davo's user avatar
  • 3
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 ...
glades's user avatar
  • 4,445
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() { ...
Vecljox's user avatar
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 ...
Torch's user avatar
  • 11
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 ...
Sam Coutteau's user avatar
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 ...
Andrew Spar's user avatar
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&...
westdefeat's user avatar
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 ...
404 Name Not Found's user avatar
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::...
Bobby's user avatar
  • 75
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);...
neutrion's user avatar
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&...
Xeverous's user avatar
  • 1,016
-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::...
Amir Khaki's user avatar
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 ...
user1055947's user avatar

15 30 50 per page
1
2 3 4 5
41