Kubyshev Andrey wrote:
> 
> Hello openssl-users,
> 
> suppose that i have following code :
> 
>           BIO * key=BIO_new(BIO_s_mem());
>           BUF_MEM * bm=BUF_MEM_new();
>           BIO_set_mem_buf(key, bm, 0);
>           BIO * b64=BIO_new(BIO_f_base64());
>           BIO * inp=BIO_push(b64,key);
> 
> now question : for what objects should i call free functions to
> have no memory leaks? For all that i created or only for "root" inp
> which consist all object ? i.e: whats correct :
> 
>       only BIO_free (inp);
> 
>       or:
> 
>       BIO_free (inp);
>       BIO_free (b64);
>       BIO_free (key);
>       BUF_MEM_free(bm);
> 

Neither :-)

If instead you do:

           BIO_set_mem_buf(key, bm, BIO_CLOSE);

then 'bm' will be freed when its BIO is freed. Also for a chain of BIOs
you need to call BIO_free_all(). So along with that:

BIO_free_all(inp);

is sufficient. The second way:

       BIO_free (inp);
       BIO_free (b64);
       BIO_free (key);
       BUF_MEM_free(bm);

is wrong because after BIO_push() inp==b64 and you'd end up freeing it
twice. However if you fix that then its also OK.

Steve.
-- 
Dr Stephen N. Henson.   http://www.drh-consultancy.demon.co.uk/
Personal Email: [EMAIL PROTECTED] 
Senior crypto engineer, Celo Communications: http://www.celocom.com/
Core developer of the   OpenSSL project: http://www.openssl.org/
Business Email: [EMAIL PROTECTED] PGP key: via homepage.
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to