Hello. Please help. I was writed the snmp-function . Function is good work but... memory leak :(
Why i have the memory leak in my program?
This my source code : i use the net-class for crossplatform threads ... http://netclass.sourceforge.net/
====================================================
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/utilities.h>
#include <net-snmp/mib_api.h>
#include <semaphore.h>
#include <netclass.h>
#include "func.h"


struct answ_snmp{
  bool an;
  unsigned long val;
  string val_ch;
  int flag;
};

struct p_arg{
  char ip[20];
  char comm[30];
  char ch_oid[128];
  oid oid_name[MAX_OID_LEN];
  size_t oid_len;
  snmp_session ss;
  snmp_session *sess;
  snmp_pdu *req;
  int i;
};

pthread_mutex_t job_mutex = PTHREAD_MUTEX_INITIALIZER;

vector<answ_snmp> th_answ;
//answ_snmp th_answ[1000];

int asynch_response(int operation, snmp_session *ss, int reqid, snmp_pdu *pdu, void* mg)
{
int cx = *(int *) mg;
if (operation == 1)
{
char buf[128];
netsnmp_variable_list *vp;
vp = pdu->variables;
if (pdu->errstat == SNMP_ERR_NOERROR)
{
while (vp)
{
snprint_value(buf, 128, vp->name, vp->name_length, vp);
th_answ[cx].val_ch += buf;
th_answ[cx].flag = 0;
vp = vp->next_variable;
}
}
else
{
th_answ[cx].val = 0;
th_answ[cx].flag = 1;
}
}
else
{
th_answ[cx].val = 0;
th_answ[cx].flag = 2;
// exit(1);
}
th_answ[cx].an = false;
SOCK_CLEANUP;
}


void asynchronous(p_arg *th_arg)
{
SOCK_STARTUP;
pthread_mutex_lock(&job_mutex);
if ( ! ( th_arg->sess = snmp_open ( & ( th_arg->ss ) ) ) )
{
th_answ[th_arg->i].flag = 2;
th_answ[th_arg->i].val = 0;
th_answ[th_arg->i].an = false;
SOCK_CLEANUP;
snmp_perror("snmp_open");
}
else
{
th_arg->req = snmp_pdu_create ( SNMP_MSG_GET ) ;
snmp_add_null_var ( th_arg->req, th_arg->oid_name, th_arg->oid_len );
if ( !snmp_send ( th_arg->sess, th_arg->req ) )
snmp_free_pdu ( th_arg->req );
}


while ( th_answ[th_arg->i].an )
{
int fds = 0, block = 1;
fd_set fdset;
timeval timeout;
FD_ZERO ( &fdset );
snmp_select_info ( &fds, &fdset, &timeout, &block );
fds = select ( fds, &fdset, NULL, NULL, block ? NULL : &timeout );
if(fds)
{
snmp_read ( &fdset );
}
else
{
snmp_timeout();
}
}
snmp_close(th_arg->sess);
pthread_mutex_unlock(&job_mutex);
}


ncCallBackRetType func_thread(void *par)
{
    p_arg *p = (p_arg*)par;
    asynchronous ( p );
}


struct dev{ string ip; string comm; string oid; };


extern "C" c_answ snmp_get ( vector<job> &jobs )
{
vector<dev> devs;
c_answ fez;
fez.count = 0;
for ( unsigned int i = 0 ; i < jobs.size() ; i++ )
{
dev dev_buf;
dev_buf.ip = jobs[i].param.substr ( 0, jobs[i].param.find_first_of ( '\n' ) );
dev_buf.comm = jobs[i].param.substr ( jobs[i].param.find_first_of ( '\n' ) + 1 ,
jobs[i].param.find_last_of ( '\n' ) - jobs[i].param.find_first_of ( '\n' ) - 1 );
dev_buf.oid = jobs[i].param.substr ( jobs[i].param.find_last_of ( '\n' ) + 1 ,
jobs[i].param.size() - jobs[i].param.find_last_of ( '\n' ) );
devs.push_back ( dev_buf );
}


try
{
cout << "init|";
c_answ ret;
//************intit********************************************************************************
init_snmp("asynchapp");
netClass nc;
ncThread *th_id;
p_arg *th_arg;
th_arg = new p_arg[600];
th_id = new ncThread[600];
vector<p_arg> th_args;
th_answ.clear();
//************base*********************************************************************************
cout << "parse|";
for ( int i = 0; i < devs.size() ; i++ )
{
answ_snmp buf_answ;
buf_answ.an = true;
buf_answ.val_ch = "";
buf_answ.val = 0;
buf_answ.flag = 0;
p_arg buf_arg;
bzero( th_arg[i].ch_oid, 128 );
strcpy ( th_arg[i].ch_oid, devs[i].oid.c_str() );
bzero ( th_arg[i].comm, 30 );
strcpy ( th_arg[i].comm, devs[i].comm.c_str() );
bzero ( th_arg[i].ip, 20 );
strcpy ( th_arg[i].ip, devs[i].ip.c_str() );
th_arg[i].oid_len = MAX_OID_LEN;
th_arg[i].i = i;
if ( !snmp_parse_oid ( th_arg[i].ch_oid, th_arg[i].oid_name, & ( th_arg[i].oid_len ) ) )
{
// snmp_perror(th_arg[i].ch_oid);
buf_answ.flag = 1;
}
if ( !check_ip ( th_arg[i].ip ) )
{
buf_answ.flag = 1;
}


            snmp_sess_init ( & ( th_arg[i].ss ) );
            th_arg[i].ss.version = SNMP_VERSION_1;
            th_arg[i].ss.timeout = 1 * 300000L;
            th_arg[i].ss.retries = 1;
            th_arg[i].ss.peername = th_arg[i].ip;
            th_arg[i].ss.community = (u_char*)th_arg[i].comm;
            th_arg[i].ss.community_len = strlen(th_arg[i].comm);
            th_arg[i].ss.callback = asynch_response;
            th_arg[i].ss.callback_magic = &(th_arg[i].i);

th_answ.push_back ( buf_answ );
}
//***********asynchapp****************************************************************************
cout << "thread start...|\n";
for ( unsigned int i = 0 ; i < devs.size() ; i++ )
{
if ( th_answ[i].flag == 0 )
{
// cout << th_arg[i].comm << ' ' << th_arg[i].ip << ' ' << th_arg[i].ch_oid << endl;
if ( th_id[i].start ( &func_thread, &th_arg[i] ) == NC_FAILED )
cout.flush() << "Thread " << i << " creation failed." << endl;
}
}
cout << "thread join...|";
for ( unsigned int i = 0 ; i < devs.size() ; i++ )
{
if ( th_answ[i].flag == 0 )
{
if ( th_id[i].join() == NC_FAILED)
cout.flush() << "Thread " << i << " join failed." << endl;
}
}
cout << "ok|parse rezult|" << endl;
for ( unsigned int i = 0; i < devs.size() ; i++ )
{
if ( th_answ[i].flag == 0 )
{
string buf;
buf = th_answ[i].val_ch.substr ( th_answ[i].val_ch.find_first_of ( ':' ) + 2, ( th_answ[i].val_ch.size() - th_answ[i].val_ch.find_first_of ( ':' ) ) );
th_answ[i].val = atoll ( buf.c_str() );
}
}
cout << "try new for ret|" << endl;
ret.a = new answ[devs.size()];
cout << "ok|convert to ret\n";
ret.count = devs.size();
for ( unsigned int i = 0 ; i < devs.size() ; i++ )
{
ret.a[i].id = jobs[i].id;
ret.a[i].flag = th_answ[i].flag;
ret.a[i].val = th_answ[i].val;
}
delete[] th_arg;
delete[] th_id;
th_answ.clear();
devs.clear();
return ret;
}
catch ( ... )
{
cerr << "Error ! In snmpget has Exception\n";
th_answ.clear();
devs.clear();
c_answ ret_on_err;
ret_on_err.count = 0;
return ret_on_err;
// exit(1);
}
}
===================================================
With Best Regards.



------------------------------------------------------- This SF.Net email is sponsored by BEA Weblogic Workshop FREE Java Enterprise J2EE developer tools! Get your free copy of BEA WebLogic Workshop 8.1 today. http://ads.osdn.com/?ad_idG21&alloc_id040&op=click _______________________________________________ Net-snmp-coders mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/net-snmp-coders
  • (n... paramita biswas
    • ... Dave Shield
    • ... Clyde Atillo
    • ... Вася Пупкин
    • ... Fatih Erikci
    • ... Goh, Yen Mei
    • ... Goh, Yen Mei
    • ... Goh, Yen Mei
      • ... Dave Shield
    • ... Goh, Yen Mei

Reply via email to