Consider the following code(The system specification is mentioned down below):
#include <stdexcept>
#include <thread>
void f()
{
throw std::runtime_error("Something");
}
int main()
{
std::thread thr(f);
thr.join();
}
I easily could run it with a debugger and have the stack trace from where the exception threw. Like this:
But the same code fails if I use a QThread instead of std::thread:
#include <QCoreApplication>
#include <QThread>
void f()
{
throw std::runtime_error("Something");
}
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
auto const thr = QThread::create(f);
thr->start();
return QCoreApplication::exec();
}
Like this:
It's possible to debug it like std::thread? and have the stack frames from where the exception threw?
Environment:
OS: Fedora 35
Kernel: Linux 5.15.10
Compiler: GCC 11.2.1
Qt: 5.15.2
QtCreator: 6.0.1
Related
I am using Windows 8.1, visual studio community 2013.
I downloaded boost 1.59.
I then open Developer Command Prompt for VS2013, run bootstrap.bat, then run b2.exe.
All .lib files are placed under ./stage/lib/.
I set the c++ include path, and linker path. I built my program successfully and run under debug mode.
Here is the error message I get:
Unhandled exception at 0x77394598 in BoostStation.exe: Microsoft C++ exception: boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> > at memory location 0x001BFD74.
Here is the break point:
throw enable_current_exception(enable_error_info(e)); // from throw_exception.hpp
Anyone knows how to solve the problem?
Another question, Are there any .dll files generated by this build and Where can I find them?
Here is my program:
MulticastSender.h
#include <boost/asio.hpp>
#include <boost/scoped_ptr.hpp>
#include <string>
class MulticastSender
{
public:
MulticastSender(const boost::asio::ip::address& multicast_addr, const unsigned short multicast_port)
: ep_(multicast_addr, multicast_port)
{
socket_.reset(new boost::asio::ip::udp::socket(svc_, ep_.protocol()));
}
~MulticastSender()
{
socket_.reset(NULL);
}
public:
void send_data(const std::string& msg)
{
socket_->send_to(boost::asio::buffer(msg), ep_);
}
private:
boost::asio::ip::udp::endpoint ep_;
boost::scoped_ptr<boost::asio::ip::udp::socket> socket_;
boost::asio::io_service svc_;
};
main.cpp
#include "stdafx.h"
#include "MulticastSender.h"
int _tmain(int argc, _TCHAR* argv[])
{
boost::asio::ip::address multiCastGroup;
multiCastGroup.from_string("192.168.32.1");
MulticastSender outDoor(multiCastGroup, 6000);
while (true)
{
outDoor.send_data("Hello");
Sleep(1000);
}
return 0;
}
Your boost installation is ok, because obviously you're able to compile and link a program that throws a boost::exception.
Catch the exception by wrapping your code in a try/catch block, then print out the message. I changed your main-function accordingly:
#include "stdafx.h"
#include "MulticastSender.h"
#include "boost/exception.hpp"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
try
{
boost::asio::ip::address multiCastGroup;
multiCastGroup.from_string("192.168.32.1");
MulticastSender outDoor(multiCastGroup, 6000);
while (true)
{
outDoor.send_data("Hello");
Sleep(1000);
}
}
catch (const std::exception& e)
{
std::cout << boost::diagnostic_information(e) << std::endl;
}
return 0;
}
This will catch the exception that is thrown by boost and print its message before the program exits.
You should also read up on exceptions in general: http://www.cplusplus.com/doc/tutorial/exceptions/
I am starting to learn threads in the C++11 standard in Qt.I can't include library ,no such of directory For example, I have the following simple code:
#include <QCoreApplication>
#include <thread>
#include <iostream>
using namespace std;
void func();
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyThread th1;
th1.run();
return a.exec();
}
void func()
{
cout << "This string from thread!"<<endl;
}
On the second string of the code, I have an error. The compiler doesn't "see" , I know that i must "include" 11 standard, so i go to .pro and paste CONFIG += c++11, but it isn't helping me :C
Please, I need your help!
You try to use QThread subclass, but you said that you want to use C++11 thread so use this:
#include <thread>
#include <QDebug>
#include <QApplication>
#include <iostream>
void foo()
{
std::cout << "This string from thread!"<<endl;
//while(true)
//{
// qDebug() <<"works";
// Sleep(500);
//}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
std::thread t(foo);
t.join();
return a.exec();
}
Also next is wrong:
MyThread th1;
th1.run();
Should be:
MyThread th1;
th1.start();
Details about threading with Qt classes:
https://www.qt.io/blog/2010/06/17/youre-doing-it-wrong
http://qt-project.org/doc/qt-5/thread-basics.html
http://qt-project.org/doc/qt-5/qtconcurrent-index.html
I'm using Gtkmm and multithreading.
I have a class "NetworkWorker" doig stuffs with the network in a secondary thread.
In this class i want to make many signals which will be handled by my class "MainWindow".
The methods which handle these signals, will edit append text in a TextView.
I have the following code:
NetworkWorker.h
#ifndef NETWORKWORKER_H_
# define NETWORKWORKER_H_
# include <sigc++/sigc++.h>
# include <glibmm/threads.h>
# include <string>
class NetworkWorker
{
public:
NetworkWorker();
~NetworkWorker();
void start();
void stop();
sigc::signal<void, std::string&>& signal_data_received();
private:
void run();
sigc::signal<void, std::string&> m_signal_data_received;
Glib::Threads::Thread* m_thread;
Glib::Threads::Mutex m_mutex;
bool m_stop;
};
#endif
NetworkWorker.c
#include <cstdlib>
#include <glibmm/timer.h>
#include <glibmm/threads.h>
#include <iostream>
#include <sigc++/sigc++.h>
#include "NetworkWorker.h"
NetworkWorker::NetworkWorker() :
m_thread(NULL), m_stop(false)
{
}
NetworkWorker::~NetworkWorker()
{
stop();
}
void NetworkWorker::start()
{
if (!m_thread)
m_thread = Glib::Threads::Thread::create(sigc::mem_fun(*this, &NetworkWorker::run));
}
void NetworkWorker::stop()
{
{
Glib::Threads::Mutex::Lock lock(m_mutex);
m_stop = true;
}
if (m_thread)
m_thread->join();
}
sigc::signal<void, std::string&>& NetworkWorker::signal_data_received()
{
return m_signal_data_received;
}
void NetworkWorker::run()
{
while (true)
{
{
Glib::Threads::Mutex::Lock lock(m_mutex);
if (m_stop)
break;
}
Glib::usleep(5000);
std::cout << "Thread" << std::endl;
std::string* str = new std::string("MyData");
m_signal_data_received.emit(*str);
}
}
MainWindow.h
#ifndef MAIN_WINDOW_H_
# define MAIN_WINDOW_H_
# include <gtkmm/textview.h>
# include <gtkmm/window.h>
# include <string>
class MainWindow : public Gtk::Window
{
public:
MainWindow();
~MainWindow();
void appendText(const std::string& str);
private:
Gtk::TextView m_text_view;
};
#endif
MainWindow.c
#include <gtkmm/notebook.h>
#include <gtkmm/widget.h>
#include <iostream>
#include <string>
#include "MainWindow.h"
MainWindow::MainWindow()
{
set_title("My App");
set_default_size(800, 600);
add(m_text_view);
}
MainWindow::~MainWindow()
{
}
void MainWindow::appendText(const std::string& str)
{
std::string final_text = str + "\n";
Glib::RefPtr<Gtk::TextBuffer> buffer = m_text_view.get_buffer();
Gtk::TextBuffer::iterator it = buffer->end();
buffer->insert(it, final_text);
Glib::RefPtr<Gtk::Adjustment> adj = m_text_view.get_vadjustment();
adj->set_value(adj->get_upper() - adj->get_page_size());
}
and my main.cpp
#include <cstdlib>
#include <gtkmm/main.h>
#include <iostream>
#include <string>
#include "MainWindow.h"
#include "NetworkWorker.h"
void recv(const std::string& str)
{
std::cout << str << std::endl;
}
int main(int argc, char **argv)
{
Gtk::Main app(Gtk::Main(argc, argv));
MainWindow main_window;
NetworkWorker network_worker;
main_window.show_all();
network_worker.signal_data_received().connect(sigc::ptr_fun(&recv));
network_worker.signal_data_received().connect(sigc::mem_fun(main_window, &MainWindow::appendText));
network_worker.start();
Gtk::Main::run(main_window);
return (EXIT_SUCCESS);
}
These snippetes have been a re-adapted for this question, so maybe some change is incoherent.
When I execute this code, I have the following output:
$> ./client
Thread
MyData
Thread
MyData
[...]
Thread
MyData
Thread
MyData
(client:5596): Gtk-CRITICAL **: gtk_text_layout_real_invalidate: assertion 'layout->wrap_loop_count == 0' failed
Thread
MyData
Thread
MyData
[...]
Thread
MyData
Thread
MyData
[1] 5596 segmentation fault (core dumped) ./client
Can some one help me to resolve this issue ? :)
The issue is you are calling non threadsafe function call (signal callbacks are not threadsafe).
So you need to use something like Glib::signal_idle().connect( sigc::mem_fun(*this, &IdleExample::on_idle) );(or whatever is equivalent to C API call g_idle_add(GCallback func)) from your thread. This function is threadsafe (at least the one from the C API).
See this tutorial for a simplified example.
Never call or signal from different threads when using UI libraries. Usually the APIs are designed to be called from a single thread. This is the single most often made mistake when using UI toolkits.
Given below is the code for embedding an application using QX11EmbedContainer.
#include "mainwindow.h"
#include <QApplication>
#include <QX11EmbedContainer>
#include <QProcess>
using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QX11EmbedContainer container;
container.show();
QProcess process(&container);
process.start("C:/Users/Administrator/Desktop.../.exe");
int status=a.exec();
process.close();
return status;
}
On running the application the error that I am getting is:
C:\Qt4.8.5\src\gui\kernel\qx11embed_x11.h:77: error: C2061: syntax error : identifier 'XEvent'
Where am I going wrong?
QX11EmbedContainer is X11-specific, it will not work under other systems (like Windows).
i'm trying to learn c++, but i can not find if it's possible to extend a class in this way:
main.cc
#include "mWindow.h"
using namespace std;
int main( int argc, char* argv[] ) {
gtk_init( &argc, &argv );
mWindow win = mWindow();
gtk_main();
return 0;
}
mWindow.cc
#include "mWindow.h"
mWindow::mWindow() {
gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (this, "my window");
gtk_widget_show_all (GTK_WIDGET(this));
}
mWindow.h
#ifndef MWINDOW_H_INCLUDED
#define MWINDOW_H_INCLUDED
#include <gtk/gtk.h>
using namespace std;
class mWindow : public GtkWindow {
public:
mWindow();
};
#endif
I suggest you take a look at gtkmm (http://www.gtkmm.org/) if you want to use GTK+ in conjunction with C++, i.e. there is no need to try to reinvent the wheel and write your own C++ interface for GTK+ (which is a C library).
thanks,
I was trying to use C libraries as if they were C++.
This is how I solved with gtkmm:
main.cc
#include <gtkmm/main.h>
#include "examplewindow.h"
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
ExampleWindow window;
Gtk::Main::run(window);
return 0;
}
examplewindow.h
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm-2.4/gtkmm.h>
class ExampleWindow : public Gtk::Window {
public:
ExampleWindow();
};
#endif //GTKMM_EXAMPLEWINDOW_H
examplewindow.cc
#include "examplewindow.h"
ExampleWindow::ExampleWindow() {
set_title("Gtk::TextView example");
set_border_width(5);
set_default_size(400, 200);
show_all_children();
}
also add the command to complete successfully, at least on Arch Linux:
g++ $(pkg-config --cflags --libs gtkmm-2.4) main.cc examplewindow.cc examplewindow.h -o executable
another small indication, what i shouldl use as dynamic arrays or vectors and for hashmap?