wxTimer not calling overriden Notify() - c++11

I'm running into an issue where I implemented a derived wxTimer class to override the Notify() call since I'm not using an owner implementation as described in the documentation.
When I debug the run, I can see
the timer is being instantiated
my_timer_instance->IsRunning() returns true
MyTimer::Notify() is never called
This leads me to believe that the timer is being set and running, but when it expires it's calling the base class Notify() procedure and not my override it's not calling notify() but I'm not sure why.
EDIT: I added frame->getTimer()->Notify(); to my app and the correct procedure was called. Therefore, the timer just isn't calling Notify when it expires.
EDIT2: Added this minimal working example, and the timer works as expected. I'll try to compare the two and see what the problem is.
MyApp.hpp
#pragma once
#ifndef __NONAME_H__
#define __NONAME_H__
#include <wx/artprov.h>
#include <wx/xrc/xmlres.h>
#include <wx/statusbr.h>
#include <wx/gdicmn.h>
#include <wx/font.h>
#include <wx/colour.h>
#include <wx/settings.h>
#include <wx/string.h>
#include <wx/frame.h>
#include <wx/timer.h>
///////////////////////////////////////////////////////////////////////////
class MyTimerClass : public wxTimer
{
wxFrame* MyFrame;
public:
MyTimerClass(wxFrame* frame): MyFrame(frame) {};
void Notify() override;
};
///////////////////////////////////////////////////////////////////////////////
/// Class MyFrame1
///////////////////////////////////////////////////////////////////////////////
class MyFrame1 : public wxFrame
{
private:
protected:
wxStatusBar* m_statusBar1;
MyTimerClass* MyTimer;
public:
void StartTimer(int TimeInSeconds);
MyFrame1(wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize(500, 300), long style = wxDEFAULT_FRAME_STYLE | wxTAB_TRAVERSAL);
~MyFrame1();
};
#endif //__NONAME_H__
MyApp.cpp
#include "MyApp.hpp"
#include "wx/wxprec.h"
// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
///////////////////////////////////////////////////////////////////////////
void MyTimerClass::Notify()
{
MyFrame->SetStatusText("Timer popped", 0);
}
MyFrame1::MyFrame1(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style) : wxFrame(parent, id, title, pos, size, style)
{
MyTimer = new MyTimerClass(this);
this->SetSizeHints(wxDefaultSize, wxDefaultSize);
m_statusBar1 = this->CreateStatusBar(1, wxSTB_SIZEGRIP, wxID_ANY);
this->Centre(wxBOTH);
this->StartTimer(5);
}
void MyFrame1::StartTimer(int TimeInSeconds)
{
SetStatusText("Timer started with " + std::to_string(TimeInSeconds) + " seconds.");
MyTimer->Start(TimeInSeconds * 1000);
}
MyFrame1::~MyFrame1()
{
}
// ----------------------------------------------------------------------------
// resources
// ----------------------------------------------------------------------------
// the application icon (under Windows it is in resources and even
// though we could still include the XPM here it would be unused)
#ifndef wxHAS_IMAGES_IN_RESOURCES
#include "../sample.xpm"
#endif
// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------
class MyApp : public wxApp
{
public:
virtual bool OnInit() wxOVERRIDE;
};
enum
{
// menu items
Minimal_Quit = wxID_EXIT,
Minimal_About = wxID_ABOUT
};
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
// call the base class initialization method, currently it only parses a
// few common command-line options but it could be do more in the future
if (!wxApp::OnInit())
return false;
// create the main application window
MyFrame1 *frame = new MyFrame1(NULL, -1, "Test Frame");
frame->Show(true);
return true;
}

#BobbyTables,
From the documentation:
This member should be overridden by the user if the default
constructor was used and SetOwner() wasn't called.
Is it the case?

