Thread: crash after upgrade

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    crash after upgrade

    Consider the following code which closes an overlapped I/O serial handle during application shutdown.

    Code:
    Win32Com.CancelIo(hPort);
                
    Win32Com.CloseHandle(hPort);
    It works fine under .NET 2.0 but after switching to .NET 4.0 it crashes on the CloseHandle. Removing CancelIO doesnt help.

    Does anyone know what the correct way is to close an overlapped I/O handle? And why is there the difference between NET2.0 and 4.0?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Can't speak for .net, but in C/C++, you should call GetOverlappedResult() with the last param TRUE. This ensures that any pending async operations are complete before closing the handle.

    gg

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Knowing the exception that's thrown when it crashes would help determine the cause...
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    It's an access violation, not a .NET exception

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by Codeplug View Post
    Can't speak for .net, but in C/C++, you should call GetOverlappedResult() with the last param TRUE. This ensures that any pending async operations are complete before closing the handle.

    gg
    GetOverlappedResult helped indeed, there were still bytes pending to be written. Thanks!

    So the correct order of closing a serial port would be:

    GetOverlappedResult(hPort, ptrUWO, out sent, checkSends)
    CancelIo(hPort);
    CloseHandle(hPort);

    Correct?

    *edit*
    Hmmm it still sometimes crashes only a lot less
    Last edited by KIBO; 11-07-2012 at 02:08 AM.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    While overlapped IO is pending, you need to make sure that your OVERLAPPED structure, Event handle, and src/dest buffers are all valid. So before any of those resources are released, you need to make sure that all pending IO has completed and the easy way to do that is to call GetOverlappedResult with the last param TRUE.

    If you are ready to "get out" and don't care about any pending IO then you can:
    CancelIo()
    GetOverlappedResult( , , , TRUE)

    If you call GetOverlappedResult( , , , TRUE) first, then there is no pending IO so CancelIo() doesn't need to be called.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RAM upgrade
    By BobS0327 in forum Tech Board
    Replies: 6
    Last Post: 12-10-2008, 08:14 AM
  2. Upgrade wxGTK to 2.8*
    By IdioticCreation in forum Linux Programming
    Replies: 1
    Last Post: 06-21-2007, 01:46 PM
  3. Upgrade installed
    By webmaster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-01-2002, 12:16 PM
  4. Upgrade available?
    By BellosX in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-11-2001, 03:06 AM