7 Replies - 2720 Views - Last Post: 26 January 2011 - 03:52 AM Rate Topic: -----

#1 ipavlik  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 27-January 09

CDatabase::OpenEx fails without exception in release mode

Posted 21 January 2011 - 07:01 AM

Hello.

I built a library (with statically linked MFC), which has several functions working with database. There is a CDatabase pointer passed to those functions. Inside, it is opened using OpenEx function. With my application (Dialog based) it works fine.
Sample code within the library:
extern "C" __declspec(dllexport) void Test(CDatabase* pDb, CString& str)
{
	BOOL bOpen=FALSE;
	TRY
	{
		bOpen=pDb->OpenEx(_T("some dsn"), CDatabase::noOdbcDialog);
		CRecordset rs;
		rs.m_pDatabase=pDb;
		rs.Open(AFX_DB_USE_DEFAULT_TYPE, _T("some select"));
		rs.GetFieldValue((short)0, str);
		rs.Close();
		pDb->Close();
	}
	CATCH(CDBException, pEx)
	{
		str.Format(_T("OpenEx: %d; "), bOpen);
		str+=pEx->m_strError;
	}
	END_CATCH
}


Pointer to CDatabase is passed because in original code I keep connection open for other operations.
When I tried to use those functions with console application (supporting MFC) OpenEx opens database in debug mode correctly. However in release mode returns codes 2025462466 or 2023703620 and throws no exception.
Sample code from console app:
extern "C" __declspec(dllimport) void Test(CDatabase* pDb, CString& str); //within .h file
...
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	if(!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
		return 1;
	}
	CString str;
	CDatabase db;
	Test(&db, str);
	_tprintf(_T("Info: ")+str);

	return 0;
}


As I allowed debug info generating and debugged a library, Stepping into the CDatabase::OpenEx() worked as it should have in debug mode, but in release mode it was sent to an empty method:
CMFCTasksPaneToolBarCmdUI::SetRadio(BOOL /*bOn*/ = TRUE)

A piece of stack:
mfc90u.dll!CMFCTasksPaneToolBarCmdUI::SetCheck(int __formal=8) Line 3828	C++
ordbstat.dll!Test(CDatabase * pDb=0x00404460, ATL::CStringT<wchar_t,StrTraitMFC<wchar_t,ATL::ChTraitsCRT<wchar_t> > > & str="Init: 0") Line 349	C++
ServiceTest.exe!004010bb() 	
[Frames below may be incorrect and/or missing, no symbols loaded for ServiceTest.exe]	
msvcr90.dll!_initterm(void (void)* * pfbegin=0x00000001, void (void)* * pfend=0x00b2c130) Line 903	C
ServiceTest.exe!00401a5d() 	
kernel32.dll!7c817077()


Im using Visual Studio 2008.
Any suggestions will be appreciated.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: CDatabase::OpenEx fails without exception in release mode

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6106
  • View blog
  • Posts: 23,653
  • Joined: 23-August 08

Re: CDatabase::OpenEx fails without exception in release mode

Posted 21 January 2011 - 07:25 AM

You likely have a bug somewhere in your program that's overwriting memory. It's disguised in debug mode because compiling in debug mode provides more padding/slack between memory locations for housekeeping purposes. Compiling in release mode removes that slack, exposing the bug that was hidden.
Was This Post Helpful? 0
  • +
  • -

#3 ipavlik  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 27-January 09

Re: CDatabase::OpenEx fails without exception in release mode

Posted 24 January 2011 - 04:30 AM

I rebuilt both library and application with only code mentioned in this article + some initialization by VS templates. The error still remains with the same behavior. If, where the memory overwriting can occur? Is it possible to detect it somehow?

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1762
  • View blog
  • Posts: 3,417
  • Joined: 30-May 10

Re: CDatabase::OpenEx fails without exception in release mode

Posted 24 January 2011 - 04:42 AM

Debugging random memory overwrites is especially hard, since it is seldom the writer (the bug) which causes the program to immediately crash.

Most of the time, you're staring at innocent code and wondering WTF is going on.

There are tools (which usually cost $$$$$ on Windows) which can help track down the culprit.
http://en.wikipedia....Rational_Purify
http://en.wikipedia....i/BoundsChecker

Or more generally
http://en.wikipedia....Memory_debugger
Was This Post Helpful? 0
  • +
  • -

#5 ipavlik  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 27-January 09

Re: CDatabase::OpenEx fails without exception in release mode

Posted 25 January 2011 - 12:07 AM

Is it possible, that a bug is inside MFC libraries since I simplified code within the projects as was possible and things behave in same way?
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1762
  • View blog
  • Posts: 3,417
  • Joined: 30-May 10

Re: CDatabase::OpenEx fails without exception in release mode

Posted 25 January 2011 - 12:56 AM

> Is it possible, that a bug is inside MFC libraries
Are you asking if it is possible that you're the first person to find a problem in a set of libraries used by millions of people on a daily basis?

You would need to post your COMPLETE simplified example so that others (not me, since I don't have a windows machine) can look at and test your code.
Was This Post Helpful? 0
  • +
  • -

#7 ipavlik  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 27-January 09

Re: CDatabase::OpenEx fails without exception in release mode

Posted 25 January 2011 - 04:59 AM

OK, if someone is interested with this problem, I'm attaching VS 2008 project Attached File  DbBug.zip (18.76K)
Number of downloads: 49. To avoid any dependencies, as a data source an Excel file test.xls is used located in the same directory as the current exe with some additional code to find it.

Thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#8 ipavlik  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 27-January 09

Re: CDatabase::OpenEx fails without exception in release mode

Posted 26 January 2011 - 03:52 AM

Just a brief information - I forgot to switch console project to use MFC in a static library. Did it and everything works fine, damn. Don't need to use it in a shared library, so that's enough for me. I only regret time I wasted with it :(

This post has been edited by ipavlik: 26 January 2011 - 03:52 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1