Thanks to Andreas Grabs for proposing a fix.
This feeds straight into context_module::instance() and I was inadvertantly using the forum->id, rather than the cm->id.