By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,581 Members | 1,975 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,581 IT Pros & Developers. It's quick & easy.

Pointer-to-member-function code okay?

P: n/a
I am compiling under Visual C++ 2005 Express (with the Platform SDK

I have a class that is responsible for creating and repeatedly running
a Timeslice() method using a thread:

class cProcessBlock : public cBase
. . .
virtual void Timeslice(); // executed repeatedly by a 'while' loop
in a thread

I also declare a function pointer typedef to handle cProcessBlock callbacks:

typedef void (cProcessBlock::*PortCallbackFnPtr)(shared_ptr<cBa se>);

cProcessBlock has a descendant, cPort that registers callback methods
(in an STL map) and is responsible for calling these when objects are

class cPort : public cProcessBlock
. . .
void cPort::Timeslice()
// Continuously receive new objects and process using registered
shared_ptr<cBase> pNewObject;
pNewObject = Receive();

// Call appropriate callback,
// depending on the received object's class name
if (pNewObject) {
PortCallbackFnPtr CallbackFn =
if (CallbackFn) // if callback handler is found
map<string, PortCallbackFnPtr> m_CallbackRegistry;

I then define a descendant of cProcessBlock and create an instance:

class cBlockRx : public cProcessBlock {
cBlockRx(. . .) {
m_pPort = shared_ptr<cPort>(new cPort);
(&cBlockRx::RxCallback), "cSomeObject");"Test.txt", ios::out);
void RxCallback(shared_ptr<cBase> pRx) {
m_streamOutput << "Got something" << endl << flush;

shared_ptr<cPort> m_pPort;
ofstream m_streamOutput;
} BlockRx;

When I start up the code that causes BlockRx to receive cSomeObjects,
the callback is entered correctly, although the output to the file
stream (m_streamOutput) bombs with an unhandled exception (access
violation) deep in the MS ostream code (the line begins "streamsize _Pad").

It bombs when I try a couple of other options in the callback too, which
leads me to suspect that I'm doing something nasty to the stack.

I guess I'm asking if the function pointer code above looks good, or if
there are reasons why I might be inviting "undefined behaviour".

I have also tried:
((*this).*static_cast<void(cProcessBlock::*)(share d_ptr<cBase>)>(CallbackFn))(pNewObject);
and ((*(static_cast<cProcessBlock*>(this))).*(Callback Fn))(pNewObject);
at the point where the callback is called. These were no better.

Thanks for any help!

Mar 17 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.