Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-10-22
8
Medium Priority
?
1,017 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

670 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