I'm working with MFC's CRecordset
class. I have overridden the virtual method DoBulkFieldExchange
.
In general, my overridden methods are called and working just fine. However, I have a situation where the base class DoBulkFieldExchange
method is called.
In an unrelated method of my derived CRecordset
class, I call AfxThrowDBException
. This calls the CRecordset
destructor. And, during cleanup, DoBulkFieldExchange
is called in the base class and not in my derived class. In this case, it causes an assert as the base class is not expecting the default version to be called with this configuration.
I know my derived class is set up correctly because it gets called. So what are the circumstances where the base class' method is called instead?
Here's my custom CRecordset
class:
class CRS : public CRecordset
{
public:
int m_nId;
TCHAR m_szName[CUSTOMER_NAME_MAXLENGTH + 1];
int* m_pnIds;
long* m_pnIdLengths;
LPTSTR m_pszNames;
long* m_pnNameLengths;
public:
CRS(CDatabase* pDatabase = NULL)
: CRecordset(pDatabase)
{
m_nFields = 2;
m_nId = 0;
m_szName[0] = '\0';
m_pnIds = NULL;
m_pnIdLengths = NULL;
m_pszNames = NULL;
m_pnNameLengths = NULL;
}
CString GetDefaultSQL()
{
return CCustomerData::m_szTableName;
}
void DoFieldExchange(CFieldExchange* pFX)
{
pFX->SetFieldType(CFieldExchange::outputColumn);
RFX_Int(pFX, _T("Id"), m_nId);
RFX_Text(pFX, _T("Name"), m_szName, CUSTOMER_NAME_MAXLENGTH);
}
void DoBulkFieldExchange(CFieldExchange* pFX)
{
pFX->SetFieldType(CFieldExchange::outputColumn);
RFX_Int_Bulk(pFX, _T("Id"), &m_pnIds, &m_pnIdLengths);
RFX_Text_Bulk(pFX, _T("Name"), &m_pszNames, &m_pnNameLengths, (CUSTOMER_NAME_MAXLENGTH + 1) * 2);
}
};
And here's the code that uses it. ExecuteSqlQuery
just calls CRS::Open()
with the given database.
CRemoteDatabase db;
db.Open();
auto prs = db.ExecuteSqlQuery<CRS>(NULL, CRecordset::forwardOnly, CRecordset::useMultiRowFetch);
while (!prs->IsEOF())
{
// The call to GetFieldValue is producing an 'Invalid cursor position'
// error, which causes AfxThrowDBException to be called. This
// indirectly calls the destructor, which then calls the base-class
// DoBulkFieldExchange method, which in turn ASSERTs. Why doesn't
// it call my derived method?
CString sValue;
prs->GetFieldValue((short)CUSTOMER_ID, sValue);
}