473,322 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

releasing resources allocated for a particular object

For example, I have a BITMAPINFO* and a BYTE* object to release the
resources. Would the most bulletproof way of doing so involve checking
to see if they are null, then if they are not, invoke a free()
function call on that and make sure that they point to null?
Aug 26 '08 #1
3 1465
ssylee wrote:
For example, I have a BITMAPINFO* and a BYTE* object to release the
resources. Would the most bulletproof way of doing so involve checking
to see if they are null, then if they are not, invoke a free()
function call on that and make sure that they point to null?
Hmm... start with "For example"?

If this is C++ typically you would use "delete" and you can pass a null to the
delete operator.

The best practice is to make a certain component responsible for freeing memory
for certain objects. So resetting a freed pointer variable to NULL to safeguard
from possible multiple freeing of memory clearly is not a good programming practice.
Aug 27 '08 #2
Hello ssylee,
For example, I have a BITMAPINFO* and a BYTE* object to release the
resources. Would the most bulletproof way of doing so involve checking
to see if they are null, then if they are not, invoke a free()
function call on that and make sure that they point to null?
The C++ way is probably that you wrap the resource in the class, and then
release it in the destructor.

free() is more traditional class c way of doing things.
Aug 27 '08 #3
ssylee <st********@gmail.comkirjutas:
For example, I have a BITMAPINFO* and a BYTE* object to release the
resources. Would the most bulletproof way of doing so involve checking
to see if they are null, then if they are not, invoke a free()
function call on that and make sure that they point to null?
In case of C-style interfaces which require releasing of resources I have
found Alexandrescu's ScopeGuard quite convenient (look it up in net). The
usage goes like this (for malloc()/free() it is very similar):

#include <ScopeGuard/ScopeGuard.h>
// ...
// call some C-style interface
addrinfo* res;
int k =getaddrinfo(server.c_str(), str(port).c_str(), NULL, &res);

// check the result; if failed, just return (the resource is
// not allocated and thus there is no need to release it):
if (k!=0) {
throw std::runtime_error("Cannot resolve server name");
}

// Set up a ScopeGuard which will release
// the resource when exiting the function.
// The ON_BLOCK_EXIT macro is part of the ScopeGuard utility.
ON_BLOCK_EXIT(freeaddrinfo, res);

// rest of the function...

Note that by releasing there is no need to check that the resource handle
is valid as it has been checked already before, neither is there any need
for setting the resource handle to NULL or anything else.

hth
Paavo
Aug 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: lebo | last post by:
Hi I'm trying to understand how Python handles memory usage and dynamic object loading and unloading. Problem to solve? Build a very low memory footprint (non-GUI) Python application for...
4
by: mairhtin o'feannag | last post by:
Hello, I have a tablespace striped across three drives, call them 1,2,3, just to be clever. :) I allocated a lot more space (DMS) than I should have, since I didn't know a way to estimate the...
5
by: Mark Rae | last post by:
Hi, I'm encountering a strange phenomenon whereby a DataSet object is not releasing its memory when it's being disposed and/or set to Nothing. It is part of a Windows service written in VB.NET...
5
by: Michael Tissington | last post by:
I have an application written in C++ (NOT .NET) which creates an instance of a vb .net object. My application releases the .net object when it has finished using it. However .NET GC is not...
2
by: Pete Smith | last post by:
I have written print application which prints multiple pages by calling PrintPage event multiple times. I am closing all the databases related objects resources when it is not required. Since...
3
by: DaTurk | last post by:
Hi, I'm implementing the Idisposable pattern and am wondering what the difference it between managed, and unmanaged resources? http://msdn2.microsoft.com/en-us/library/fs2xkftw.aspx in the...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
12
by: iker.arizmendi | last post by:
Is there any way to get Python to release memory back to the C allocator? I'm currently running a script that goes through the following steps: 1) Creates a very large number of Python objects...
0
by: 062699 | last post by:
Hi All, Thank you in advance. I am having trouble releasing resources when I open a child form. Please see the code below. Form f; private void button1_Click(object sender, EventArgs e) { ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.