Page 1 of 1

Display Plugin: Bug to Watch out For

Posted: September 29th, 2009, 6:49 am
by thaimin
I was writing a display plugin and it was working great until one day I went into standby and resumed and I got some very weird behaviors.

LCD Smartie essentially was calling the DISPLAYDLL_Init function twice upon resuming, and the second call was always DURING the first call. My code was not prepared for this, and thus I was trying to open the COM port twice, failing the second time, but in the mean time locking the COM port and preventing any further action.

*** So, the tip is: RIGHT before opening the COM port, check if you already opened it. It was not good enough to check at the beginning of the DISPLAYDLL_Init function.

While identifying this problem, I came across the reason why this happens.
  • you call some function in DISPLAYDLL_Init
  • which calls PeekMessage (directly or indirectly)
  • which causes the PowerBroadcast message to be RE-SENT to LCD Smartie (because it hasn't consumed it yet...)
  • which makes LCD Smartie realize that it should re-initialize the display
  • which causes the DISPLAYDLL_Init function to be called again, from the same thread
This whole odd chain of events ONLY happens once and ONLY after resuming from standby.
The functions that caused me problems were CoInitializeEx and IWbemLocator::ConnectServer (used for WMI queries)
The function bsearch (binary search) calls PeekMessage as well, so even mundane functions may cause this chain of events!

The best fixes for this would be within LCD Smartie itself:
  • Run DISPLAYDLL_Init in a separate thread (allowing LCD Smartie to consume the PowerBroadcast message immediately)
  • Record that a specific PowerBroadcast message has already been received and ignore it
If anyone actually plans on fixing this problem (LCD Smartie core development seems to be dead) I can give you the stack trace of what happens.

Re: Display Plugin: Bug to Watch out For

Posted: October 4th, 2009, 10:29 am
by mattcro
Are you developing with / using Smartie 5.4.1? This "beta" version includes a fix for COM port restart issues and solved the problem for me when resuming from standby and hibernate.

This may well be the issue you are seeing with your plugin, but I don't know for sure...

http://forums.lcdsmartie.org/viewtopic. ... 96&p=12097
http://lcdsmartie.sourceforge.net/smartied.htm

Re: Display Plugin: Bug to Watch out For

Posted: August 3rd, 2010, 4:28 am
by thaimin
I hit this bug as well. Through some very difficult sleuthing I found out that it was because of a call to CoInitializeEx or ConnectServer (there may be others, but in my code these were to blame). LCD Smartie already has called CoInitializeEx during resume and this call causes LCD Smartie to think that it is waking up again, thus the reason it calls Init again while you are still in Init. I first fixed it by using your method. No my code doesn't depend on those functions anymore.