为什么在关闭读取 ACCDB 数据库的 MFC 应用程序时会出现此异常?

发布于 2025-01-14 03:55:57 字数 2771 浏览 4 评论 0原文

我遇到了一个问题,但无法弄清楚为什么会发生。

上下文:

  • Windows 11
  • Visual Studio 2022
  • MFC Dialog 64 位
  • 调试模式

当我在调试模式下运行软件并执行特定操作(读取 ACCDB 数据库)然后终止软件时,出现异常。

我使用基本方法打开和关闭数据库:

if (m_dbDatabase.OpenEx(strDBConnectString, CDatabase::noOdbcDialog))
{
    m_pRecords = std::make_unique<CRecordset>(&m_dbDatabase);
}

并且:

void CPTSDatabase::CloseDatabase()
{
    if (m_dbDatabase.IsOpen())
        m_dbDatabase.Close();
}

数据库活动运行正常并且没有引起异常。然而,当我终止软件时:

在此处输入图像描述

static void __cdecl try_cor_exit_process(UINT const return_code) throw()
{
    __crt_unique_hmodule mscoree;
    if (!GetModuleHandleExW(0, L"mscoree.dll", mscoree.get_address_of()))
        return;

    auto const cor_exit_process = __crt_get_proc_address<exit_process_pft>(mscoree.get(), "CorExitProcess");
    if (!cor_exit_process)
        return;

    cor_exit_process(return_code);
}

我不明白为什么显示消息。我的应用程序 InitInstanceExitInstance 都(分别)引用:

  • ::CoInitialize(nullptr);
  • ::CoUninitialize();

我可以确认的是,如果我不运行打开数据库读入然后关闭应用程序的代码,我将不会收到此异常。


这是时间延迟调试条目: 输入图片此处描述

不知道这对我现阶段有什么帮助。

顺便提一句。如果我注释掉 CoUninitialize 则没有什么区别。我仍然收到错误。


我不知道它是否相关,当对数据库执行 OpenEx 调用时,我调试了它并注意到:

d:\a01_work\43\s\src\vctools\VC7Libs\Ship\ATLMFC\Src\MFC\dbcore.cpp(616) : AppMsg - 警告:ODBC 成功并包含信息,d:\a01_work\43\s \src\vctools\VC7Libs\Ship\ATLMFC\Src\MFC\dbcore.cpp(174) : AppMsg - 驱动程序的 SQLSetConnectAttr 失败

但实际函数返回 true 并给了我一个有效的数据库。我不知道此消息是否是问题的一部分。


更新

我想我会尝试一个简单的测试项目(对话框),然后简单地在对话框 OnInitDialog 中获取它以打开我的数据库,然后关闭它。然后我关闭了测试应用程序。

如果我在 DEBUG x86 中运行此代码:

if (db.OpenEx(_T("Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\My Documents\\Public Talks\\Public Talks.MDB;Pwd=~~~~"), CDatabase::noOdbcDialog))
{
    AfxMessageBox(_T("DB Opened"));
    db.Close();
}

并关闭该应用程序 - 不会触发任何问题。

但是当我使用 DEBUG x64 尝试此操作并关闭它时:

if (db.OpenEx(_T("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\\My Documents\\Public Talks\\Public Talks.ACCDB;Pwd=~~~~~"), CDatabase::noOdbcDialog))
{
    AfxMessageBox(_T("DB Opened"));
    db.Close();
}

现在触发:

在此处输入图像描述

I have encountered an issue and can't work out why it is happening.

Context:

  • Windows 11
  • Visual Studio 2022
  • MFC Dialog 64 bit
  • DEBUG mode

When I run the software in DEBUG mode, and perform a specific action (reading a ACCDB database) and then terminate the software, I get an exception.

I use the basic methods to open and close the database:

if (m_dbDatabase.OpenEx(strDBConnectString, CDatabase::noOdbcDialog))
{
    m_pRecords = std::make_unique<CRecordset>(&m_dbDatabase);
}

And:

void CPTSDatabase::CloseDatabase()
{
    if (m_dbDatabase.IsOpen())
        m_dbDatabase.Close();
}

The database activity is operating correctly and not causing exceptions. Yet, when I terminate the software:

enter image description here

static void __cdecl try_cor_exit_process(UINT const return_code) throw()
{
    __crt_unique_hmodule mscoree;
    if (!GetModuleHandleExW(0, L"mscoree.dll", mscoree.get_address_of()))
        return;

    auto const cor_exit_process = __crt_get_proc_address<exit_process_pft>(mscoree.get(), "CorExitProcess");
    if (!cor_exit_process)
        return;

    cor_exit_process(return_code);
}

I don't understand why did message is being displayed. My application InitInstance and ExitInstance both make reference to (respectively):

  • ::CoInitialize(nullptr);
  • ::CoUninitialize();

What I can confirm is that if I do not run the code that opens the database to read in and then close my application, I will not get this exception.


This is the Time Delay Debug entry:
enter image description here

Not sure how that helps me at this stage.

BTW. If I comment out CoUninitialize it makes no difference. I sill get the error.


I don't know if it is related, when the OpenEx call is performed on the database I debugged into it and noticed:

d:\a01_work\43\s\src\vctools\VC7Libs\Ship\ATLMFC\Src\MFC\dbcore.cpp(616) : AppMsg - Warning: ODBC Success With Info, d:\a01_work\43\s\src\vctools\VC7Libs\Ship\ATLMFC\Src\MFC\dbcore.cpp(174) : AppMsg - Driver's SQLSetConnectAttr failed

The actual function returned true though and gave me a valid database. I do not know if this message is part of the problem.


Update

I thought I would try a simple test project (dialog) and simply get it in the dialogs OnInitDialog to open my DB and then close it. Then I shut the test app down.

If I run this code in DEBUG x86:

if (db.OpenEx(_T("Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\My Documents\\Public Talks\\Public Talks.MDB;Pwd=~~~~"), CDatabase::noOdbcDialog))
{
    AfxMessageBox(_T("DB Opened"));
    db.Close();
}

And close that app down - no issues triggered.

But when I try this with DEBUG x64 and close it:

if (db.OpenEx(_T("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\\My Documents\\Public Talks\\Public Talks.ACCDB;Pwd=~~~~~"), CDatabase::noOdbcDialog))
{
    AfxMessageBox(_T("DB Opened"));
    db.Close();
}

Now that triggers:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文