Writing

Software, technology, sysadmin war stories, and more. Feed
Thursday, November 24, 2011

Circular C++ class pointers are evil

If you write C++ code, please don't do this.

Server::States::Def* def = new Server::States::Def;
 
(*def)[Things::STATE_INIT] = new ThingStateInit(this);
(*def)[Things::STATE_WARMUP] = new ThingStateWarmup(this);
(*def)[Things::STATE_RUN] = new ThingStateRun(this);

See the problem yet? By passing "this" in, you have given that class a pointer back to yourself. So now instead of just having class A call class B, you also have class B calling back into class A as well.

Try following that flow when it's time to debug something!