zmq_ctx_destroy() hangs in MFC dll - c++

I'm writing an extension to MFC app with use of ZMQ (zmq.hpp). When I'm trying to unload my DLL from the app, the zmq_ctx_destroy() function hangs forever.
I have found a similar issue but there is no answer.
I've tried to debug it and found out that it stops in function zmq::thread_t::stop() on the first line:
DWORD rc = WaitForSingleObject (descriptor, INFINITE);
It hung even without sending anything. Simplified code looks like this:
zmq::context_t context(1);
zmq::socket_t socket(context, ZMQ_REQ);
socket.connect(ENDPOINT.c_str());
Socket and context destroyed when leaving scope.
Call Stack:
libzmq-v100-mt-gd-4_0_4.dll! zmq::thread_t::stop() Line 56 + 0x17 bytes C++
libzmq-v100-mt-gd-4_0_4.dll! zmq::select_t::~select_t() Line 57 + 0x13 bytes C++
libzmq-v100-mt-gd-4_0_4.dll! zmq::select_t::`scalar deleting destructor'() + 0x2c bytes C++
libzmq-v100-mt-gd-4_0_4.dll! zmq::io_thread_t::~io_thread_t() Line 39 + 0x37 bytes C++
libzmq-v100-mt-gd-4_0_4.dll! zmq::io_thread_t::`scalar deleting destructor'() + 0x2c bytes C++
libzmq-v100-mt-gd-4_0_4.dll! zmq::ctx_t::~ctx_t() Line 82 + 0x49 bytes C++
libzmq-v100-mt-gd-4_0_4.dll! zmq::ctx_t::`scalar deleting destructor'() + 0x2c bytes C++
libzmq-v100-mt-gd-4_0_4.dll! zmq::ctx_t::terminate() Line 153 + 0x3d bytes C++
libzmq-v100-mt-gd-4_0_4.dll! zmq_ctx_term(void * ctx_) Line 171 + 0xa bytes C++
libzmq-v100-mt-gd-4_0_4.dll! zmq_ctx_destroy(void * ctx_) Line 242 C++
DataReader.dll! zmq::context_t::close() Line 309 + 0xe bytes C++
DataReader.dll! zmq::context_t::~context_t() Line 303 C++
The MFC app has a mechanism to run specifically created DLLs. This DLL is based on CWinApp, all DLL-specific initialization code in the InitInstance member function and termination code in ExitInstance. So this JIRA issue should not be the case.
After a couple of days I found out that the app also relies on sockets as ZMQ. So at the end of its life ZMQ context was waiting for closing of all opened sockets in the process, but MFC app continues to use its opened sockets. That was the reason why zmq_ctx_destroy() function hangs forever.

Solution for cases like this one (when app uses sockets as well and you need to add some functionality based on ZMQ).
Create a new process. This process will create a ZMQ context and send/receive all messages. Data from dll could be passed to that process via Windows messages or shared memory.

Related

CreateProcess to execute batch file

In a Windows Application I created I am attempting to run a batch file that points to a .vbs. It runs the vbs script but it does not modify the registry as needed. However, if I run the batch file manually it functions properly. Does anyone have any insight as to what could be the issue? I thought it was permissions but why would it matter if I manually click on the batch file or if my program opens it? Regardless, I have modified the batch file to try to run as admin but nothing online seems to work.
It says nowhere that CreateProcess can run batch files. As batch files aren't programs CreateProcess can't do anything with it. CMD.exe executes batch files.
YOU WOULD HAVE KNOWN THIS BY TESTING THE RETURN VALUE and by reading the Docs
From Help
Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Also from Help
To run a batch file, you must start the command interpreter; set lpApplicationName to cmd.exe and set lpCommandLine to the name of the batch file.
This is What You are Doing Wrong
1. You have a bizzare program design.
Any program can prompt for user input and write to the registry. You are writing a program that will have at least four different ways of writing the registry (.NET, WSH, WMI, API). Then you run a batch that has two ways of writing the registry (WMI and reg.exe). Then a vbs file with two ways (WMI, WSH).
2. You do not test for errors.
You must test for errors. The only person with access to the error is YOU. Errors say what happened. You must fix the error or crash. And there is no point asking without the error number, error source, what it was trying to do, and the parameters.
Errors are to be expected. Users do delete or move files for example.
3. You must read the documentation
It has to be open while you program. It explains what you need to tell it for it to behave in certain ways. Just typing random commands (as many here do - they are invalid of course) OR typing random parameters (as you are) doesn't work. If you don't read them you don't know what they mean.
4. Playing helpless
When you get told the answer, you go and look everything up in the documentation, and then google it.
You not reply with a pathetic "it didn't work". You post the error number, error source, what it was trying to do, and the parameters.
This is how to decode errors
Decoding Errors
-2147220978 style numbers are 32 bit signed integers, convert to hex with calculator.
Windows errors (smallish numbers) and COM HResults (typically, but with exceptions, start with an 8 as in 0x80040154) are defined in WinError.h, except 8007nnnn where you look up the Window error number that it contains.
As a general rule Windows errors are less than 65,535 (0xFFFF). Errors starting 0x80000001 are Component Object Model (COM) HResults. Errors starting 0xC0000001 are NTStatus results. Errors starting 0xD0000001 are also NTStatus values returned in a HResult.
NTStatus errors (typically but not always start with an C as in 0xC0000022) are defined in NTStatus.h.
.h files are the best source because it includes the symbolic name of the error which can give clues such as the source of the error. FormatMessage doesn't give the symbolic name only the description.
You get these files by downloading the Platform SDK (it's gigabytes)
http://www.microsoft.com/en-us/download/details.aspx%3Fid%3D8279&sa=U&ei=w2IrULDDLsHFmAWbmIHoBg&ved=0CBwQFjAA&usg=AFQjCNHZn9-4f2NnuN9o3UWUsOF3wL7HBQ
If you just want the two files I have them on my skydrive so I can reference them anywhere I go.
https://skydrive.live.com/redir?resid=E2F0CE17A268A4FA!121
Note internet errors (12,000 - 12,999) are windows errors but are specified in wininet.h also available above.
There are errors defined in other .h files. But 99% are in the three above.
Structure of HResults and NTStatus Codes
The most significant bit in HResults, and the two most significant bits in NTStatus are set on error. Hence Hresults start 8 on error and NTStatus starts C on Error. The next 14 or 15 bits are reserved and some specify the facility - what area the error is in. This is the third and fourth number when reading hex. EG 0xnn07nnnn - An HResult facility code 7 is a normal Windows' error (returned from a COM program - hence it's returned as a HResult). Facility codes are defined in Winerror.h for HResults and NTStatus.h for NTStatus codes. They are different.
To Decode 0x8003nnnn Errors
HResults with facility code 3 means the HResult contains OLE Structured Storage errors (0x0 to 0xff). These are the same as Dos error codes. These don't seem to be in Windows' header files and the list of codes is at the end of this post.
To Decode 0x8004nnnn Errors
HResults with facility code 4 means the HResult contains OLE errors (0x0 to 0x1ff) while the rest of the range (0x200 onwards) is component specific errors so 20e from one component will have a different meaning to 20e from another component.
This is why the source of the error is extra important for errors above 0x80040200.
To Decode 0x8007nnnn Errors
HResults with facility code 7 means the HResult contains a Windows' error code. You have to look up the Windows' error code not the HResult.
To decode 0x80070002. The 0x means it's a hexadecimal number, the 8 means error, the first 7 means it a windows error, and the rest of the number, 2, is the actual Windows error.
To look up the error we need it in decimal format. Start Calculator (Start - All Programs - Accessories - Calculator) and choose View menu - Scientific, then View menu - Hex. Enter 2. Then View menu - Decimal. It will say 2.
Start a Command Prompt (Start - All Programs - Accessories - Command Prompt) and type
net helpmsg 2
and it will say
The system cannot find the file specified.
or look it up in winerror.h
//
// MessageId: ERROR_FILE_NOT_FOUND
//
// MessageText:
//
// The system cannot find the file specified.
//
#define ERROR_FILE_NOT_FOUND 2L
To Decode 0x8019nnnn Errors
HResults with facility 0x19 are HTTP errors. Codes under 16,384 (0x4000) are the same as HTTP errors, eg HTTP status 404: The requested URL does not exist on the server is 0x80190194 (0x194 = 404). Codes 16,384 and higher are BITS specific.
To Decode 0xDnnnnnnn Errors
HResults starting 0xD are an HResult with a NTStatus value in it. Just change the lead D to a C and treat as an NTStatus (Hresult = NTStatus OR 0x10000000).
Dos Error Codes (for 0x8003nnnn errors)
Code Message
01 Invalid function number
02 File not found
03 Path not found
04 Too many open files (no handles left)
05 Access denied
06 Invalid handle
07 Memory control blocks destroyed
08 Insufficient memory
09 Invalid memory block address
0A Invalid environment
0B Invalid format
0C Invalid access mode (open mode is invalid)
0D Invalid data
0E Reserved
0F Invalid drive specified
10 Attempt to remove current directory
11 Not same device
12 No more files
13 Attempt to write on a write-protected diskette
14 Unknown unit
15 Drive not ready
16 Unknown command
17 CRC error
18 Bad request structure length
19 Seek error
1A Unknown media type
1B Sector not found
1C Printer out of paper
1D Write fault
1E Read fault
1F General failure
20 Sharing violation
21 Lock violation
22 Invalid disk change
23 FCB unavailable
24 Sharing buffer overflow
25 Reserved
26 Unable to complete file operation (DOS 4.x)
27-31 Reserved
32 Network request not supported
33 Remote computer not listening
34 Duplicate name on network
35 Network name not found
36 Network busy
37 Network device no longer exists
38 NetBIOS command limit exceeded
39 Network adapter error
3A Incorrect network response
3B Unexpected network error
3C Incompatible remote adapter
3D Print queue full
3E No space for print file
3F Print file deleted
40 Network name deleted
41 Access denied
42 Network device type incorrect
43 Network name not found
44 Network name limit exceeded
45 NetBIOS session limit exceeded
46 Temporarily paused
47 Network request not accepted
48 Print or disk redirection is paused
49-4F Reserved
50 File already exists
51 Reserved
52 Cannot make directory entry
53 Fail on INT 24
54 Too many redirections
55 Duplicate redirection
56 Invalid password
57 Invalid parameter
58 Network device fault
59 Function not supported by network (DOS 4.x)
5A Required system component not installed (DOS 4.x)
Facility Codes
NTStatus Facilities HResults Facilities
Common status values 0x0 Null 0x0
Debugger 0x1 Rpc 0x1
Rpc_runtime 0x2 Dispatch 0x2
Rpc_stubs 0x3 Storage 0x3
Io_error_code 0x4 Itf 0x4
Various drivers 0x5-0xf Win32 0x7
Ntwin32 0x7 Windows 0x8
Ntsspi 0x9 Sspi 0x9
Terminal_server 0xa Security 0x9
Faciltiy_mui_error_code 0xb Control 0xa
Usb_error_code 0x10 Cert 0xb
Hid_error_code 0x11 Internet 0xc
Firewire_error_code 0x12 Mediaserver 0xd
Cluster_error_code 0x13 Msmq 0xe
Acpi_error_code 0x14 Setupapi 0xf
Sxs_error_code 0x15 Scard 0x10
Transaction 0x19 Complus 0x11
Commonlog 0x1a Aaf 0x12
Video 0x1b Urt 0x13
Filter_manager 0x1c Acs 0x14
Monitor 0x1d Dplay 0x15
Graphics_kernel 0x1e Umi 0x16
Driver_framework 0x20 Sxs 0x17
Fve_error_code 0x21 Windows_ce 0x18
Fwp_error_code 0x22 Http 0x19
Ndis_error_code 0x23 Usermode_commonlog 0x1a
Hypervisor 0x35 Usermode_filter_manager 0x1f
Ipsec 0x36 Backgroundcopy 0x20
Maximum_value 0x37 Configuration 0x21
State_management 0x22
Metadirectory 0x23
Windowsupdate 0x24
Directoryservice 0x25
Graphics 0x26
Shell 0x27
Tpm_services 0x28
Tpm_software 0x29
Pla 0x30
Fve 0x31
Fwp 0x32
Winrm 0x33
Ndis 0x34
Usermode_hypervisor 0x35
Cmi 0x36
Windows_defender 0x50

MFC CDialog::Create hangs on x64

I have this legacy application which I've been trying to get up and running.
It has some dependencies on an old DirectX SDK version which can only be installed on WinXP (or prior) so I used a virtual machine of mine and successfully built the application. However, due to limitations of the virtual graphics hardware the application is not rendering its graphics correctly, but that was just expected. The rest of the application (UI-interaction and such) seems to work just fine on the virtual machine (32-bit Windows XP).
When I try to to run the applcation on my physical machine, 64-bit Windows 7, it hangs in an early state. I used some debug output to track down exactly where it hangs, and it's when CDialog::Create is called for creating a modeless dialog.
I finally tried to start it on my laptop, 32-bit Windows 8.1, and it worked. I'm thinking that it should have something to do with x86 vs x64, but of course I cannot be sure.
Has anyone come across something similar? I've googled a lot on CDialog::Create hanging, and found some interesting threads, but everything looks correct in code, and well, it runs, but only on 32-bit machines.
Update:
I managed to build the application in VS2010 by removing a bunch of code. When I debug the application the callstack shows that it hangs on the CreateDialogIndirect call in CWnd::CreateDlgIndirect.
I found a thread regarding this, but it didn't solve anything, AfxOleInit is being called.
This discussion sounds more interesting but I haven't gotten around to try the proposed solution yet.
Update 2:
Setting the DEP to AlwaysOff didn't change anything.
The complete call-stack looks like this:
ntdll.dll!_NtDelayExecution#8() + 0x15 bytes
ntdll.dll!_NtDelayExecution#8() + 0x15 bytes
KernelBase.dll!_Sleep#4() + 0xf bytes
nvoglv32.dll!6955244d()
[Frames below may be incorrect and/or missing, no symbols loaded for nvoglv32.dll]
nvoglv32.dll!68e3e2e7()
nvoglv32.dll!6954d2ad()
nvoglv32.dll!6954d37c()
user32.dll!_DispatchHookA#16() + 0x56 bytes
user32.dll!_fnHkINLPCWPSTRUCTA#24() + 0x66 bytes
user32.dll!___fnINLPCREATESTRUCT#4() + 0x68 bytes
ntdll.dll!_KiUserCallbackDispatcher#12() + 0x2e bytes
user32.dll!_NtUserCreateWindowEx#60() + 0x15 bytes
user32.dll!_VerNtUserCreateWindowEx#52() + 0x18a bytes
user32.dll!_InternalCreateDialog#28() + 0x42a bytes
user32.dll!_CreateDialogIndirectParamAorW#24() + 0x33 bytes
user32.dll!_CreateDialogIndirectParamA#20() + 0x1b bytes
Viewer.exe!CWnd::CreateDlgIndirect(const DLGTEMPLATE * lpDialogTemplate, CWnd * pParentWnd, HINSTANCE__ * hInst) Line 370 C++
Viewer.exe!CDialog::CreateIndirect(const DLGTEMPLATE * lpDialogTemplate, CWnd * pParentWnd, void * lpDialogInit, HINSTANCE__ * hInst) Line 262 + 0x17 bytes C++
Viewer.exe!CDialog::CreateIndirect(void * hDialogTemplate, CWnd * pParentWnd, HINSTANCE__ * hInst) Line 240 + 0x10 bytes C++
Viewer.exe!CDialog::Create(const char * lpszTemplateName, CWnd * pParentWnd) Line 223 C++
Viewer.exe!CToolDialog::CreateModeless(CWnd * pParent) Line 41
Viewer.exe!CMainFrame::CreateTools() Line 176 + 0x17 bytes C++
Viewer.exe!CViewerApp::OnIdle(long lCount) Line 1293 C++
Viewer.exe!CWinThread::Run() Line 621 + 0xa bytes C++
Viewer.exe!AfxWinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, char * lpCmdLine, int nCmdShow) Line 47 + 0x7 bytes C++
Viewer.exe!__tmainCRTStartup() Line 275 + 0x1c bytes C
kernel32.dll!#BaseThreadInitThunk#12() + 0x12 bytes
ntdll.dll!___RtlUserThreadStart#8() + 0x27 bytes
ntdll.dll!__RtlUserThreadStart#8() + 0x1b bytes
Update 3
The application is usually started using a C# launcher application (to be able to select user profiles and other startup settings) and starting it this way it hangs. Also, starting it from the file explorer (or command prompt) it hangs. BUT, if I start it directly from VS2010 using F5, it runs...
SOLVED
I updated my graphics driver, and everything works great.
I updated my graphics driver, and everything works great.

Windows hang in WM_WINDOWPOSCHANGED

I have a DirectX9 application that sporadically runs into a hang in WM_WINDOWPOSCHANGED. The hang occurs only rarely when alt+tabbing into and out of fullscreen exclusive mode.
The application is hung because the main window message processing thread is stuck at WaitForSingleObject() in the DefWindowProc handler. Other threads we have created are operating normally.
Call Stack on Stuck Main Thread:
ntdll.dll!_NtWaitForSingleObject#12() + 0x15 bytes
ntdll.dll!_NtWaitForSingleObject#12() + 0x15 bytes
kernel32.dll!_WaitForSingleObjectExImplementation#12() + 0x43 bytes
kernel32.dll!_WaitForSingleObject#8() + 0x12 bytes
d3d9.dll!WindowProc() + 0x27e95 bytes
user32.dll!_InternalCallWinProc#20() + 0x23 bytes
user32.dll!_UserCallWinProcCheckWow#32() + 0xb7 bytes
user32.dll!_DispatchClientMessage#24() + 0x51 bytes
user32.dll!___fnDWORD#4() + 0x2b bytes
ntdll.dll!_KiUserCallbackDispatcher#12() + 0x2e bytes
user32.dll!_NtUserMessageCall#28() + 0x15 bytes
user32.dll!_RealDefWindowProcWorker#24() + 0x26afe bytes
user32.dll!_RealDefWindowProcW#16() + 0x2a bytes
uxtheme.dll!_ThemeDefWindowProc() + 0x152 bytes
uxtheme.dll!_ThemeDefWindowProcW#16() + 0x18 bytes
user32.dll!_DefWindowProcW#16() + 0x805 bytes
> Player.exe!cnWindowProc(HWND__ * hWnd=0x006507f4, unsigned int message=0x00000047, unsigned int wParam=0x00000000, long lParam=0x0034eea8) Line 502 + 0x18 bytes C++
I am wondering what Windows is waiting for here. DirectX is created and owned by a separate thread, but that thread is not stuck. We seem to be handling things correctly for lost devices (freeing up video memory resources and calling Reset()).
If anyone has ideas on why windows is hanging here, I would appreciate it.
Usually when I see this problem it is because SetWindowPos() needs to be called after setting the window style with a SetWindowLong() function. Changes will not be updated until SetWindowPos() is called, as detailed here:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633591%28v=vs.85%29.aspx
It does not always seem to crash without calling SetWindowPos(), but will sometimes. I am not sure why the result is usually so sporadic. Either way the results will not update without that call.

How to use ATL Soap Client over SSL connection

I have to write a client that connects to a web service over SSL. I'd been planning to use ATL Soap Client for this (those classes generated by msvs2005 when right clicking on project->add web reference) however those classes seem to be unable to connect to the server over SSL.
When I try to send a request, it fails here:
// default only supports HTTP
return scheme == ATL_URL_SCHEME_HTTP ? true : false;
And the stack trace is:
MyClientDll.dll!ATL::ZEvtSyncSocket::SupportsScheme(ATL::ATL_URL_SCHEME scheme=ATL_URL_SCHEME_HTTPS) Line 355 C++
MyClientDll.dll!ATL::CAtlHttpClientT::_SetDefaultUrl(const char * szURL=0x00000000, short nPort=443) Line 2269 + 0x15 bytes C++
MyClientDll.dll!ATL::CAtlHttpClientT::SetDefaultUrl(const ATL::CUrl * pUrl=0x0012beac, short nPortNumber=443) Line 1126 C++
MyClientDll.dll!ATL::CAtlHttpClientT::Navigate(const ATL::CUrl * pUrl=0x0012beac, ATL::ATL_NAVIGATE_DATA * pData=0x0012babc) Line 161 + 0x1a bytes C++
MyClientDll.dll!ATL::CSoapSocketClientT::SendRequest(const char * szAction=0x0a369058) Line 6667 + 0x1c bytes C++
Anyone knows how to make this ATL Soap client support SSL?

Stack Overflow exception (0xC00000FD) at winsock select function

I have an application which makes use of winsock.
I/O part is handled on an other thread.
And I am using blocking select method for sockets.
But the point is that after 5-6 hours,my application gives 0xC00000FD exception, at the line of select function.
As far as I know, this exception occurs when there is recursion, or very large local variables. But neither of them is the case for me.
So do you have any idea why am I getting this exception?
Or any ideas to discover what actually causes exception?
many thanks
EDIT 2:
Dear All, I am very sorry but since reproducing the case takes long time, I just realized that this has not solved the problem.
Everything seems ok when stack overflow exception occurs at the line of select function.
I mean it is a server socket with a one client connected. So there is 2 socket in rset and 1 in wset. After selecting, I am checking all ready sockets and making required, read,write,accept. Timeout is 250 ms. Do you think can this be the problem? I don't want this function to be blocking so it is not null. But what will be the exact difference if I use {0,0}
An important hint is:
Same code was working without any problem, when client socket wasn't sending any data.
But when I started sending some data from client to server this problem occured.
I am sure that there is no problem with FD_SETs and FD_CLRs, I mean when client was not sending only 1(server) socket was in rset and 1(client) was in wset.
Anyway I had a look a lot of samples, but it seems that there is not a difference.
Please see local variables screenshot below(I have deleted name of executable, since it is a commercial product)
http://img192.imageshack.us/img192/1948/stackoverflow.jpg
And here is the call stack:
ntdll.dll!7c90df3a()
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
mswsock.dll!71a53c9c()
ntdll.dll!7c90d26c()
mswsock.dll!71a55f9f()
mswsock.dll!71a55974()
ws2_32.dll!71ab314f()
xyz.exe!vm_socket_select(vm_socket * hds=0x04c1fb84, int nhd=1, int masks=7) Line 230 + 0x1b bytes C
xyz.exe!ND::nd_socket::SocketThreadProc() Line 173 + 0x12 bytes C++
xyz.exe!ND::nd_socket::ThreadRoutineStarter(void * u=0x07d63f90) Line 332 C++
xyz.exe!_callthreadstartex() Line 348 + 0x6 bytes C
xyz.exe!_threadstartex(void * ptd=0x011a3ce8) Line 326 + 0x5 bytes C
kernel32.dll!7c80b713()
I am waiting for any advice.
Many thanks
Have you tried stopping your program in a debugger after some time running? Then take a look at the stack it might give you a hint.
Recursion doesn't mean one of your functions call itself endlessly, it can't be more tricky and involve several layers before it comes back where it started.