Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > handle deallocate memory on return

Reply
Thread Tools

handle deallocate memory on return

 
 
alessio211734
Guest
Posts: n/a
 
      06-30-2009
I would like avoid to free memory every time that my function return.

I would like avoid to use goto c instruction to jump a code where I
call
EVP_PKEY_free(pkey_fsys);
EVP_PKEY_free(pkey_cert);
to free memory (openssl function)


My code:

if (!(EVP_PKEY_cmp_parameters(pkey_fsys,pkey_cert)))
{
if (pkey_fsys->type != pkey_cert->type)
{
printf("Public keys type mismatch\n");
m_pContext->sendError
(RSA_KEY_MAN_LOAD_CERT,BAD_PUBLIC_KEY);
EVP_PKEY_free(pkey_fsys);
EVP_PKEY_free(pkey_cert);
return;
}
if (BN_cmp(pkey_fsys->pkey.rsa->e,pkey_cert-
>pkey.rsa->n) != 0)

{
printf("Public keys exponent mismatch\n");
m_pContext->sendError
(RSA_KEY_MAN_LOAD_CERT,PUBLIC_EXP_MISMATCH);
EVP_PKEY_free(pkey_fsys);
EVP_PKEY_free(pkey_cert);
return;
}
if (BN_cmp(pkey_fsys->pkey.rsa->n,pkey_cert-
>pkey.rsa->n) != 0)

{
printf("Public keys modulus mismatch\n");
m_pContext->sendError
(RSA_KEY_MAN_LOAD_CERT,PUBLIC_MOD_MISMATCH);
EVP_PKEY_free(pkey_fsys);
EVP_PKEY_free(pkey_cert);
return;
}
}
EVP_PKEY_free(pkey_fsys);
EVP_PKEY_free(pkey_cert);
}



Please suggest me a better solution.
 
Reply With Quote
 
 
 
 
Joshua Maurice
Guest
Posts: n/a
 
      06-30-2009
On Jun 30, 1:25*am, alessio211734 <(E-Mail Removed)> wrote:
> I would like avoid to free memory every time that my function return.
>
> I would like avoid to use goto c instruction to jump a code where I
> call
> *EVP_PKEY_free(pkey_fsys);
> *EVP_PKEY_free(pkey_cert);
> to free memory (openssl function)
>
> My code:

[Snip]
> Please suggest me a better solution.


C++ was written for \expressly this\ (among other things).
Destructors. Here's a quick example. Cleaner alternatives exist, such
as the scope guard header (google is your friend here), but this
example shows the "guts" of how it works:

if (!(EVP_PKEY_cmp_parameters(pkey_fsys,pkey_cert)))
{
struct KeyOwner
{ KeyOwner(key_type key_) : key(key_) {}
~KeyOwner() { EVP_PKEY_free(key); }
key_type key;
};

KeyOwner pkey_fsys_owner(pkey_fsys);
KeyOwner pkey_cert_owner(pkey_cert)

// lots of if - returns, want to free keys

// scope exited normally, want to free keys
}

The KeyOwner objects will be destroyed when the scope is exited iff
they are created as they are stack variables. When they are destroyed,
they will free their owned key. The scope guard header gives you a
general purpose class and macro which accomplishes the same thing.
Boost shared pointer has similar functionality, though it incurs some
overhead (shared and threadsafe) whereas the above has no overhead.

Also, the general purpose solution really only shines with C++0x
closures, though the scope guard macro ON_BLOCK_EXIT does pretty well
with C++03 binders, and better still with Boost lambda library.

Also, you may want to consider wrapping such resources in a class
whose destructor will free it, then you can simply use auto_ptr
instead of KeyGuard or more "general purpose" solutions.
 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      06-30-2009
alessio211734 wrote:
> I would like avoid to free memory every time that my function return.
>
> I would like avoid to use goto c instruction to jump a code where I
> call
> EVP_PKEY_free(pkey_fsys);
> EVP_PKEY_free(pkey_cert);
> to free memory (openssl function)


Make your own smart pointer class by copying boost::scoped_ptr and replace
boost::checked_delete( px );
by
EVP_PKEY_free( px );

Unlike your implementation the result will be exception safe.

Unfortunately boost::scoped_ptr cannot be customized to use a custom
deleter, so you have to implement your own one.

In case you have argument defendant lookup you could override
checked_delete in the namespace of your pointer type to redirect the
delete call to EVP_PKEY_free. But this is not very obvious and may
violate the ODR in case you are not careful enough with the includes. I
am also unsure if it really works. So the custom smart pointer should be
preferred.


Marcel
 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      07-03-2009
Hendrik Schober schrieb:
> Marcel Müller wrote:
>> alessio211734 wrote:
>>> I would like avoid to free memory every time that my function return.
>>>
>>> I would like avoid to use goto c instruction to jump a code where I
>>> call
>>> EVP_PKEY_free(pkey_fsys);
>>> EVP_PKEY_free(pkey_cert);
>>> to free memory (openssl function)

>>
>> Make your own smart pointer class by copying boost::scoped_ptr [...]

>
> Not a good idea.
> (Note that you can pass a deleter to 'boost::shared_ptr'.)


But shared_ptr is quite expensive in comparision to scoped_ptr, and has
wrong (shared ownership) semantics. For a simple scope, shared_ptr is
unneccessary. In C++0x, we can use unique_ptr with a custom deleter.
Until that, a simple scope based smart pointer is not that hard.

-phy
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to deallocate memory for a tree structure. pereges C Programming 6 07-07-2008 10:02 AM
to deallocate the memory sugankanya86@gmail.com C++ 2 04-03-2008 11:56 AM
Python memory deallocate mariano.difelice@gmail.com Python 20 05-12-2006 01:14 PM
How to deallocate memory when handling exception Tony Johansson C++ 3 08-13-2005 09:55 PM
Using for_each() to deallocate memory Eric Lilja C++ 2 03-14-2005 02:51 PM



Advertisments