mfc, Application crashes when closed

mfc >> Application crashes when closed

by UHVuaXQgS2F1cg » Thu, 10 May 2007 00:31:03 GMT

Hi,

I have a windows application written using MFC , VC++ 6.0, I am using the
Application Verifier debugging tool to solve some bugs in my application.
When running in debug mode with the Application Verifier running in the
background, the application crashes when I close it. I have overriden the
OnClose method to do the following :

I crashes at the line : see for "//error line "above. Tear down is a user
defined function, and it fails in the setsockopt function

setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,(char FAR
*)&mreq,sizeof(mreq)); which is called inside teardown.

When I verified the value of sock, the value was 000000 and I thought that
was the cause of it. I commented out that line and it started throwing error
in the previous line's tear down function. I commented out everything in the
for loop and now it gives me another error .

Another imp piece of information: When I try to build my project in Debug
mode, I get two warnings all the time:

LINK : warning LNK4098: defaultlib "LIBCMT" conflicts with use of other
libs; use /NODEFAULTLIB:library
LINK : warning LNK4098: defaultlib "MSVCRT" conflicts with use of other
libs; use /NODEFAULTLIB:library

I tried to add /NODEFAULTLIB:MSVCRTD.LIB and LIBCMTD.LIB in ignore files in
my Project settings under Link tab. But it was of no use.

Are these problems interlinked? I read somewhere that such warnings can give
rise to problems while freeing memory which I am doing in Onclose function.

mfc >> RE: Application crashes when closed

by UHVuaXQgS2F1cg » Thu, 10 May 2007 01:50:02 GMT

ok.. Half of the problem seems to be solved. I checked the code and saw that
in the function

setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,(char FAR

sock handle value being passed was 0 since for some cases we were
initializing sockets and for some we were not. But we were tryin to close all
the sockets, irrespective of whether they were initiialized . I changed the
code to check that and now it no longer gives error on that line.

But the other error still exists while closing the application "Unloading
DLL containing an active critical.

When I do step wise exec, The Onclose function successfully executes and
closes the window without any problem and then goes thru some stmts in
CMDTARG.CPP, DLGCORE.CPP, and then WINCORE.CPP and after it executes some
stmt in WindowProc method, it goes to disassembly and calls a user break
point.

The xml file that I attached in the previous post is the log file produced
by Application verifier for this bug. How should i solve it? Does it have
anything to do with how my libraries have been linked and the warnings that I
am getting. This application is a multithreaded app.

mfc >> RE: Application crashes when closed

by TWFyayBTYWxzYmVyeQ » Thu, 10 May 2007 02:36:01 GMT

> But the other error still exists while closing the application "Unloading

I'm not aware of what exactly this message means but by reading it I wonder
- is there a thread still running that is trying to take ownership of a
critical section while another thread owns it (has not called
LeaveCriticalSection()) ?

mfc >> Application crashes when closed

here are massive number of things wrong here, any one of which could lead to a crash. See
below...
On Wed, 9 May 2007 09:31:03 -0700, Punit Kaur < XXXX@XXXXX.COM > wrote:

*****
Define "crash". THe word itself is essentially a content-free noise word unless it is
accompanied by a PRECISE description of what happened! Access fault? Stack overflow?
Assertion failure? Without anything meaningful here, there is no way to guess what has
gone wrong, or what you mean by the noise word "crash"
*****
****
Why a char and not a CString? This is not Unicode-aware, and any buffer overflow in any
of these rather quaint fixed-size buffers will kill you
*****
****
CString
****
*****
Why do you need a variable which holds a pointer? This is immediately suspect as a
programming style
****
****
CString.
****
*****
CString
****
*****
Didn't you ever hear of the & operator? For example
GetLocalTime(&Timey);
would be the right way to write this, eliminates the pointless (pun intended) pointer
variable
*****
****
There are so many things wrong here that it is mind-boggling. If anything goes wrong in
this printout, your program will die a horrible, horrible death. Oh wait, it's doing that
already...
cDate.Format(_T("%04d-%02d-%02d"), Timey.wYear, Timey.wMonth, Timey.wDay);
cTime.Format(_T("%02d:%02d:%02d"), Timey.wHour, Timey.wMinute, Timey.wSecond);

you don'thave to provide the punctuation marks as %c formatting of literal values; that's
just silly.
*****
*****
Why the gratiuitous sleep? If it has to be there, your program is already wrong, so fix
the reason it is wrong and get rid of ths trash.
*****
*****
See previous comment about sleep. This is a horrific structure for detecting if a thread
has finished. Either WaitForSingleObject on the thread handle or better still implement a
completley asynchronous shutdown mechanism. This is just trash. Lose it all.
*****
****
Ditto.
****
*****
Ditto
****
*****
Ditto
****
*****
DItto

Note that if any of these threads FAILS to stop in the minuscle time you allotted, your
program will probably die a horrible death. Oh wait, it's already doing that!
*****
****
memsets scare me. In any case, ::ZeroMemory would be a better choice
*****
*****
CString TxIP;
TxIP.Format(_T("%-s"), _T("IP"));
I presume in this case that the literal "IP" is a placeholder for something that is not
yet computed.

Essentially, assume if you use fixed character buffers and sprintf that your program is
intrinsically flawed, and will crash due to buffer overruns. Take this as a given. The
number of times you ever need to use sprtinf in any real program is so vanishingly small
as to be indistinguishable from 0.
*****
*****
See comments about sleep
****
****
See comments about sleep
****
*****
THis looks deadly right here. At this point, you need to figure out why you are unloading
a DLL that has an active critical section! Until this is solved, there isn't much else to
discuss. I would not at all be surprised if that bizarre attempt to shut down the threads
was malfunctioning and led to this situation.

You need to resolve all threading issues first. I can think of a whole bunch of things
that code like this can lead to, none of them good. You need to rewrite the shutdown
sequence and make it an asynchronous shutdown sequence. Otherwise, you are going to
continue to be plagued with problems.

mfc >> RE: Application crashes when closed

by UHVuaXQgS2F1cg » Thu, 10 May 2007 02:54:01 GMT

I thought that was possible and I commented the thread code. No threads are
being spawned in my application now. Commented out all the AfxBeginThread
code. Now I have only three lines in my OnClose function

mfc >> Application crashes when closed

by UHVuaXQgS2F1cg » Thu, 10 May 2007 03:30:01 GMT

Any pointers on how to solve these warnings? For debug mode, I have the
following selected...In my project settings under C,C++ Code Generation- use
runtime library: Debug Multithreaded DLL . What else do I need to change? Any
link where it clearly describes the Visual Studio project settings for a
multithreaded app?

LINK : warning LNK4098: defaultlib "LIBCMT" conflicts with use of other
****
These are serious errors. You have somehow messed up your linker commands.
THis is
clearly a multithreaded app, and you may well have loaded the
single-threaded library.
THere is little point to even running the program until these two messages
are corrected.
*****

mfc >> Application crashes when closed

by Joseph M. Newcomer » Thu, 10 May 2007 10:06:11 GMT

Make sure that you don't have an explicit load of the CRT libraries in the linker command
line. Also, make sure that you don't have any .obj files that were compiled using the
single-threaded library, as they will force it to be loaded, causing the conflict.
joe

mfc >> Application crashes when closed

by UHVuaXQgS2F1cg » Fri, 11 May 2007 00:48:02 GMT

I used WinDbg tool to find out where the problem is and it showed the
problem in myodbc3.dll, I am using MySQL database. So I commented out
everything relted to the database and it gave no problems. I also created a
new dialog based application where I just open the database in the OnInit
dialog() and close the database in OnClose().

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
if (!TelexDB.IsOpen())
{
/********added code ***************/
try
{
TelexDB.Open( "VoiceStore",FALSE,FALSE,"ODBC;UID=root;PWD=",FALSE );
}
catch(...)
{
MessageBox("Failed To Connect To Database","Error",
MB_OK|MB_ICONINFORMATION) ;
}
}
/**************End of added code ************/

It doesnt show any compiler errors. While using WinDbg and when I close the
application it gives this error. I am guessing there is either a problem in
the way I am opening and closing the database or there is a bug in
myodbc3.dll.

Henry,
Do not pass an instance of a form to the Application.Run method (in your
Main routine). Instead show your form, and then call Application.Run()
When you are ready to exit the application use Application.Exit.
Hope this helps
Jay
"Henry" < XXXX@XXXXX.COM > wrote in message
news:184301c37873$c5c06b80$ XXXX@XXXXX.COM ...
> My application has a single form. When I need, I open the
> same kind of form (duplicate) from the form. If I close
> the duplicated form, then the application is keeping
> alive. OK. My problem is that if I close the initial
> form, then all the other forms are closed and the
> application exit.
>
> How can I do so that each time I just close one window, no
> matter which window, the application still run if there is
> another window present.

Hi, I am using the TTrayIcon from Borland. I found out that when I close
a window from my application using a right on my window's taskbar
object, it closes my whole application. I only want my application to be
closed when I right click on the system tray icon and choose Close.
Is there a property that I have missed?
Thank you
Simon