Re: Stack Overflow on Insert

Kim Gräsman <[email protected]> Fri, 24 May 2002 16:45:30 +0200
Newsgroups gmane.comp.windows.devel.oledb.devel
Message-ID <44059D2977EF4C4DB810E37890E3346E6FBF05@seluninfmail01.wrk.framfablabs.net>
// Loop through records
for (nRecordNum = 0; nRecordNum < NUMROWS; nRecordNum++)
{
        // Loop through columns of output database
      for (int iColumn = 0; iColumn < NUMCOLS; iColumn++)
        {
                wcsncpy((WCHAR*)pszColumnBuf[iColumn + 1], (WCHAR*)T2OLE(strValue), 128); // kaboom!
        }

        // ...
}

If you delve deeply enough into atlconv.h, you'll notice that T2OLE is implemented using alloca, a CRT function for allocating memory on the stack. It's fast as heck, but if you allocate too much memory you end up with a stack overflow, obviously.

So, if you can, move the conversion out of the loop (I'm not sure where strValue is coming from), or use a heap-based conversion instead (HeapAlloc a buffer, and use WideCharToMultiByte).

Hope that helps,
Kim


> -----Original Message-----
> From: Jeff Nygren [mailto:[email protected]]
> Sent: den 24 maj 2002 16:09
> To: [email protected]
> Subject: Re: Stack Overflow on Insert
> 
> 
> Hi, Kim,
> 
> Yes and yes.  ;-)  Is there an issue with the ATL conversion macros?
> 
> Thank you for taking a look at this. Here are the 'guts' of 
> my sample app.
> 
> Jeff Nygren
> 
> 
>    hr = DBOutputCommand.CreateAccessor(nOutputCols, &pszColumnBuf[1],
> sizeof(TCHAR)*nOutputCols*256);
>       // (I skipped pszColumnBuf[0] just so index matches 
> column ordinal.
> Yes I allocated an extra array element.)
>    for (ULONG lCol = 1; lCol <= nOutputCols; lCol++)
>       DBOutputCommand.AddBindEntry(lCol, DBTYPE_WSTR, 256,
> &pszColumnBuf[lCol]);
> 
>    hr = DBOutputCommand.Open(m_DBOutputSession, sQuery, &PropertySet);
> 
>    // Loop through records
>    for (nRecordNum = 0; nRecordNum < NUMROWS; nRecordNum++)
>    {
>       // Loop through columns of output database
>       for (int iColumn = 0; iColumn < NUMCOLS; iColumn++)
>          wcsncpy((WCHAR*)pszColumnBuf[iColumn + 1],
> (WCHAR*)T2OLE(strValue), 128);
> 
>       if (S_OK == (hr = DBOutputCommand.Insert()))
>       {
>          m_nRecordCount = nRecordNum+1;
>          UpdateData(FALSE);
>          YieldCPU();
>       }
>    }  // End of input record loop
> 
> 
> 
> 
> Hi Jeff,
> 
> Are you using any of the ATL conversion macros in the loop? 
> W2A, A2W, etc?
> 
> Could you post the code for LoadAccessorBuffers?
> 
> Best regards,
> Kim
> -----Original Message-----
> From: Jeff Nygren [mailto:[email protected]]
> Sent: den 23 maj 2002 18:50
> To: [email protected]
> Subject: Re: Stack Overflow on Insert
> 
> I don't think so, the code is pretty simple and straight-forward.
> 
> Jeff Nygren
> 
> 
> Is there any possibility of a recursive loop going on?
> 
> -----Original Message-----
> From: Jeff Nygren [mailto:[email protected]]
> Sent: 23 05 2002 15:50
> To: [email protected]
> Subject: [OLEDB_DEV] Stack Overflow on Insert
> 
> I have some code that adds records to a MS Access database. 
> If I try to
> add too many records, I get a stack overflow. I'm guessing there is
> something more I need to do, but I don't know what it is. My code is
> basically:
> 
> ...
> 
> You can read messages from the OLEDB_DEV archive, unsubscribe 
> from OLEDB_DEV,
> or subscribe to other DevelopMentor lists at 
http://discuss.develop.com.

You can read messages from the OLEDB_DEV archive, unsubscribe from OLEDB_DEV,
or subscribe to other DevelopMentor lists at http://discuss.develop.com.