On Tue, Sep 13, 2005 at 09:34:03AM +0100, Dave Shield wrote:
> 
> I'd suggest that, as a basic rule of thumb, objects should
> be owned by the code module that created them.
>
> If memory is allocated by a library routine as part of creating
> or processing something, then that section of the library should
> be responsible for releasing it again (either automatically as
> part of tidying up, or via a suitable free API call).
> 
> If memory is allocated and passed in from "outside", then
> "outside" should be responsible for releasing it again.
> 
> There may well be exceptions to this basic model (in either
> direction), but they should be clearly documented as such.
> Keeping allocation and release together seems the most natural
> and balanced structure, IMO.

I fully agree with this.

Then I would like to know if the following is one of the exceptions.

Assúme the following subagent foo.c, I expect the remarks to be true:

    1   #include <net-snmp/net-snmp-config.h>
    2   #include <net-snmp/net-snmp-includes.h>
    3   #include <net-snmp/agent/net-snmp-agent-includes.h>
    4
    5   void
    6   init_foo(void)
    7   {
    8     oid tree[] = { 1, 3, 6, 1, 3, 1 };
    9     netsnmp_handler_registration* reg =
   10       netsnmp_create_handler_registration("Test", NULL,
   11                                           tree, OID_LENGTH(tree),
   12                                           HANDLER_CAN_RWRITE);

Here I have created the registration.

   13     netsnmp_register_handler(reg);

Here I have registered the handler.

   14     netsnmp_unregister_handler(reg);

Here I have unregistered the handler but it is still valid so I coud
reregister it.

   15     netsnmp_handler_registration_free(reg);

Here I have destroyed the registration.

   16   }

Sadly this is not the case in the current code.

netsnmp_unregister_handler calls unregister_mib_context with arguments
taken from reg.
This is quite OK.

unregister_mib_context in turn calls netsnmp_subtree_free on the
netsnmp_subtree structure that corresponds to the registration.
This is also OK.

netsnmp_subtree_free in turn calls netsnmp_handler_registration_free on a
reference reg that is embedded in netsnmp_subtree struct.
This is not OK from the above point of view, unless this is one of the other
cases.

Then unregister_mib_context goes on to use it's arguments, that came from reg,
and dereference them to send into snmp_call_callbacks.
This is an dereference of freed memory if the previous case was OK.

I think that the right thing to do is to remove the call to
netsnmp_handler_registration_free from netsnmp_subtree_free but unfortunateley
this introduces at least one memory leak, possibly more.

/MF


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server.
Download it for free - -and be entered to win a 42" plasma tv or your very
own Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Net-snmp-coders mailing list
Net-snmp-coders@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to