rahulMCA

Hi,

I have a windows service written in C# 2005. The service basically listens to a queue in MSMQ. As soon as a message arrives in the monitored queue, the service processes it, does some processing in MS-access using ADO, OLEDB and ODBC.NET and finally executes a few queries, fills dataset and populates Excel sheets using MS-Office PIA(Primary Interop Assembly) and sends an email attaching the excel workbook. The service runs fine, processes a message when it comes and sends mail as well. But, after some time, it crashes putting an entry in the EventLog as :

<<

Source : .NET Runtime 2.0 Error Reporting

EventID : 1000

Category : None

Description : Faulting application windowsservice1.exe version 1.0.0.0 stamp 4652715c, faulting module unknown, version 0.0.0.0, stamp 00000000, deubu 0, fault address 0x4de7649a.

>>

By not getting any hint from it, I added try/catch block in 100% code. But, I am still getting this problem.

Then, I used Adplus, and generated a Crash Dump using the following command line:

adplus.vbs -crash -pn WindowsService1.exe

After loading the crash dump in windbg, I got the following output:

Loading Dump File [D:\Program Files\Debugging Tools for Windows\Crash_Mode__Date_05-21-2007__Time_12-03-40PM\PID-192__WINDOWSSERVICE1.EXE__2nd_chance_AccessViolation__full_0d80_2007-05-21_12-39-55-359_00c0.dmp]
User Mini Dump File with Full Memory: Only application data is available

Comment: '2nd_chance_AccessViolation_exception_in_WINDOWSSERVICE1.EXE_running_on_EC4-RRD-148703'
Symbol search path is: D:\WinXPSP2Symbols;D:\rahul\PIA\PIAWindowsService\PIAWinService\RRD.MailDat.Processor\bin\Debug;D:\rahul\PIA\PIAWindowsService\PIAWinService\WindowsService1\bin\Debug
Executable search path is:
Windows XP Version 2600 (Service Pack 2) UP Free x86 compatible
Product: WinNt, suite: SingleUserTS
Debug session time: Mon May 21 12:39:55.000 2007 (GMT+5)
System Uptime: 0 days 4:19:18.974
Process Uptime: 0 days 0:37:15.000
.......................................................................................................
Loading unloaded module list
................
This dump file has an exception of interest stored in it.
The stored exception information can be accessed via .ecxr.
(c0.df4): Access violation - code c0000005 (first/second chance not available)
eax=7ffaa000 ebx=00000000 ecx=40000000 edx=00000043 esi=4de7649a edi=0975fea0
eip=4de7649a esp=0975fe3c ebp=0975fe64 iopl=0 nv up ei pl nz na po nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000202
<Unloaded_msado15.dll>+0x6649a:
4de7649a
0:012> kb
ChildEBP RetAddr Args to Child
WARNING: Frame IP not in any known module. Following frames may be wrong.
0975fe38 7e418734 00150d10 00000219 00000007 <Unloaded_msado15.dll>+0x6649a
0975fe64 7e418816 4de7649a 00150d10 00000219 user32!InternalCallWinProc+0x28
0975fecc 7e4189cd 00000000 4de7649a 00150d10 user32!UserCallWinProcCheckWow+0x150
0975ff2c 7e418a10 0975ff54 00000000 0975ff70 user32!DispatchMessageWorker+0x306
0975ff3c 77513787 0975ff54 00000000 77606c28 user32!DispatchMessageW+0xf
0975ff70 775270d5 00007530 7c802540 001c6248 ole32!CDllHost:Tongue TiedTAWorkerLoop+0x5c
0975ff8c 77527008 0975ffb4 774fe3dc 77606c28 ole32!CDllHost::WorkerThread+0xc8
0975ff94 774fe3dc 77606c28 948e43d5 001c6248 ole32!DLLHostThreadEntry+0xd
0975ffa8 774fe444 0376e57c 0975ffec 7c80b683 ole32!CRpcThread::WorkerLoop+0x1e
0975ffb4 7c80b683 001c6248 948e43d5 0376e57c ole32!CRpcThreadCache::RpcWorkerThreadEntry+0x1b
0975ffec 00000000 774fe429 001c6248 00000000 kernel32!BaseThreadStart+0x37

I am unable to understand the origin of this problem. Any help in this regard will be highly appreciated.

Regards,

Rahul




Re: Visual C# General C#.NET 2005 Windows Service Crashing automatically

Fernando Nunes

Hi Rahul,

I don't have a straight answer to your problem, but usually when a .NET program crashes straight on, not throwing exceptions that "you" can catch, it is usually something outside the Framework, or its inner workings, and most of the time, because of us, some code that isn't explicitly releasing objects or using stuff in a wrong way.

You seem to be using Interop for the Office thingie... do you: System.Runtime.InteropServices.Marshal.ReleaseComObject(interopObject) after your done using it

You can also try to use some kind of "Instrumentation" and make your windows service log its action into a text file or database, that way it might also help you to track down if it always crashes doing some kind of action, and who knows, narrow your scope of the problem.

Hope it helps,

Fernando Nunes





Re: Visual C# General C#.NET 2005 Windows Service Crashing automatically

rahulMCA

Hi Fernando,

