[Date Prev][Date Next] [Chronological] [Thread] [Top]

memory leak



Hello everybody,

I'm developing a ldap application and I have noticed that my program
suffers a memory leak. I have minimized the code to a simply program
without errors treatment and so on. This are the ldap functions I use:

ld= ldap_init (ldapHost, atoi(ldapPort));
msgid = ldap_simple_bind_s (ld, radiusMgrDn, radiusMgrPw);
ldap_search_s (ld, basedn, LDAP_SCOPE_SUBTREE, filtro, NULL, 0 ,
&result);
numValues = ldap_count_entries (ld, result);
e = ldap_first_entry (ld, result);
ldapDatosRadius2 = ldap_get_values( ld, e, attrLdap_password);

And this are the parameters I free:

ldap_value_free (ldapDatosRadius);
ldap_value_free (ldapDatosRadius2);
ldap_unbind_s (ld);
/*ldap_msgfree (e);*/

(the complete main source code is shown at the end of the email)

I don't know exactly the variables I should free. I think I also should
free "e", but when I do it and I run the application, the program is
aborted and this error occurs:
$ ../_linux.O/prueba.a 
prueba.a: io.c:184: ber_free: Assertion
`((ber)->ber_opts.lbo_valid==0x2)' failed.
Aborted

Does anybody know which are the variables I should free and I don't??

Thank you very much in advantage and have a nice day.
ALICIA

--------------------------------------------------------------
main ()
{
/* INICIO DE PROGRAMA */

LDAP *ld;
LDAPMessage *result, *e;
BerElement *ber;
char **ldapDatosRadius;
char **ldapDatosRadius2;
/* Cada array puede tener 5 atributos de 25 cararcteres cada uno */
char nomAtributosRadius[5][25]={"User-Name", "Password",
"SESIONES-INFOVIA"};
char nomAtributosLdap[5][25]={"uid", "userpassword", "portlimit"};
char cadena [4 * 1024];
char filtro[256];
char buffer[1024];
char attrLdap_username[CADENA_MAXIMA]={"uid"};  /* Vble donde se
almacena el nombre del atributo Ldap para el caso de User-Name */
char attrLdap_password [CADENA_MAXIMA]={"userpassword"};        /* Vble
donde se almacena el nombre del atributo atributo Ldap en el caso de
Password */
int numValues; 
int numAtrib=3;                                         /* Numero de
atributos que se quieren del usuario buscado */
int k=1;
int cont, i, len, B, numIter;
int primer_atributo=1;
int msgid, rc;
struct timeval timeout;
/* Vbles que entraban por parametro en la libreria */
char loginUsuarioRadius[30]={"usuario1@ceaid2"};
char salida[4096];

/*Iniciamos bucle */
numIter = 5000;
for (B=1 ; B <= numIter ; B++)
{

/* Abrimos fichero de trazas */
logFile = fopen
("/home/redIP/LINBOX/DESARROLLO/PRUEBAS/ESQUELETO/trazas_prueba.txt",
"a");
fprintf (logFile, "\n\n\n TRAZAS INAUGURADAS (ITERACION=%d)....\n", B);

/*LDAP_INIT : CREA ESTRUCTURA LDAP */
fprintf (logFile, "******* ldap_init *******\n");
ld= ldap_init (ldapHost, atoi(ldapPort));

/*LDAP_SIMPLE_BIND: AUTENTICACION SIMPLE ASINCRONA (ldap_unbind hace
free de ld) */
fprintf (logFile, "******* ldap_simple_bind *******\n");
msgid = ldap_simple_bind_s (ld, radiusMgrDn, radiusMgrPw);

/* LDAP_SEARCH_S: REALIZA LA BUSQUEDA POR FILTRO (somos responsables de
liberar la memoria que puede reservar) */
/* Preparamos el filtro */
sprintf (filtro, "(uid=%s)", loginUsuarioRadius);
fprintf (logFile, "filtro: %s\n", filtro);

fprintf (logFile, "******* ldap_search_s *******\n");
ldap_search_s (ld, basedn, LDAP_SCOPE_SUBTREE, filtro, NULL, 0 ,
&result);

/*LDAP_COUNT_ENTRIES: NUM D ENRADAS ENCONTRADAS EN BUSQUEDA */
fprintf (logFile, "******* ldap_count_entries *******\n");
numValues = ldap_count_entries (ld, result);

/* LDAP_FIRST_ENTRY: CAPTURA DATOS DE PRIMERA ENTRADA */
fprintf (logFile, "******* ldap_first_entry *******\n");
e = ldap_first_entry (ld, result);

/*LDAP_GET_VALUES: MANEJAR VALORES DE ATRIBUTOS */
fprintf (logFile, "******* ldap_get_values *******\n");
ldapDatosRadius2 = ldap_get_values( ld, e, attrLdap_password);
if (ldapDatosRadius2 [0]!=NULL)
{
        sprintf (buffer, "Password=%s\n", ldapDatosRadius2[0]);
        strcpy (cadena, buffer);
        for (k=0; k<numAtrib; k++)
        {
                if ((strcmp(nomAtributosRadius [k], "Password") != 0) &&
(strcmp(nomAtributosRadius[k], "User-Name") != 0))
                {
                        fprintf (logFile, "******* ldap_get_value
*******\n");
                        ldapDatosRadius = ldap_get_values (ld, e,
nomAtributosLdap[k]);
                        if (ldapDatosRadius != NULL)
                        {
                                sprintf (buffer, "%s=%s",
nomAtributosRadius[k], ldapDatosRadius[0]);
                                /* Para quitarle los blancos al atributo
*/
                                cont = strlen (buffer) -1;
                                while (buffer [cont] == ' ')
                                {
                                        buffer [cont] = '\n';
                                        cont --;
                                }
                                if (primer_atributo == 1)
                                {
                                        strcat (cadena, buffer);
                                        primer_atributo = 0;
                                }
                                else
                                {
                                        strcat (cadena, ",");
                                        strcat (cadena, buffer);
                                }
                                cont = strlen (buffer);
for (i=1; ldapDatosRadius[i] != NULL; i++)
                                {
                                        fprintf (logFile, "Es un
atributo multi-value, ");
                                        sprintf (buffer, "%s=%s, ",
nomAtributosRadius[k], ldapDatosRadius[i]);
                                        strcat (cadena, buffer );
                                        fprintf (logFile, "NEW: Por
ahora la cadena es . %s", cadena);
                                }
                        }
                }
        }
        len = strlen(cadena);
        strcpy (salida, cadena);
        fprintf (logFile, "\nEl vector de respuesta se ha rellenado:
%s", salida);
        }

fprintf (logFile, "******* ldap_value_free *******\n");
ldap_value_free (ldapDatosRadius);
fprintf (logFile, "******* ldap_values_free *******\n");
ldap_value_free (ldapDatosRadius2);
fprintf (logFile, "******* ldap_msgfree *******\n");
ldap_msgfree (result);
fprintf (logFile, "******* ldap_msgfree *******\n");
/*ldap_msgfree (e);*/
fprintf (logFile, "Se ha cerrado la conexion\n");
ldap_unbind_s (ld);
fprintf (logFile, "Ahora devolveriamos al radius : %s\n", salida);

if (B==numIter)
        fprintf (logFile, "---->>  SALGO DEL PROGRAMA POR MI PROPIO
PIE!!\n");
/* Cerramos el fichero de trazas */
fclose (logFile);

}
/* fin del bucle*/


/*FIN DEL PROGRAMA */
}
----------------------------------------------------------------------