Hello,
during writing a netsnmp subagent I noticed my code had a memory leak. I
tracked it down to a memory leak in netsnmp.
My code has to create and release entries in the SNMP tree and it seems that
function netsnmp_unregister_handler() does not free all resources allocated
during handler creation.
I attached a programm which produce the memory leak. Just compile it with
> gcc -I. `net-snmp-config --cflags` -c -o test.o test.c
> gcc -o test test.o `net-snmp-config --agent-libs`
and test the memory leak with
> valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./test
( the programm has an infinite loop, just stop it with ctrl-c to end the loop)
Does someone know what triggers this problem ?
Henning Rogge
*************************************************
Diplom Informatiker Henning Rogge
Forschungsgesellschaft für
Angewandte Naturwissenschaften e. V. (FGAN)
Neuenahrer Str. 20, 53343 Wachtberg, Germany
Tel.: 0049 (0)228 9435-961
Fax: 0049 (0)228 9435-685
E-Mail: [EMAIL PROTECTED]
Web: www.fgan.de
************************************************
Sitz der Gesellschaft: Bonn
Registergericht: Amtsgericht Bonn VR 2530
Vorstand: Dr. rer. nat. Ralf Dornhaus (Vors.), Prof. Dr. Joachim Ender
(Stellv.)
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <signal.h>
#include <stdio.h>
int keep_running;
RETSIGTYPE
stop_server(int a) {
keep_running=0;
}
static
netsnmp_handler_registration *
getreg(const char *name,
const char *ourname,
oid * reg_oid, size_t reg_oid_len,
void *it,
int modes,
Netsnmp_Node_Handler * scalarh, Netsnmp_Node_Handler * subhandler,
const char *contextName)
{
netsnmp_handler_registration *myreg;
netsnmp_mib_handler *myhandler;
if (subhandler) {
myreg =
netsnmp_create_handler_registration(name,
subhandler,
reg_oid, reg_oid_len,
modes);
myhandler = netsnmp_create_handler(ourname, scalarh);
myhandler->myvoid = (void *) it;
netsnmp_inject_handler(myreg, myhandler);
} else {
myreg =
netsnmp_create_handler_registration(name,
scalarh,
reg_oid, reg_oid_len,
modes);
myreg->handler->myvoid = (void *) it;
}
if (contextName)
myreg->contextName = strdup(contextName);
return myreg;
}
netsnmp_handler_registration
*register_int_instance(const char *name,
oid * reg_oid, size_t reg_oid_len,
int *it, Netsnmp_Node_Handler * subhandler)
{
netsnmp_handler_registration *myreg;
myreg = getreg(name, "int_handler", reg_oid, reg_oid_len, it,
HANDLER_CAN_RWRITE, netsnmp_instance_int_handler,
subhandler, NULL);
if(netsnmp_register_instance(myreg)) {
return NULL;
}
return myreg;
}
static int testInt = 0;
int main(int argc, char **argv) {
snmp_enable_stderrlog();
/* make us a agentx client. */
netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_X_SOCKET, "tcp:localhost:705");
/* initialize tcpip, if necessary */
SOCK_STARTUP;
/* initialize the agent library */
init_agent("test");
init_snmp("test");
oid oidBase[] = { 1, 3, 6, 1, 4, 1, 8072, 2, 4, 1, 1, 2, 4, 0 };
int count = 0;
keep_running = 1;
signal(SIGTERM, stop_server);
signal(SIGINT, stop_server);
/* your main loop here... */
while (keep_running) {
int fds = 0, block = 0;
fd_set fdset;
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 10000;
FD_ZERO(&fdset);
snmp_select_info(&fds, &fdset, &timeout, &block);
fds = select(fds, &fdset, NULL, NULL, block ? NULL : &timeout);
if (fds < 0) {
break;
}
if (fds > 0) {
snmp_read(&fdset);
} else {
snmp_timeout();
}
oidBase[OID_LENGTH(oidBase)-1]++;
netsnmp_handler_registration *reg = register_int_instance("test",
oidBase,
OID_LENGTH(oidBase),
&testInt, NULL);
if (count++ % 100 == 0) {
printf("%d\n", count);
}
netsnmp_unregister_handler(reg);
}
/* at shutdown time */
snmp_shutdown("test");
SOCK_CLEANUP;
return 0;
}
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders