SQLConnectDriver Memory Leak

  • Hello,

    I hava an application that uses ODBC SqlConnectDriver to conect to SQLServer 2000 sp4. When It connects to the server increases my memory around 1MB, and when It disconnect from the server, my aplication memory doesn't decrease. Is there any solution to that memory leak in ODBC?

    thanks.

  • Are you explicitly closing the connection and cleaning up the objects to be sure they are gone? COuld you post an example of how you make the connection an how you end it?

    And whn you say your memory are you talking a local machin where the server is not running or at the server? Is a particular item in memory increasing or just memory in general? And finally is it jumping 1MB each time you connect and then disconnect not going down, such so that it you run 50 times it increases about 50 MB and when closed it does not free?

  • Example:

    ret = SQLAllocHandle (SQL_HANDLE_DBC, henv1, &hdbc1);

     if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)

     {   

      if (ret == SQL_ERROR || ret == SQL_INVALID_HANDLE)

      {

       hdbc1=NULL;

       return FALSE;

      }

     } 

     sprintf (ConnStrIn,

       "DRIVER={SQL Server};SERVER=%s;UID=%s;PWD=%s;DATABASE=%s;",

       ServerName,

       UserName,

       Authentication,

       BDName);

     

     ret = SQLDriverConnect(hdbc1,      // Connection handle

           NULL,         // Window handle

           ConnStrIn,      // Input connect string

           SQL_NTS,         // Null-terminated string

           ConnStrOut,      // Address of output buffer

           MAX_PATH,      // Size of output buffer

           &cbConnStrOut,   // Address of output length

           SQL_DRIVER_NOPROMPT);

     if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)

     {

      SQLFreeHandle (SQL_HANDLE_DBC, hdbc1); 

      hdbc1=NULL;

      return FALSE;

     }

     // Atributos de la conexion

     // Modo defecto (read/write)

     ret = SQLSetConnectAttr (hdbc1, SQL_ATTR_ACCESS_MODE, (void *)SQL_MODE_DEFAULT, 0);

     if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)

     {

      if (ret == SQL_ERROR)

      {

       SQLDisconnect (*hdbc1);

       SQLFreeHandle (SQL_HANDLE_DBC, hdbc1); 

       hdbc1=NULL;

       return FALSE;

      };

     }

    if (hdbc == NULL) return TRUE;

     // Desconectar de la base de datos

     

     ret = SQLDisconnect (hdbc);

     if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) 

     {

      BDD_TratarError (ret,SQL_HANDLE_DBC, hdbc, funcion, "SQLDisconnect","desconectando");

      SQLFreeHandle (SQL_HANDLE_DBC, hdbc);  

      return FALSE;

     }

     // Liberar el handle

     ret = SQLFreeHandle (SQL_HANDLE_DBC, hdbc);

     if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) 

     {

      BDD_TratarError (ret,SQL_HANDLE_DBC, hdbc, funcion, "SQLFreeHandle","desconectando");

      return FALSE;

     }

     ret = SQLFreeHandle (SQL_HANDLE_ENV, henv);

     if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) 

     {

      BDD_TratarError (ret,SQL_HANDLE_ENV, henv, funcion, "SQLFreeHandle","liberando environment");

      return FALSE;

     }

    Whit this example my aplication in a client, not in the server, the memory increase about 900 Kb.

    thanks.

  • Not 100% sure but I am almost certain that the Highlighted code is preventing the Connection to be closed Properly:

    ret = SQLAllocHandle (SQL_HANDLE_DBC, henv1, &hdbc1);

     if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)

     {  

      if (ret == SQL_ERROR || ret == SQL_INVALID_HANDLE)

      {

       hdbc1=NULL;

       return FALSE;

      }

     }

     sprintf (ConnStrIn,

       "DRIVER={SQL Server};SERVER=%s;UID=%s;PWD=%s;DATABASE=%s;",

       ServerName,

       UserName,

       Authentication,

       BDName);

     

     ret = SQLDriverConnect(hdbc1,      // Connection handle

           NULL,         // Window handle

           ConnStrIn,      // Input connect string

           SQL_NTS,         // Null-terminated string

           ConnStrOut,      // Address of output buffer

           MAX_PATH,      // Size of output buffer

           &cbConnStrOut,   // Address of output length

           SQL_DRIVER_NOPROMPT);

     if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)

     {

      SQLFreeHandle (SQL_HANDLE_DBC, hdbc1);

      hdbc1=NULL;

      return FALSE;

     }

     // Atributos de la conexion

     // Modo defecto (read/write)

     ret = SQLSetConnectAttr (hdbc1, SQL_ATTR_ACCESS_MODE, (void *)SQL_MODE_DEFAULT, 0);

     if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)

     {

      if (ret == SQL_ERROR)

      {

       SQLDisconnect (*hdbc1);

       SQLFreeHandle (SQL_HANDLE_DBC, hdbc1);

       hdbc1=NULL;

       return FALSE;

      };

     }

    if (hdbc == NULL) return TRUE;

     // Desconectar de la base de datos

     

     ret = SQLDisconnect (hdbc);

     if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)

     {

      BDD_TratarError (ret,SQL_HANDLE_DBC, hdbc, funcion, "SQLDisconnect","desconectando");

      SQLFreeHandle (SQL_HANDLE_DBC, hdbc); 

      return FALSE;

     }

     // Liberar el handle

     ret = SQLFreeHandle (SQL_HANDLE_DBC, hdbc);

     if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)

     {

      BDD_TratarError (ret,SQL_HANDLE_DBC, hdbc, funcion, "SQLFreeHandle","desconectando");

      return FALSE;

     }

     ret = SQLFreeHandle (SQL_HANDLE_ENV, henv);

     if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)

     {

      BDD_TratarError (ret,SQL_HANDLE_ENV, henv, funcion, "SQLFreeHandle","liberando environment");

      return FALSE;

     }

    Try Commenting that line and give it a go

    Cheers,

     


    * Noel

  • Good catch Noel in a glance I hadn't even spotted that. Is this a typo an hdbc should actually be hdbc1. If not then you would jump out at that point and your connection was never freed.

  • Sorry, this code is copy from two functions and I didn't change tha name hdbc to hdbc1 when I paste in the forum. But that isn't the problem, because in my code is fine, and step to step debugger function propertly and didn't free the reserved memory.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply