Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Library : Other (not classified elsewhere) » Class method called by keyboard hook callback has an empty "this" pointer
Class method called by keyboard hook callback has an empty "this" pointer [message #37334] Sun, 23 September 2012 11:31 Go to next message
Mircode is currently offline  Mircode
Messages: 17
Registered: September 2012
Promising Member
Hello!

I managed to create a working low level keyboard hook on windows. Now I want to update a label in my GUI everytime something happens.

In order to access my main window class from the hook callback, I created a global variable, which points to this class:

KeyBuddy2* MainWindow;


Inside the constructor I define it

KeyBuddy2::KeyBuddy2()
{
	MainWindow=this;
	CtrlLayout(*this, "Window title");
	SetHook();
}


Inside the LowLevelKeyboardProc function, I call

MainWindow->ProcessKbdEvent(...)


but inside the ProcessKbdEvent method, "this" does not point to MainWindow. Instead, it is a nullpointer.

I am happy that I found this problem to be the cause of my program crashing all the time... I wanted to access class members from inside this method.

As a workaround, I can write MainWindow->member instead of just member. But still, this should not be necessary. Does anyone know the cause of this behavior?

Greetings,
Mirko
Re: Class method called by keyboard hook callback has an empty "this" pointer [message #37335 is a reply to message #37334] Sun, 23 September 2012 12:51 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi Mirko,

Welcome to the forum Cool

Mircode wrote on Sun, 23 September 2012 11:31

Inside the LowLevelKeyboardProc function, I call

MainWindow->ProcessKbdEvent(...)


but inside the ProcessKbdEvent method, "this" does not point to MainWindow. Instead, it is a nullpointer.


This would most probably happen if MainWindow==null at the time of calling MainWindow->ProcessKbdEvent(). You can add a check for this in LowLevelKeyboardProc to confirm this. I don't know how your code is structured, but I believe that the function is called before any instance of KeyBuddy2 is created or something like that.

Best regards,
Honza

[Updated on: Sun, 23 September 2012 12:52]

Report message to a moderator

Re: Class method called by keyboard hook callback has an empty "this" pointer [message #37336 is a reply to message #37335] Sun, 23 September 2012 18:45 Go to previous messageGo to next message
Mircode is currently offline  Mircode
Messages: 17
Registered: September 2012
Promising Member
Hi, thanks!

You were right, MainWindow was 0. The problem was that I didn't know about the "extern" keyword. Each cpp file hat his own "global" MainWindow variable.

Now it works fine.
Re: Class method called by keyboard hook callback has an empty "this" pointer [message #37337 is a reply to message #37336] Sun, 23 September 2012 20:27 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

You're welcome. In U++ it is fairly common to use a static variable in global function instead of global variables to prevent all kinds of troubles that global variables cause.

In your case, you can do something like
KeyBuddy2* MainWindow() {
    static KeyBuddy2* ptr;
    return ptr;
}
...
KeyBuddy2::KeyBuddy2()
{
	MainWindow()=this;
	CtrlLayout(*this, "Window title");
	SetHook();
}
...
MainWindow()->ProcessKbdEvent(...)


There is even a macro for this: GLOBAL_VAR(type, name), for those that are too lazy to write Smile (Such as me Wink )

Honza
Re: Class method called by keyboard hook callback has an empty "this" pointer [message #37343 is a reply to message #37337] Mon, 24 September 2012 18:25 Go to previous messageGo to next message
Mircode is currently offline  Mircode
Messages: 17
Registered: September 2012
Promising Member
Uuh, static you say. That looks cool. I didn't know this concept. I should really read through a c++ book someday.

I bet this was invented by people who used global variables all the time but then they were forbidden to do so because its bad style.

So they came up with something that secretly incorporated the usefulness of globals and looks object oriented to the outside. Smile

Thanks again.
Re: Class method called by keyboard hook callback has an empty "this" pointer [message #37353 is a reply to message #37343] Wed, 26 September 2012 01:16 Go to previous message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
Welcome to the forum.

Mircode wrote on Mon, 24 September 2012 09:25

I should really read through a c++ book someday.

I bet this was invented by people who used global variables all the time but then they were forbidden to do so because its bad style.


Global variables are not forbidden. Local variables are safer and takes less memory.

U++ will be good for you to learn C++ but you do need to do some reading to learn the basics and concepts.
Previous Topic: [bug report] EditField::StdBar Undo menu not checked and EditField::RightDown is reentrant
Next Topic: ZMQ library in U++ (inproc, IPC, TCP and multicast)
Goto Forum:
  


Current Time: Fri Dec 13 21:45:35 CET 2024

Total time taken to generate the page: 0.01904 seconds