handle deallocate memory on return

Discussion in 'C++' started by alessio211734, Jun 30, 2009.

  1. 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.
     
    alessio211734, Jun 30, 2009
    #1
    1. Advertising

  2. On Jun 30, 1:25 am, 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)
    >
    > 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.
     
    Joshua Maurice, Jun 30, 2009
    #2
    1. Advertising

  3. 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
     
    Marcel Müller, Jun 30, 2009
    #3
  4. 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
     
    Thomas J. Gritzan, Jul 3, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Lilja
    Replies:
    2
    Views:
    3,633
    Victor Bazarov
    Mar 14, 2005
  2. Replies:
    20
    Views:
    1,375
    Heiko Wundram
    May 12, 2006
  3. Tony Johansson
    Replies:
    3
    Views:
    332
    Old Wolf
    Aug 13, 2005
  4. Replies:
    2
    Views:
    356
    Ron Natalie
    Apr 3, 2008
  5. pereges
    Replies:
    6
    Views:
    309
    santosh
    Jul 7, 2008
Loading...

Share This Page