Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Memory leak
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==RAII== {{Main article|Resource acquisition is initialization}} [[Resource acquisition is initialization]] (RAII) is an approach to the problem commonly taken in [[C++]], [[D programming language|D]], and [[Ada (programming language)|Ada]]. It involves associating scoped objects with the acquired resources, and automatically releasing the resources once the objects are out of scope. Unlike garbage collection, RAII has the advantage of knowing when objects exist and when they do not. Compare the following C and C++ examples: <syntaxhighlight lang="c"> /* C version */ #include <stdlib.h> void f(int n) { int* array = calloc(n, sizeof(int)); do_some_work(array); free(array); } </syntaxhighlight> <syntaxhighlight lang="cpp"> // C++ version #include <vector> void f(int n) { std::vector<int> array (n); do_some_work(array); } </syntaxhighlight> The C version, as implemented in the example, requires explicit deallocation; the array is [[Dynamic memory allocation|dynamically allocated]] (from the heap in most C implementations), and continues to exist until explicitly freed. The C++ version requires no explicit deallocation; it will always occur automatically as soon as the object <code>array</code> goes out of scope, including if an exception is thrown. This avoids some of the overhead of [[garbage collection (computer science)|garbage collection]] schemes. And because object destructors can free resources other than memory, RAII helps to prevent the [[Handle leak|leaking of input and output resources accessed through a handle]], which mark-and-sweep garbage collection does not handle gracefully. These include open files, open windows, user notifications, objects in a graphics drawing library, thread synchronisation primitives such as critical sections, network connections, and connections to the [[Windows Registry]] or another database. However, using RAII correctly is not always easy and has its own pitfalls. For instance, if one is not careful, it is possible to create [[dangling pointer]]s (or references) by returning data by reference, only to have that data be deleted when its containing object goes out of scope. [[D (programming language)|D]] uses a combination of RAII and garbage collection, employing automatic destruction when it is clear that an object cannot be accessed outside its original scope, and garbage collection otherwise.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Memory leak
(section)
Add topic