This code below is a part from main() in the apps/snmpget.c. I'm trying to 
copy something from this to my own project and I don't understand well one
thing: why you don't release the memory from "pdu" variable at the end?

Trying to add the line at the end ("snmp_free_pdu(pdu)") leads to crash 
here in snmpget and in my own program leads to "double free" from glibc.

Where is the memory released?

Thank you for answer

Tomas

-----------------------------------------------------------------------
...
...
     /*
      * Perform the request.
      *
      * If the Get Request fails, note the OID that caused the error,
      * "fix" the PDU (removing the error-prone OID) and retry.
      */
   retry:
     status = snmp_synch_response(ss, pdu, &response);
     if (status == STAT_SUCCESS) {
         if (response->errstat == SNMP_ERR_NOERROR) {
             for (vars = response->variables; vars;
                  vars = vars->next_variable)
                 print_variable(vars->name, vars->name_length, vars);

         } else {
             fprintf(stderr, "Error in packet\nReason: %s\n",
                     snmp_errstring(response->errstat));

             if (response->errindex != 0) {
                 fprintf(stderr, "Failed object: ");
                 for (count = 1, vars = response->variables;
                      vars && count != response->errindex;
                      vars = vars->next_variable, count++)
                     /*EMPTY*/;
                 if (vars) {
                     fprint_objid(stderr, vars->name, vars->name_length);
                 }
                 fprintf(stderr, "\n");
             }
             exitval = 2;

             /*
              * retry if the errored variable was successfully removed
              */
             if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
                                         NETSNMP_DS_APP_DONT_FIX_PDUS)) {
                 pdu = snmp_fix_pdu(response, SNMP_MSG_GET);
                 snmp_free_pdu(response);
                 response = NULL;
                 if (pdu != NULL) {
                     goto retry;
                 }
             }
         }                       /* endif -- SNMP_ERR_NOERROR */

     } else if (status == STAT_TIMEOUT) {
         fprintf(stderr, "Timeout: No Response from %s.\n",
                 session.peername);
         exitval = 1;

     } else {                    /* status == STAT_ERROR */
         snmp_sess_perror("snmpget", ss);
         exitval = 1;

     }                           /* endif -- STAT_SUCCESS */


     if (response)
         snmp_free_pdu(response);
     snmp_free_pdu(pdu); /* WHY IS THIS MISSING??? */
     snmp_close(ss);
     SOCK_CLEANUP;
     return exitval;

}                               /* end main() */

-----------------------------------------------------------------------

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to