handle deallocate memory on return

A

alessio211734

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.
 
J

Joshua Maurice

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.
 
M

Marcel Müller

alessio211734 said:
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
 
T

Thomas J. Gritzan

Hendrik said:
Marcel said:
alessio211734 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top