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::GetCurrentTi me();
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::OnB utton1()
{
::AfxBeginThread(ThreadPro c,this);
}
UINT ThreadProc(LPVOID arg)
{
//::AfxOleInit();
COledateTimeThreadDlg *pDlg;
pDlg = (COledateTimeThreadDlg *)arg;
for(;;)
{
COleDateTimeSpan datespan;
COleDateTime date1(::time(NULL)),date2;
::Sleep(100);
date2 = COleDateTime::GetCurrentTi me();
datespan = date2-date1;
}
}
On the line date2 = COleDateTime::GetCurrentTi
When i use F11 to switch into the function
it moves the execution to
BOOL CHtmlView::GetTheaterMode(
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::OnB
{
::AfxBeginThread(ThreadPro
}
UINT ThreadProc(LPVOID arg)
{
//::AfxOleInit();
COledateTimeThreadDlg *pDlg;
pDlg = (COledateTimeThreadDlg *)arg;
for(;;)
{
COleDateTimeSpan datespan;
COleDateTime date1(::time(NULL)),date2;
::Sleep(100);
date2 = COleDateTime::GetCurrentTi
datespan = date2-date1;
}
}
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)
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
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
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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::GetCurrentTi
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).