On 4/5/2012 5:36 AM, Sunjeet Singh wrote:
  ENGINE_free all of the structural references you got, including the one for 
the ENGINE you're using
And so in order to free all structural references of an ENGINE * e, I could 
just do this?-

while( e->struct_ref>  0 ) {
        ENGINE_free( e );
}

And to keep all but one functional reference to e, I could do this?-

while(e->funct_ref>  1 ) {
        ENGINE_finish( e );
}

I don't think there is any cost to having extra references (of either kind) to 
an ENGINE, as long as all of the references are freed when you are done.
Once again, free all functional references and structural references if any and 
then call the ENGINE_cleanup() function ?

No, you got the concept of reference counting all wrong!

You are not supposed to artificially remove references you did not
make, as that may crash the code that made that reference for its
own use.

The rules are much simpler:

If you store some kind of explicit pointer or handle to the engine
in your code, call the applicable ENGINE_ function to prevent other
code from unloading the engine while you use it.  When you remove
or zero out that pointer or handle, call the opposite ENGINE_
function so others can now free it when they no longer need it
either.  If your code happens to hold the last reference when
making the call, the unloading that was prevented by your reference
will happen at that time.

Therefore:

If someone else artificially decrements all the usage counts to 0
while you are using the pointer or handle, your code is likely to
crash when the engine disappears unexpectedly.

If someone else artificially decrements all the usage counts to 1
while two or more parts of your code are using pointers or handles
to the engine, then when one part of your code frees its reference,
it will unload the engine prematurely, thus crashing the other part
that was still using its reference.

Now swap the roles of you and someone else (the Golden Rule) and it
becomes clear that your proposed code is likely to cause some other
"random" piece of code to crash at some "random" time after you
decrement the counter to 1.

So don't do this.

--
Jakob Bohm, CIO, partner, WiseMo A/S. http://www.wisemo.com
Transformervej 29, 2730 Herlev, Denmark. direct: +45 31 13 16 10 <call:+4531131610>
This message is only for its intended recipient, delete if misaddressed.
WiseMo - Remote Service Management for PCs, Phones and Embedded
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to