Thanks for your response and suggestion. Yes, I am using the office interop and release memory using the following function for all COM objects.

<<

private void ReleaseCOMobject(Object obj)

{

try

{

while (System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) > 0)

{ ;}

}

catch (Exception ex)

{

_logger.WriteError("Exception at AccessQueryExecutor.ReleaseCOMobject() : " + ex.Message);

}

}

>>

I am also logging exceptions in a text file.....but it is coming as blank. Also, I have hooked the service start with AppDomain.CurrentDomain.UnhandledException event and logging it in a text file.

Thanks & Regards,

Rahul






Re: Visual C# General C#.NET 2005 Windows Service Crashing automatically

Dean Harding

My guess is also the Office interop assemblies. Try serializing access to the office objects -- office is a single-threaded application, and it doesn't like being run in a server with multiple threads accessing it at the same time.

If multi-threaded access is absolutely required, you'd be better off using spreadsheetML and writing the XML directly.





Re: Visual C# General C#.NET 2005 Windows Service Crashing automatically

rahulMCA

Hi Dean,

Thanks for your response and suggestion. I will try and let you know.

Regards,

Rahul






Re: Visual C# General C#.NET 2005 Windows Service Crashing automatically

rahulMCA

Hi,

I even tried commenting the complete call to Excel PIA, even though the service is crashing. I am wondering why the try/catch or atleast the text log file is not getting a hit. Even, the event handler for "AppDomain.CurrentDomain.UnhandledException" is not being hit.

Thanks & Regards,

Rahul






Re: Visual C# General C#.NET 2005 Windows Service Crashing automatically

Dean Harding

Can you attach a debugger If so, bring up the "Exceptions" dialog (Ctrl+Alt+E by default) and check the "Thrown" column next to "C++ Exceptions"

Doing that should cause the debugger to break as soon as the exception is thrown (i.e. it hooks the first-chance). If you've attached in mixed-mode, you might even get a useful stack trace...





Re: Visual C# General C#.NET 2005 Windows Service Crashing automatically

IsshouFuuraibou

make sure to do _logger.Flush();

Otherwise you're not guaranteed that the pipe will be cleared before it is collected/finished. It'd also be a good idea to make sure the file gets closed when you're program is terminated, but that is less important than making sure you flush your messages from the pipe to the file.





Re: Visual C# General C#.NET 2005 Windows Service Crashing automatically

rahulMCA

Hi IsshouFuuraibou,

Thanks for your reply and suggestion. In my app, _logger is writing to Windows Event Log. However, whereever I am using file logging, I am flushing the pipe before closing the file.

Thanks & Regards,

Rahul






Re: Visual C# General C#.NET 2005 Windows Service Crashing automatically

rahulMCA

Hi Dean,

Thanks for your reply and suggestion. I tried doing as you suggested. However, it didn't help. The Debugger is not breaking.

Regards,

Rahul






Re: Visual C# General C#.NET 2005 Windows Service Crashing automatically

rahulMCA

Hi All,

If I am blocking code that calls MS-Access which is being done using OLEDB.NET, ADODB and ODBC.NET, the service is not crashing. Anybody has idea about problems using these libraries from windows service

Thanks & Regards,

Rahul






Re: Visual C# General C#.NET 2005 Windows Service Crashing automatically

Dean Harding

Are you using your ReleaseCOMobject method on those ADODB instances Your stack trace indicates that the msado15.dll has been unloaded, which would be the case if you're doing ReleaseComObject while other people are still trying to use it.

Calling ReleaseComObject in a loop is actually very dangerous. You have to make really sure that nobody has any references to the RCW when you do it, otherwise you end up with these sorts of crashes.





Re: Visual C# General C#.NET 2005 Windows Service Crashing automatically

rahulMCA

Hi Dean,

Thanks for your reply and Suggestion.

Yes, I am using ReleaseCOMObject() on ADODB instances as well. However, presently, I am the only person running this service in my desktop, hence, there is no chance that other people would be trying to use it (msado15.dll).

Also, previously, I was writing only a single line(without any loop) to release a COM object, even though the service was crashing. I thought may be some COM objects may not have released, so put it in a loop.

Thanks & Regards,

Rahul






Re: Visual C# General C#.NET 2005 Windows Service Crashing automatically

Haris Mushtaq

I came accross your thread while searching for the solution of a similar problem.

I am using Neurotec VFinger SDK used for biometric security systems. I tried to implement it in a windows service on .NET 2.0 but it failed. The error in server explorer event logs appeared to be. AccessViolationException, trying to read or write protected memory.

To test the code itself, I build a windows application and copied the same code there. It was working fine. So it appears that there are some security issues with interop (Yes!, the api is win32 and has a .NET wrapper that I am using in my app. However, a particular function did not have a wrapper and I used DllImport to load it and it is that line where exception is occuring).






Re: Visual C# General C#.NET 2005 Windows Service Crashing automatically

rahulMCA

Dear All,

I got solution for my problem. Actually, for ADODB, if you open and close connections for a long running process (e.g. a windows service), ADODB itself throws exception not visible within our threads. Hence, the trick it, once you open a connection using ADODB, don't close it at all for these long running processes.

Regards,

Rahul