{"id":43693,"date":"2014-11-05T07:00:00","date_gmt":"2014-11-05T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/11\/05\/passing-the-incorrect-object-type-for-a-handle-how-bad-is-it\/"},"modified":"2014-11-05T07:00:00","modified_gmt":"2014-11-05T07:00:00","slug":"passing-the-incorrect-object-type-for-a-handle-how-bad-is-it","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20141105-00\/?p=43693","title":{"rendered":"Passing the incorrect object type for a handle, how bad is it?"},"content":{"rendered":"<p>A customer asked a somewhat strange question: &#8220;We are calling <code>Set&shy;Event<\/code> but passing the handle to a waitable timer. Application Verifier reports, &#8220;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms220969(v=VS.90).aspx\">Incorrect object type for handle<\/a>.&#8221; But the code works well. We want to know the risks of passing the wrong object type to <code>Set&shy;Event<\/code>. Is the recommendation only to pass handles of type &#8220;Event&#8221; to <code>Set&shy;Event<\/code>?\n Let&#8217;s answer those questions in reverse order.\n Yes, the recommendation is only to pass handles of type &#8220;Event&#8221; to <code>Set&shy;Event<\/code>, just as the recommendation is only to pass handles of type &#8220;Semaphore&#8221; to <code>Release&shy;Semaphore<\/code>, and more generally, only to pass valid parameters to functions.\n What is the risk of passing the wrong object type? You&#8217;re lucky that the kernel does object type validation before proceeding, so your error is caught during parameter validation and the function fails with the error <code>ERROR_INVALID_HANDLE<\/code> (or status code <code>STATUS_OBJECT_TYPE_MISMATCH<\/code>, if the function returns status codes instead of error codes).\n Of course, if you are encountering this problem only because you are using a handle after closing it (and then the handle got recycled as a timer handle), then you merely got lucky. Maybe tomorrow you won&#8217;t be so lucky, and the handle will get recycled as another unrelated event. Tomorrow, your <code>Set&shy;Event<\/code> call will succeed and <i>set some other guy&#8217;s event<\/i>. This will probably cause that other guy to get really confused. &#8220;This event is set when the modulator has finished calibrating. But the event is getting signaled before the calibration is complete, so my code ends up using an uncalibrated modulator! I set a breakpoint on my <code>Set&shy;Event<\/code> call, and it never fires, yet the event is set. Help me debug this. I&#8217;ve spent a week trying to figure out what&#8217;s wrong!&#8221;<\/p>\n<p> As to the final remark, &#8220;But the code works well,&#8221; it&#8217;s not clear what the customer meant by that. What does &#8220;works well&#8221; mean in this context? Do they mean, &#8220;The event is successfully set even though it&#8217;s not an event&#8221;? How can you successfully perform an event operation on something that isn&#8217;t an event? Or perhaps they mean, &#8220;Our code seems to work okay in spite of this mistake.&#8221; The operative phrase there is &#8220;seems to&#8221;. It may seem to work well, but someday it won&#8217;t, and at the most inconvenient time. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer asked a somewhat strange question: &#8220;We are calling Set&shy;Event but passing the handle to a waitable timer. Application Verifier reports, &#8220;Incorrect object type for handle.&#8221; But the code works well. We want to know the risks of passing the wrong object type to Set&shy;Event. Is the recommendation only to pass handles of type [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-43693","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer asked a somewhat strange question: &#8220;We are calling Set&shy;Event but passing the handle to a waitable timer. Application Verifier reports, &#8220;Incorrect object type for handle.&#8221; But the code works well. We want to know the risks of passing the wrong object type to Set&shy;Event. Is the recommendation only to pass handles of type [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/43693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=43693"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/43693\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=43693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=43693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=43693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}