CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2013
    Posts
    4

    Out of Memory and driver could not be loaded due to system error 8

    I am getting "driver could not be loaded due to system error 8" error while connecting to SQL Server 2005 from VC++. Its also throwing out of memory error. Basically i am developing and ISAPI dll. I use the following code to connect to DB.

    CDatabase DBConnection;

    if(! DBConnection.IsOpen())
    {

    DBConnection.OpenEx("Driver={SQL Server};Server=10.120.110.30;Database=Test;Trusted_Connection=yes;", CDatabase:penReadOnly | CDatabase::noOdbcDialog);
    }

    CRecordset RecSet(&DBConnection);
    RecSet.Open(CRecordset::forwardOnly,_T("{CALL SP_GetDetails('" + Username +"','403')}"));
    while(!RecSet.IsEOF())
    {
    RecSet.GetFieldValue((short)0,strValue);
    RecSet.MoveNext();
    }

    RecSet.Close();

    DBConnection.Close();

    I am new to C++. Can anybody please help to verify my code?. Also, for CDatabaseConnection, i can see 2 different method to open the connection, OpenEx and Open. Whats the difference between OpenEx and Open?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Out of Memory and driver could not be loaded due to system error 8

    Well, you have to read the documentation about MFC classes CDatabase and CRecordset.
    To understand the difference between OpenEx and Open methods just read the documentations for both and compare!
    If you'd read the docs you'd know that CDatabase and CRecordset methods could throw CDBException, so you should use try/catch blocks to catch possible exceptions.
    And the last (but not least): you have to debug your code to see what, where exactly and why goes wrong in your code!
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2013
    Posts
    4

    Re: Out of Memory and driver could not be loaded due to system error 8

    Hi Victor,

    Thanks for your response. Yes, the code which i shared was very much inside the try-catch block. Thats the reason i was able catch it and write the error in to a log file.

    Basically i am new to c++. Atlease i hope the syntax what i have used to retrieve the data from database is correct and not causing the issue because of that. Following is the code with try-catch

    CDatabase DBConnection;
    try
    {
    if(!DBConnection.IsOpen())
    {

    DBConnection.OpenEx("Driver={SQL Server};Server=10.120.110.30;Database=Test;Trusted_Connection=yes;", CDatabaseenReadOnly | CDatabase::noOdbcDialog);
    }

    CRecordset RecSet(&DBConnection);
    RecSet.Open(CRecordset::forwardOnly,_T("{CALL SP_GetDetails('" + Username +"','403')}"));
    while(!RecSet.IsEOF())
    {
    RecSet.GetFieldValue((short)0,strValue);
    RecSet.MoveNext();
    }

    RecSet.Close();
    DBConnection.Close();

    }
    catch(CDBException * ex)
    {
    DBConnection.Close();
    TCHAR buf[500];
    ex->GetErrorMessage(buf, 500);
    CString strPrompt(buf);
    CloseHandle(hToken);
    RevertToSelf();
    WriteToLogFile(strPrompt +" - after connecting to DB");
    }

    I open the connection using - DBConnection.OpenEx("Driver={SQL Server};Server=10.120.110.30;Database=Test;Trusted_Connection=yes;",

    Some code samples in internet shows passing DSN in the connection string. So far i didn't create a DSN explicity and passing it to connection string. Will that make any difference ?

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Out of Memory and driver could not be loaded due to system error 8

    Quote Originally Posted by princupthomas View Post
    Hi Victor,

    Thanks for your response. Yes, the code which i shared was very much inside the try-catch block. Thats the reason i was able catch it and write the error in to a log file.

    Basically i am new to c++. Atlease i hope the syntax what i have used to retrieve the data from database is correct and not causing the issue because of that. Following is the code with try-catch

    Code:
    CDatabase DBConnection;
    try
    {
      if(!DBConnection.IsOpen())
       {
    
         DBConnection.OpenEx("Driver={SQL Server};Server=10.120.110.30;Database=Test;Trusted_Connection=yes;", CDatabase:penReadOnly | CDatabase::noOdbcDialog);
       }
    
        CRecordset RecSet(&DBConnection); 
        RecSet.Open(CRecordset::forwardOnly,_T("{CALL SP_GetDetails('" + Username +"','403')}"));
        while(!RecSet.IsEOF())
         {
          RecSet.GetFieldValue((short)0,strValue);
          RecSet.MoveNext();
         }
    
        RecSet.Close();
        DBConnection.Close();
    
    }
    catch(CDBException * ex)
    {
       DBConnection.Close();
       TCHAR buf[500];
       ex->GetErrorMessage(buf, 500);
       CString strPrompt(buf);
       CloseHandle(hToken);
       RevertToSelf();
       WriteToLogFile(strPrompt +" - after connecting to DB");
    }
    First, please use Code tags. Otherwise your code is very hard to read/understand. Read Announcement: Before you post....
    Second, what exception/error message do you get? Which line of your code does produce this exception?

    The connection string examples for SQL Server 2005:
    http://www.connectionstrings.com/sql...-0-odbc-driver
    http://www.connectionstrings.com/sql...-0-odbc-driver
    Victor Nijegorodov

  5. #5
    Join Date
    Jan 2013
    Posts
    4

    Re: Out of Memory and driver could not be loaded due to system error 8

    Hi Victor,

    Thanks for your response. Basically i am developing an ISAPI dll, with which i can catch few of the error conditions and redirect the user to a another URL which will be picked from SQL Server DB. The Web Server(IIS) and SQL Server are in 2 different systems. I am redirecting the user by modifying the HTTP Header values, ie - HTTP Status - 302 Redirect & Location: new url

    IIS sends the response in chunks. Most of the times the error condition comes as part of the second or third chunk. So i am forced to buffer the full content and modify it, if the error conditions are detected. When ever the "SF_NOTIFY_SEND_RAW_DATA" event occurs i will buffer the content and on "SF_NOTIFY_END_OF_REQUEST" event i will check for the error condition by searching in the buffered data. Now i am mainly facing 2 errors,

    1. Specified driver could not be loaded due to system error 8 (SQL Server).
    (google says - Sytem error 8 means: Not enough storage is available to process this command)
    2. Out of Memory.

    The out of Memory error is getting caught by the try-catch block where i put the code to connect to DB. I dont have the exact line number for this. Following is the code which am using to buffer the IIS response

    Code:
    DWORD CRedirectFilter::HttpFilterProc(PHTTP_FILTER_CONTEXT pfc, DWORD NotificationType, LPVOID pvNotification) 
    {      
      DWORD dwRet = SF_STATUS_REQ_NEXT_NOTIFICATION; 
       switch (NotificationType)     
        {      
         case SF_NOTIFY_PREPROC_HEADERS:
         break;
    
         case SF_NOTIFY_SEND_RAW_DATA:   
    	dwRet = OnSendRawData(pfc, (PHTTP_FILTER_RAW_DATA)pvNotification); 
    	break;         
         case SF_NOTIFY_END_OF_REQUEST:             
    	dwRet = OnEndOfRequest((PHTTP_FILTER_CONTEXT)pfc);             
    	break; 
    	default:             
    		// We cannot reach here, unless Web Filter support has a BAD ERROR.             
    		SetLastError( ERROR_INVALID_PARAMETER);
    		WriteToLogFile("Web Filter support has a bad error - from default in switch-case");
    		dwRet = SF_STATUS_REQ_ERROR;             
    		break;     
         }       
         return dwRet; 
    } 
    
    DWORD CRedirectFilter::OnSendRawData(PHTTP_FILTER_CONTEXT pfc, PHTTP_FILTER_RAW_DATA pInRawData)
    {
       if ( NULL == pfc->pFilterContext )
      {
        pfc->pFilterContext = (LPVOID)pfc->AllocMem( pfc, sizeof(HTTP_FILTER_RAW_DATA), dwReserved);
        if ( NULL == pfc->pFilterContext )
          {
    	 DisableNotifications( pfc, SF_NOTIFY_END_OF_REQUEST | SF_NOTIFY_SEND_RAW_DATA);
    	 WriteToLogFile(" from if ( NULL == pfc->pFilterContext ) -" + URLtoCheck); 
    	 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
    	 return SF_STATUS_REQ_ERROR;
          }
    
     PHTTP_FILTER_RAW_DATA pRawData = (PHTTP_FILTER_RAW_DATA)pfc->pFilterContext;
    
    
     pRawData->cbInBuffer = pInRawData->cbInBuffer;
     pRawData->pvInData = (LPVOID)pfc->AllocMem( pfc, pRawData->cbInBuffer, dwReserved);
       if ( NULL == pRawData->pvInData )
        {
    	DisableNotifications(pfc,SF_NOTIFY_END_OF_REQUEST | SF_NOTIFY_SEND_RAW_DATA);
    	WriteToLogFile("from if ( NULL == pRawData->pvInData ) - "+URLtoCheck);
    	SetLastError( ERROR_NOT_ENOUGH_MEMORY );
    	return SF_STATUS_REQ_ERROR;
        }
    
     pRawData->cbInData = 0;
     pRawData->dwReserved = pInRawData->dwReserved ;
    
     }
    
    
      /** Get the pRawData from the Request Context. */
      PHTTP_FILTER_RAW_DATA pRawData = (PHTTP_FILTER_RAW_DATA)pfc->pFilterContext;
    
    
      /** Check for integer overflow */
      DWORD NewSize = pInRawData->cbInData + pRawData->cbInData ;
    
      if (NewSize < pInRawData->cbInData || NewSize < pRawData->cbInData )
       {
         DisableNotifications(pfc,SF_NOTIFY_END_OF_REQUEST | SF_NOTIFY_SEND_RAW_DATA);
         WriteToLogFile("from if (NewSize < pInRawData->cbInData || NewSize < pRawData->cbInData ) -" + URLtoCheck);
         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
         return SF_STATUS_REQ_ERROR;
       }
    
       int nsize = NewSize;
    	
    
    
       /* 
        * Check if the memory needed exceeds the limit allowed (the threshold). 
    	* Note. In this sample, when the filter identifies such a case, it will try to  
    	* change the response based on the partial data accumulated. After that,  
    	* the filter will cease to receive notifications for the current request and  
    	* will pass the rest of the response without change. This sample can be  
    	* modified so that the filter will block the response if the threshold is  
    	* exceeded.  
    	/
        if  (NewSize  > ACCUMULATION_SIZE_LIMIT)
          {
    
      	DWORD dwRetVal = OnEndOfRequest(pfc);
    	DisableNotifications( pfc, SF_NOTIFY_END_OF_REQUEST | SF_NOTIFY_SEND_RAW_DATA);
    	return dwRetVal;
          }
    
       /* 
    	* If the buffer in pRawData is insufficient, increase the buffer size. 
    	* Note. To achieve better performance, when additional memory allocation  
    	* is needed, the filter allocates twice the amount of memory that is needed.  
    	* This way, we reduce the number of allocation operations significantly and  
    	* achieve better performance for the filter. 
    	*/   
    	
    	if ( NewSize > pRawData->cbInBuffer)
    	{
    	
    	  /** Check for integer overflow */
    	   pRawData->cbInBuffer = 2*NewSize;
    	   if (pRawData->cbInBuffer <  NewSize)
    	    {
    	    DisableNotifications(pfc,SF_NOTIFY_END_OF_REQUEST | SF_NOTIFY_SEND_RAW_DATA);
    	    WriteToLogFile("from if (pRawData->cbInBuffer <  NewSize) - " + URLtoCheck);
    	    SetLastError( ERROR_ARITHMETIC_OVERFLOW );
    	    return SF_STATUS_REQ_ERROR;
    	    }
    	  
               LPBYTE lpBuffer = (LPBYTE)pfc->AllocMem(pfc,pRawData->cbInBuffer, dwReserved);
    	  if ( NULL == lpBuffer )
    	   {
    	     DisableNotifications(pfc,SF_NOTIFY_END_OF_REQUEST | SF_NOTIFY_SEND_RAW_DATA);
    	     WriteToLogFile("from if ( NULL == lpBuffer ) - " + URLtoCheck);
    	     SetLastError( ERROR_NOT_ENOUGH_MEMORY );
    	     return SF_STATUS_REQ_ERROR;
    	    }
    
    	     memcpy((LPVOID)lpBuffer, pRawData->pvInData, pRawData->cbInData);
    	     pRawData->pvInData = (void *)lpBuffer;
    	 }
    	   /** Append InRawData ( new chunk )  to accumulation buffer. */
    		LPBYTE lpBuffer = (LPBYTE)pRawData->pvInData;
    		memcpy((LPVOID)(&lpBuffer[pRawData->cbInData]), pInRawData->pvInData, pInRawData->cbInData);
    		pRawData->cbInData = pRawData->cbInData + pInRawData->cbInData;
    
    		/** Mark current chunk as size 0 ( i.e. Don't send enything. ) */
    		pInRawData->cbInData = 0;
    
    		return SF_STATUS_REQ_NEXT_NOTIFICATION;
    }

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Out of Memory and driver could not be loaded due to system error 8

    Quote Originally Posted by princupthomas View Post
    Hi Victor,

    Thanks for your response. Basically i am developing an ISAPI dll, with which i can catch few of the error conditions and redirect the user to a another URL which will be picked from SQL Server DB. The Web Server(IIS) and SQL Server are in 2 different systems. I am redirecting the user by modifying the HTTP Header values, ie - HTTP Status - 302 Redirect & Location: new url

    IIS sends the response in chunks.
    But did you try to test your DB-code in a stand-alone small application without any IIS and so on to be sure it works (opens DB, opens recordset and so on?)
    Victor Nijegorodov

  7. #7
    Join Date
    Jan 2013
    Posts
    4

    Re: Out of Memory and driver could not be loaded due to system error 8

    Hi Victor,

    Yes. I did the test in my development environment and it looks good. The issue comes only in the Production server environment and that too, occassionally. All other time it works as expected. I am little confused and struggling to find where the memory leak is happening.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Out of Memory and driver could not be loaded due to system error 8

    Is your program/Production server environment (which you don't state) multi-threaded? It has been my unfortunate experience that occassional memory problems that can't be easily reproduced are often due to subtle errors in multi-threaded code.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured