Tuesday, May 12, 2009

Another lesson learned

8 hours brain frying debugging finally solved a crashed bug. yes it's a single bug reported in Bugzilla, but it's actually contributed by many sources.
  1. Singleton object, one of the third party library use singleton and my application is multithreaded, so they were on fire. lesson learned: singleton is bad
  2. Smart pointer, another third party uses smart pointer to manage memory, therefore depends on state it might help me clean up my memory :). lesson learned: raw pointer isn;t that difficult then the behavior is better understood so use more raw pointer
  3. Unreset windows event, if a event was created with manual reset attribute, the state will stay set until it is manually resetted, this make your program only work correctly at the first time
  4. thread id, GetThreadId() is only available on window vista and 64bit xp. on xp stored the id yourself. and the question is where to get the id? if you used _beginthread or _beginthreadex, it is the thrdadd parameter
  5. WM_USER event doesn't dispatch by DispatchMessage(), the reason still a big mistery, hopefully i could find out soon
  6. if you got interlocking problem while you delete child window with DestroyWindow(), try set exstyle to WS_EX_NOPARENTNOTIFY when you create child window, this could prevent the interlocking. (more testing needed)
  7. the infamous, interface class no virtual destructor problem, see my previous post
  8. and many more careless mistakes that do not worth to mention here
But the problem still not fully solved yet, such as keyboard input become not responsive if multiple instances exist. and closes multiple instances at once may still cause the crash. I guess QA still need to curse and swear me for some time :)

Wednesday, May 6, 2009

destructor of interface derived class will never get called

interface struct in C++ is a structure contained only pure virtual functions. no data member shall be declared in interface struct. and because of this no constructor and destructor needed. 

here come the pitfall, consider this

struct IBase
{
virtual void doSomething() = 0;
}

class Derived : public IBase
{
Derived() { text = new char[64]; }
virtual ~Derived() { delete [] text; }

virtual void doSomething() { printf("%s", text);
}

IBase *obj = new Derived();
delete obj;

It was looking perfectly fine to me, until i placed a break point in the derived class destructor. one of the solution is to add a virtual destructor to IBase, but this is ugly and unbalance. the solution I adopted was use self made init() and uninit() insteads.

Monday, May 4, 2009

OpenGL Stack overflow

This strange problem bugging me for some time. the scenario is create opengl rendering context on an existing window, opengl render successfully until the window resize or window bring to top, the window crash immediately after that.

The problem was caused by the opengl context was created on a window that wasn't build for opengl, there are some requirement for a opengl ( i got the hint from this MSDN help)

The solution is create a child window for the exisitng window, the settings work for me was:

[quote]
exStyle = WS_EX_TOPMOST;
style = WS_CHILD | WS_VISIBLE;
[/quote]