Search code examples
c++visual-studiodllruntime-errorassertion

Visual Studio C++ : Debug Assertion Failed


I recently tried to create a program that can read an ODBC database then write the entries in an Excel file by using the CRecordset class, the program complilates perfectly, but the problems come in the execution...

First error :

Debug Assertion Failed!

Program: C:\Windows\system32\mfc140ud.dll

File: f:\dd\vctools\vc7libs\ship\atlmfc\include\afxwin1.inl

Line: 24

Second error :

Debug Assertion Failed!

Program: C:\Windows\system32\mfc140ud.dll

File: f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\dbcore.cpp

Line: 3312

The two errors are pointing toward the mfc140ud.dll file, it's not a missing file, so it's not the problem.

Here is the function where the exception is raised:

void parseDB(CRecordset &rs, const CString &SqlString, CString strOut) {

std::cout << "test2";

rs.Open(CRecordset::snapshot, SqlString, CRecordset::readOnly);
std::string entry;
std::fstream file;

std::cout << "test3";

while(!rs.IsEOF()) {

    std::cout << "test4";
    rs.GetFieldValue((short)0, strOut);
    CT2CA pszConvertedAnsiString = strOut; 
    entry = pszConvertedAnsiString;

    writeXLSX(entry.c_str(), file);

    rs.MoveNext();

}

rs.Close();

}

The "std::cout << "test"" are here for debugging, and my program generates these errors right after the "test2" display, so I deducted that the error comes from the "Open" line.

This is the way I initialize the CRecordset:

CString sDsn;
CDatabase db;
CRecordset rs(&db);
CString strOut;
CString SqlString;

Then, I use a CALL SQL function in a switch-case:

switch (sequence) {
        case 1:
            SqlString = "CALL GETCUSNAME(AGENTS)";
            break;
        case 2:
            SqlString = "CALL GETCUSNAME(CLIENT)";
            break;
        default:
            AfxMessageBox(_T("Wrong entry!"));
        }

I searched on many sites and I couldn't find an answer, that's why I ask a question here, thanks by advance.


Solution

  • The first assertion comes from AfxGetResourceHandle complaining that it has not been set up correctly.

    This will usually happen because you either didn't call AfxWinInit at the start of your application (if you have a console application and didn't set it up with the MFC wizard, this is very likely the case), or you're writing an MFC DLL called from non-MFC code, and you didn't add AFX_MANAGE_STATE(AfxGetStaticModuleState( )); at the start of every externally visible function.

    I believe the second is because MFC requires you to wrap CALL queries in curly braces, like so: {CALL GETCUSNAME(AGENTS)}. Otherwise the call is not recognized, and code execution enters a path it is not supposed to take.