Tomafa06
Tomafa06

Reputation: 140

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.

Upvotes: 1

Views: 22139

Answers (2)

iRef3at
iRef3at

Reputation: 11

I had a similar error, I found out this happens only in debug builds, cuz I was building an addin for certain desktop software and I was calling my MFC Dialogue inside this software. However, the problem was that this software was using release MFC Library (MFC140u.dll) while my dialogue debug build was using (MFC140ud.dll), I tried putting the (MFC140ud.dll) file in my project file and point vs and the software to load it. nut didn't work. The solution that worked for me was addin a Conditional Compilation preprocessor snippet to undefine DEBUG before including MFC headers. Then redefine DEBUG if it was defined before. in my pre-compiled header file.

#pragma once

#ifndef PCH_H
#define PCH_H

#ifdef _DEBUG
#define _DEBUG_WAS_DEFINED
#undef _DEBUG
#define NDEBUG
#endif

// -- Windows and MFC Inclusions --

#include "afxwin.h"
#include "afx.h"
#include "framework.h"
#include <Windows.h>

// -- Project Inclusions --
#include <xxxxx.h>

#if defined(_AFXEXT) && defined(__cplusplus)
#include <afxcoll.h>
#endif
#endif //PCH_H

Upvotes: 0

Sebastian Redl
Sebastian Redl

Reputation: 71899

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.

Upvotes: 3

Related Questions