Thread: [ERROR: Access violation writing at location] while using IRowsetfastload

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    1

    [ERROR: Access violation writing at location] while using IRowsetfastload

    ------------------------------------------------------------------------------------
    Code:
     pData = (BYTE *)malloc(ulOffset + 300);
    
       for (iRow = 0 ; iRow < nSz ; iRow++)
       {
    	   // Column: 1
    	   *(DBSTATUS *)(pData + rgBinding[0].obStatus) = DBSTATUS_S_OK;
    	   *(DBLENGTH *)(pData + rgBinding[0].obLength) = 1234567;   // Ignored for I4 data
    	   *(LONG *)(pData /*+ rgBinding[0].obValue*/) = nSessionId;
    
    	   // Column: 2
    	   *(DBSTATUS *)(pData + rgBinding[1].obStatus) = DBSTATUS_S_OK;
    	   *(DBLENGTH *)(pData + rgBinding[1].obLength) = 1234567;   // Ignored for I4 data
    	   *(LONG *)(pData + rgBinding[1].obValue) = nDataID;
    	   
    	   // Column: 3
    	   *(DBSTATUS *)(pData + rgBinding[2].obStatus) = DBSTATUS_S_OK;
    	   *(DBLENGTH *)(pData + rgBinding[2].obLength) = 1234567;   // Ignored for I4 data
    	   *(LONG *)(pData + rgBinding[2].obValue) = iRow;
    
    	   _bstr_t strVal = TableValues[iRow];
    	   VARIANT * Coldata = new VARIANT;
    	   Coldata->vt = VT_BSTR;
    	   Coldata->bstrVal = strVal;
    
    	   VARIANT * temp = new VARIANT;
    	   temp->vt = VT_INT;
    	   temp->fltVal= 10;	
    
    	   // Column: 4
    	   strVal = "01/01/04";
    	   temp->vt = VT_BSTR;
    	   temp->bstrVal = strVal;
    	   
    	   *(DBSTATUS *)(pData + rgBinding[3].obStatus) = DBSTATUS_S_OK;
    	   *(DBLENGTH *)(pData + rgBinding[3].obLength) = 1234567;   // Ignored for I4 data
    	   *(VARIANT *)(pData + rgBinding[3].obValue) = *temp;
    
    	   // Column: 5
    	   *(DBSTATUS *)(pData + rgBinding[4].obStatus) = DBSTATUS_S_OK;
    	   *(DBLENGTH *)(pData + rgBinding[4].obLength) = 1234567;   // Ignored for I4 data
    	   *(VARIANT *)(pData + rgBinding[4].obValue) = *Coldata;
    
    	   // Column: 6
    	   *(DBSTATUS *)(pData + rgBinding[5].obStatus) = DBSTATUS_S_OK;
    	   *(DBLENGTH *)(pData + rgBinding[5].obLength) = 1234567;   // Ignored for I4 data
    	   *(VARIANT *)(pData + rgBinding[5].obValue) = *temp;
    
    	   // Column: 7
    	   *(DBSTATUS *)(pData + rgBinding[6].obStatus) = DBSTATUS_S_OK;
    	   *(DBLENGTH *)(pData + rgBinding[6].obLength) = 1234567;   // Ignored for I4 data
    	   *(VARIANT *)(pData + rgBinding[6].obValue) = *temp;
    
    	   // Column: 8
    	   *(DBSTATUS *)(pData + rgBinding[7].obStatus) = DBSTATUS_S_OK;
    	   *(DBLENGTH *)(pData + rgBinding[7].obLength) = 1234567;   // Ignored for I4 data
    	   *(LONG *)(pData + rgBinding[7].obValue) = 1;
    
    	   assert (pData);
    	
    	   if (SUCCEEDED(hr))
    		   hr = pIRowsetFastLoad->InsertRow(hAcc, pData);
    <THIS IS WHERE ACCESS VIOLATION OCCURS>

    ------------------------------------------------------------------------------------

    Here are debug values for pData:


    Code:
    		
                    *(LONG *)(pData )	= 280	long
    
    		*(LONG*)(pData + 28)	= 4	long
    
    		*(LONG*)(pData + 64)	 = 0	long
    
    		*(VARIANT*)(pData + 100)	BSTR = 0x00000000001c1b78 "01/01/04"	tagVARIANT
    
    		*(VARIANT*)(pData + 136)	BSTR = 0x00000000001bf5d8 "0"	tagVARIANT
    
    		*(VARIANT*)(pData + 172)	BSTR = 0x00000000001c1b78 "01/01/04"	tagVARIANT
    
    +		*(VARIANT*)(pData + 208)	BSTR = 0x00000000001c1b78 "01/01/04"	tagVARIANT
    
    		*(LONG*)(pData + 244)	= 1	long

    As you can see, I've already hacked up my code to find out whats causing this exception.. Any help would be very appreciated!
    Last edited by krCode; 08-13-2008 at 12:05 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That code is HORRIBLE. Far too much casting going for anyone with less than planetary sized brain to follow what is going on - I'm sure the problem can be solved in a neater way.

    Also: assert (pData);
    is WAY too late - you have just accessed memory through pData 8 * 3 times, so if it was NULL, the program would have crashed a long time ago.

    Code:
    *(LONG *)(pData /*+ rgBinding[0].obValue*/) = nSessionId;
    Is that commented out bit [in red] becuase you know that it's always zero, so therefore can be eliminated. Don't you think the compiler is better at figuring that out?

    Finally, on the line where it crashes:
    Code:
     hr = pIRowsetFastLoad->InsertRow(hAcc, pData);
    What is pIRowsetFastLoad, what is hAcc? What does InsertRow do?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  2. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM
  3. Access violation writing location 0x00000000
    By bennyandthejets in forum Windows Programming
    Replies: 9
    Last Post: 04-14-2004, 03:59 AM