Thursday, January 20, 2005

This behaviour is by design

I've just spent a couple of hours tracking a bug in some code I wrote. I use the MFC CMonthCalCtrl control to input a range of dates for use in a database search. I'm also using CRecordSet (yeah yeah, I know) and a parameterised query. Because I'm using CRecordSet I need to use CTime's for the date parameters. Thus it made sense to map the CMonthCalCtrl to a CTime using DDX.

Worked fine in debug; in release it was adding about 4 years to the desired date. After a bit of headscratching I tracked down this KnowledgeBase[^] article. And that, of course was the problem. In debug mode the compiler was zeroing the SYSTIME structure, in release it wasn't and the random garbage on the stack added a bias of about 4 years to the structure passed to the CTime conversion constructor.

I love the comment at the end of the KB article - this behaviour is by design. Uh huh. I make it a bug because it means you can't use the CTime overloads in release mode.

No comments: