Solved

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

Posted on 2003-10-22
8
959 Views
Last Modified: 2013-11-20
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
Comment
Question by:vikrant_kpr
  • 3
  • 2
8 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 9606343
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
 

Author Comment

by:vikrant_kpr
ID: 9635848
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 9636023
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
 

Author Comment

by:vikrant_kpr
ID: 9636582
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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 50 total points
ID: 9640608
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now