Sign in to follow this  
desmike481

Access Violation With Member Function Pointer

Recommended Posts

Hello, I am having a hard time with a member function pointer. I'm coded them before so I'm not new to the concept. So in my AppWindow class, I defined a member function pointer (and associated object) as follows: private: void (WinMouse::*mousePaintCursorFunc)(void); WinMouse* mouseObj; My first issue comes at initialization. AppWindow::AppWindow(...var list...) : mouseObj(0), mousePaintCursorFunc(0) { ... etc } When I stop the debugger after the initialization list, the mouseObj = 0x00000000. But the mousePaintCursorFunc sometimes is 0xcdcdcdcd or 0x00000000. It's not consistent with each run. I have another member function pointer defined similarly (but it has 4 variables, where this one has none), and it always initalizes to 0, and it works 100% of the time. Ok - so then later the function pointer gets set to the address of a function from another class. The assignment goes like this: myWindow->mousePaintCursorFunc = &WinMouse::paintCursor; And that function is defined in WinMouse.h as: public: virtual void paintCursor(); At this point, if the value from initialization of mousePaintCursorFunc was 0xcdcdcdcd, this step looks to actually assign the function address. If the variable is 0x00000000, then the assignment looks to completely fail, leaving the value at 0x00000000. This is also wierd. At the time of execution, the code is again straight forward: (mouseObj->*mousePaintCursorFunc)(); In either case, this step fails with "Unhandled exception at 0x00000000 in TestApp.exe: 0xC0000005: Access violation." Please help... I've spent hours string at the screen looking for my mistake but nothing. When I try to call mouseObj->paintCursor() from the spot I use the member function, it works. The member function should work but doesn't. Even the first initalization of the variable seems off. Please advise. Thanks! [Edited by - desmike481 on December 23, 2009 8:06:40 PM]

Share this post


Link to post
Share on other sites
Hello - subsequent to my first post, I discovered that the problem with the execution is directly related to the assignment of the function occurring from within another class.

When I assign the function within another class:

myWindow->mousePaintCursorFunc = &WinMouse::paintCursor;

the execution later fails.

When I do the assignment within the same class I defined it,

mousePaintCursorFunc = &WinMouse::paintCursor;

, the execution works.

So, is there some other way to assign it from another class or do I have to assign it within the class it was defined in? strange... The initialization of it is still screwey either way...

Thanks

Share this post


Link to post
Share on other sites
You seem to struggle with a detail:

You are using a member function pointer. A non-static member function pointer must always have a valid instance of the containing class (the this pointer) available. You seem to set the instance to the static function though (without a WinMouse instance to back it up).

Therefor doing the assignment inside the instanced object you do get an invisible this pointer set, with the other statement you don't.

I'm not sure however on how the syntax is for assigning a member function pointer outside of the containing class. Might be similar to this though:

myWindow->mousePaintCursorFunc = &mouseObj->paintCursor;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this