Link to home
Start Free TrialLog in
Avatar of vikrant_kpr
vikrant_kpr

asked on

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;
      }

}
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

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).
Avatar of vikrant_kpr
vikrant_kpr

ASKER

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)
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

 
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.

ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial