CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2005
    Posts
    125

    Question OpenSemaphore Fails occassionally.

    Hi forum.

    I have the following code inside one function.

    Code:
          if ((my_os_resource_handle = OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,"Global\\ABC32$SEMAPHORE$%c")) == NULL) 
    {
    error handling
    }

    This function gets called many times.

    For the first time in years, we have seen the problem in a particular system
    This call failed three times over a span of two days and to get the program working, the entire system had to be rebooted.

    The GetLastError always returned 2

    Details of the system.
    Windows 2003 Professional

    Any thoughts on the same? .... is there any method that I can reproduce the File not available (Error code 2) situation so that I can understand more ....... in other words, are there any techniques to manipulate, access kernel objects ?

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: OpenSemaphore Fails occassionally.

    Maybe you accidently did something that made the calls to CreateSemaphore, OpenSemaphore & CloseHandle unbalanced? From MSDN http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx
    The semaphore object is destroyed when its last handle has been closed
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: OpenSemaphore Fails occassionally.

    Quote Originally Posted by softmessager View Post
    For the first time in years, we have seen the problem in a particular system. This call failed three times over a span of two days and to get the program working, the entire system had to be rebooted.
    Well, there was a bug or if not a bug, an oversight in your program that finally became exposed.

    1) As S_M_A pointed out, is your synchronization done correctly?

    2) Do you check for all return values from all Windows API and other library functions?

    One reason a lot of programs fail is that false assumptions are made when it comes to calling API functions. The belief is that the function will work, thereby no code is written to check for the return value (or exception if it's a C++ based API). Then that fateful day when these "safe" API function fails, the code reacts by doing nothing and proceeding as if the function didn't fail. Then things fall apart from there.

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured