• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1123
  • Last Modified:

COleDateTime::GetCurrentTime when used with Other than Main Thread Gives Exception and strange behaviour

I am using a Thread like this...
On the line       date2      =      COleDateTime::GetCurrentTime();
When i use F11 to switch into the function
it moves the execution to

BOOL CHtmlView::GetTheaterMode() const
although i am using a Dialog Based application
I found a solution to the above problem
by Using MFC in a static library
it works fine
but in
Using MFC as dynamic link library
the above problem pops up
Also calling or not calling
::AfxOleInit();
in the thread has no change in the above problem

The above solution is also not permanent
It fails in my other application in which i am trying to use it.

Is is mean that we can't use COleDateTime in other than main thread ?
Plz Help

void COledateTimeThreadDlg::OnButton1()
{
      ::AfxBeginThread(ThreadProc,this);      
}

UINT ThreadProc(LPVOID arg)
{
      //::AfxOleInit();
      COledateTimeThreadDlg      *pDlg;
      pDlg      =      (COledateTimeThreadDlg      *)arg;
      for(;;)
      {
            COleDateTimeSpan datespan;
            COleDateTime date1(::time(NULL)),date2;
            ::Sleep(100);
            date2      =      COleDateTime::GetCurrentTime();
            datespan      =      date2-date1;
      }

}
0
vikrant_kpr
Asked:
vikrant_kpr
  • 3
  • 2
1 Solution
 
itsmeandnobodyelseCommented:
The debugger problem using MFC Dll's isn't critical: It is because the debug databases of MFC or SDK don't fit to the dll's actually used by your application. That might happened when installing Service Packs that make updates to the dll's but not to VC++ debug databases.

The problem with exceptions in your thread might have something to do with not using the multithreaded library of the C-Runtime-Dll, libcmt.lib. libcmt.lib contains a thread-safe time() function that is used by COleDateTime::GetCurrentTime().
If project settings are not properly defined, the library libc.lib is linked to your program rather libcmt.lib. With lib.c the time() function isn't thread-safe, i. e. it is using static buffers that might crash if two threads do the same at nearly same time.

So, check your programs settings, if at C++/Code Generation you have Debug MultiThreaded Dll and in LINK/Input you may explicitly set libcmtd.lib to  'Object/library modules' and libcd.lib to 'Ignore ibraries'. For Release Settings it is libcmt.lib and libc.lib and Code Generation is Multithreaded DLL.

Regards Alex

P.S.
You may check what library currently is used with the VC tool depends (Dependency Walker).
0
 
vikrant_kprAuthor Commented:
I Got these errors when i try to use the Debug Settings as specified above.
One more thing
I am using DAO and specifying the version myself
so i am using _AFXDLL
this may be the cause of error.Plz Help.
 
msvcrtd.lib(MSVCRTD.dll) : error LNK2005: __CrtDbgReport already defined in libcmtd.lib(dbgrpt.obj)
msvcrtd.lib(MSVCRTD.dll) : error LNK2005: _realloc already defined in libcmtd.lib(dbgheap.obj)
msvcrtd.lib(MSVCRTD.dll) : error LNK2005: _free already defined in libcmtd.lib(dbgheap.obj)
msvcrtd.lib(MSVCRTD.dll) : error LNK2005: _atoi already defined in libcmtd.lib(atox.obj)
msvcrtd.lib(MSVCRTD.dll) : error LNK2005: _atol already defined in libcmtd.lib(atox.obj)
msvcrtd.lib(MSVCRTD.dll) : error LNK2005: _malloc already defined in libcmtd.lib(dbgheap.obj)
msvcrtd.lib(MSVCRTD.dll) : error LNK2005: __mbschr already defined in libcmtd.lib(mbschr.obj)
msvcrtd.lib(MSVCRTD.dll) : error LNK2005: _memmove already defined in libcmtd.lib(memmove.obj)
msvcrtd.lib(MSVCRTD.dll) : error LNK2005: __setmbcp already defined in libcmtd.lib(mbctype.obj)
LINK : warning LNK4098: defaultlib "msvcrtd.lib" conflicts with use of other libs; use /NODEFAULTLIB:library
libcmtd.lib(crt0init.obj) : warning LNK4098: defaultlib "msvcrt.lib" conflicts with use of other libs; use /NODEFAULTLIB:library


10 error(s), 2 warning(s)
0
 
itsmeandnobodyelseCommented:
msvcrtd.lib has the same objects as libcmtd.lib.

I learned from Article ID: Q154753  (Knowledge Base) that in your case 'msvcrtd.lib' must be used because of 'Multithreaded DLL'.  So, remove the libcmtd.lib entry from the 'Object/Library modules'.

If you use DAO then there might be the problem. I read recently that DAO isn't thread-safe, what might be the reason for your problems.

Regards, Alex

 
0
 
vikrant_kprAuthor Commented:
Finally any suggestions ?
so that i can get Debug version working
or any way with which i can debug my program
currently in Release version the above said problem is not there.

0
 
itsmeandnobodyelseCommented:
Maybe you should try 'Debug Multithreaded' rather than 'Debug Multithreaded DLL'. That might help you with DAO also because all runtime-functions get linked statically to your program, thus avoiding problems when using a single-threaded DLL like DAO.

If this doesn't help you could ignore it as long as Release works or you have to check all DLLs using Dependency Walker (depends.exe). If there is any mix of single-threaded and multi-threaded libraries, or Debug and Release DLLs, then this should be corrected. If you could spot dao.dll as reason, you may consider using ODBC classes (CDatabase, CRecordset), that are slower but thread-safe.

Hope that helps, Alex
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now