Nothing seems to be wrong in the code you show (although I'd change a few things, such as using raw pointer for my_timer_instance), so the problem must be elsewhere. As usual, the best would be to come up with a SSCCE, without it I can only offer some guesses as to what the problem actually is.
Are you running the event loop? The timers will only fire when it's running, so if you block doing some computation, this wouldn't happen.
Also, what is frame in Notify()? Is this a global (I'd rather pass it as parameter to MyTimer ctor)?

So after mimicking the code provided in the question, the following changes were made:
Instead of using a getter and setter to access the private timer member, I instead use
void refreshTimer(int time_in_seconds) in my parent frame class and create the timer in the parent frame's constructor rather than letting the app create it and pass it in.
I don't see why either of those two things would change the behavior of the timer but the timer now works as expected. I apologize for not being able to identify a concrete bug as the source of the problem.
NOTE: This behavior was caused by the timer being invoked outside the wxwindow's thread. Be careful when creating multithreaded programs using wxwidgets as a GUI. To circumvent this issue since I needed the timer to be invoked in a different thread, I created my own timer class that works correctly.

Related

How to make an internal header when using interface + templates for dependency injection

In this blog post about dependency injection in C++, the author explain a hybrid approach that uses both templates and interfaces as follows:
ICar.h (publicly visible):
#pragma once
struct ICar
{
virtual void Drive() = 0;
virtual ~ICar() = default;
};
std::unique_ptr<ICar> MakeV8Car();
std::unique_ptr<ICar> MakeV6Car();
Car.h (internal):
#pragma once
#include "ICar.h"
template <typename TEngine>
class Car : public ICar
{
public:
void Drive() override
{
m_engine.Start();
// drive
m_engine.Stop();
}
private:
TEngine m_engine;
};
Car.cpp:
#include "Car.h"
#include "V8Engine.h"
#include "V6Engine.h"
std::unique_ptr<ICar> MakeV8Car()
{
return std::make_unique<Car<V8Engine>>();
}
std::unique_ptr<ICar> MakeV6Car();
{
return std::make_unique<Car<V6Engine>>();
}
All of which makes good sense to me, except for the internal part. Let's assume I've created a shared object from the above.
How is Car.h private in the context of this shared object?
I've read on the meaning of a private header in the answer which states:
Keep your data in MyClassPrivate and distribute MyClass.h.
Presumably meaning to not distribute MyClass_p.h, but how does one avoid distributing a header file and still have the .so work?

Periodically hid_hw_raw_request in kernel module, how to?

I'm writing a kernel module that need to ask an hid raw device periodically.
I tried hrtimer and a simple timer and each time I call hid_hw_raw_request I got a "BUG: scheduling while atomic".
If I try the same function outside my timer function (eg in the init), it works fine (no bug).
How could periodically call this function without generating any bug ?
You need to use a work queue to issue your hid_hw_raw_request as deferred work. This can be done as in the following example module:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/workqueue.h>
static void hid_work_handler(struct work_struct *hid_work);
static struct workqueue_struct *hid_workqueue;
static DECLARE_WORK(hid_work, hid_work_handler);
static void hid_work_handler(struct work_struct *hid_work)
{
...
hid_hw_raw_request(...);
...
}
static int __init hid_work_init(void)
{
if (!hid_workqueue)
hid_workqueue = create_singlethread_workqueue("hid_workqueue");
if (hid_workqueue)
queue_work(hid_workqueue, &hid_work);
return 0;
}
static void __exit hid_work_exit(void)
{
if (hid_workqueue) {
flush_workqueue(hid_workqueue);
destroy_workqueue(hid_workqueue);
}
}
module_init(hid_work_init);
module_exit(hid_work_exit);
MODULE_DESCRIPTION("hid_work_test");
MODULE_LICENSE("GPL");
Note that for the real implementation you'll need to create your own data struct with an included struct work_struct to be queued. This data struct will likely contain the hiddev, buffer, etc. that the hid_work_handler needs to do the actual transfer. See LDD3 Chapter 7 for more details (albeit syntax of calls is outdated, the basic explanation still applies).

Callback pattern with a Functor

I'm trying to wrap an HttpRequest object (from Cocos2d-x) in my own functor. Everything's working fine except calling the callback passed to my functor. Can you spot the error in the classes below? (I only pasted the relevant parts of the code).
Cloud.hpp:
#ifndef Cloud_hpp
#define Cloud_hpp
#include "external/json/document.h"
#include "network/HttpClient.h"
using namespace cocos2d::network;
typedef std::function<void()> CloudCallback;
class Cloud
{
private:
std::string url { "http://localhost:3000/1.0/" };
std::string end_point;
CloudCallback callback;
std::string getPath();
void onHttpRequestCompleted(HttpClient *sender, HttpResponse *response);
public:
Cloud (std::string end_point) : end_point(end_point) {}
void operator() (CloudCallback callback);
};
#endif /* Cloud_hpp */
This is the class that stores the callback passed in the constructor. Here's the implementation:
#include "Cloud.hpp"
#include <iostream>
std::string Cloud::getPath()
{
return url + end_point;
}
void Cloud::operator()(CloudCallback callback)
{
this->callback = callback;
std::vector<std::string> headers;
HttpRequest* request = new (std::nothrow) HttpRequest();
request->setUrl(this->getPath().c_str());
request->setRequestType(HttpRequest::Type::GET);
request->setHeaders(headers);
request->setResponseCallback(CC_CALLBACK_2(Cloud::onHttpRequestCompleted, this));
HttpClient::getInstance()->send(request);
request->release();
}
void Cloud::onHttpRequestCompleted(HttpClient *sender, HttpResponse *response)
{
this->callback();
}
What I'm trying to do is, make a simple Http request with the help of a functor, calling like this:
Cloud cloud("decks");
cloud([&]() {
CCLOG("Got the decks");
});
I'm getting EXC_BAD_ACCESS(Code=EXC_I386_GPFLT) as soon as the line
this->callback();
is called.
What is it that I am doing wrong here?
EDIT: Now I guess it's something to do with threads. If I remove the HttpRequest and call the callback method passed to the operator() immediately, this works without any problems. Begging for help :-)
It looks like the problem could be a lifetime issue. Since the http response callback is called asynchronously, some objects may have been destroyed in the meantime. There are two possibilities:
The Cloud object itself is destroyed before the callback is called.
One or more objects referenced by the lambda (since you're capturing by reference), may have also gone out of scope and been destroyed.
Try this:
void Cloud::operator()(const CloudCallback &callback)

asio socket, inside a class declaration, scope issue

I want to have a class inside which I initialize and open a tcp socket, but I want to repeatedly write to the socket only by calling a method of that class, without having to re-establish connection everytime.
My code is below. I get the compile time errors as pasted here below the code block.
The method is unable to see the socket that was initialized in the constructor. Seems to be a simple C++ "scope" issue, but beats me!
#include <ctime>
#include <iostream>
#include <string>
#include <asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using asio::ip::udp;
using asio::ip::tcp;
class rpcClass {
char sendBuffer[16];
asio::ip::tcp::endpoint epdPt;
asio::io_service io_service;
asio::ip::tcp::endpoint endPt;
public:
rpcClass () { // constructor
asio::ip::tcp::socket socketTCP(io_service);
strcpy (sendBuffer, "*Constructor*\n");
endPt = asio::ip::tcp::endpoint(asio::ip::address::from_string("127.0.0.1"),\
boost::lexical_cast<int>(5004));
socketTCP.connect(endPt);
socketTCP.write_some (asio::buffer(sendBuffer));
};
void sendRPCData (void) { //send data when called from main()
strcpy (sendBuffer, "rpcData\n");
socketTCP.write_some (asio::buffer(sendBuffer));
};
};
int main(void) {
rpcClass rpc; // I WANT THE rpc OBJECT TO SEND DATA
rpc.sendRPCData (); // ONLY WHEN THIS METHOD IS CALLED
return (0);
}
Here is the error output (Eclipse)
rpc-class.cpp: In member function ‘void rpcClass::sendRPCData()’:
rpc-class.cpp: error: ‘socketTCP’ was not declared in this scope
Thanks a lot for helping!
-pn
You've declared socketTCP as a local variable in the constructor, not a member variable of the class.
That means you can't use it in other functions.
Presumably you meant to declare a member variable, and initialize that in the constructor instead of a local variable.

Qt: Connecting SIGNAL to SLOT in 2 different windows

I'm going nuts trying to find the problem here. I have a main window, and a form type window made in Qt. I'm using the Visual Studio 2010 addon. For some reason, my SLOT is never called in the main window; however, the signal appears to be emitted.
Here's what I've done:
This is the form:
class ScalerValuesWindow : public QWidget
{
Q_OBJECT
private:
Ui::ScalerValuesWindow ui;
// Variables
std::vector<int> scalerValues;
public slots:
void storeScalerValues();
signals:
void ScalerValues(std::vector<int>);
public:
ScalerValuesWindow(QWidget *parent = 0);
};
void ScalerValuesWindow::storeScalerValues()
{
emit ScalerValues(scalerValues);
hide();
}
Then here's my main window connection line in my constructor:
scalerValuesWindow = new ScalerValuesWindow;
connect(scalerValuesWindow, SIGNAL(ScalerValues(std::vector<int>)), this, SLOT(RetrieveScalerValues(std::vector<int>)));
This is in my main window's class declaration:
public slots:
void RetrieveScalerValues(vector<int> scalerValues);
And this is the slot:
void RelayduinoGuiThreading::RetrieveScalerValues(vector<int> scalerVals)
{
scalerValues = scalerVals;
}
I have Q_OBJECT declared in both. I have no idea what could be causing this.
Any advice would be greatly appreciated.
You must define your slot as:
public slots:
void RetrieveScalerValues(std::vector<int> scalerValues);
^^^
(Inspect the generated moc file to see exactly what signal/slot signatures are being generated.)
Don't use using namespace std; in your headers, that's bad practice anyway (pulls in that huge namespace into all the users of that header, which is impolite).

Resources