By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,089 Members | 1,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,089 IT Pros & Developers. It's quick & easy.

releasing resources allocated for a particular object

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.