Exception handling doesn't work with Qt on Windows - c++

I'm facing strange problem. Namely, Qt somehow turns off exception handling in my program. I can't catch any exception, and when I throw an exception application crashes.
I'm using Qt 4.7.0 (32 bit) from Qt SDK v2010.05 on Windows 7 (64 bit), g++ (GCC) 4.5.1 from MinGW, NetBeans 6.9.1.
But I also cheked this with g++ 3.4.5 (also from MinGW) and Qt Creator 2.0.1 - same strange behavior.
For example (simplest case):
#include <Qt/QApplication.h>
#include <iostream>
#include <stdexcept>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
try {
cout << "Before exception" << endl;
throw runtime_error("Exception occured");
cout << "After exception" << endl;
} catch (runtime_error& exc) {
cout << exc.what() << endl;
exit(1);
}
return 0;
}
When I execute above program I've got this output:
Before exception
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
I've tried to add flag "-fexceptions" to g++ but it hasn't changed anything.
When I don't use Qt, everything is OK:
#include <Qt/QApplication.h> // It is not caused only by including Qt header
// so it doesn't matter if I comment this out or not
#include <iostream>
#include <stdexcept>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[]) {
// QApplication app(argc, argv);
try {
cout << "Before exception" << endl;
throw runtime_error("Exception occured");
cout << "After exception" << endl;
} catch (runtime_error& exc) {
cout << exc.what() << endl;
exit(1);
}
return 0;
}
The output:
Before exception
Exception occured
Does anybody know why is this happen that way and how to fix this? Has it something to do with type of exception handling method (SJLJ or Dwarf-2) used when Qt was build?

I've reconfigured and recompiled Qt with flag -exceptions:
D:\Qt\2010.05\qt>mingw32-make confclean && configure -exceptions && mingw32-make
and now everything is ok!
Thanks all for help, especially to Nick D!
Anyway, it's very strange that I had Qt build without this flag. I had downloaded Qt SDK in binary form from official site.

It's no longer necessary to use the -exceptions flag with Qt. In Qt Creator 4 it's the default, and my Windows Qt app happily uses vast and extensive exception handling with no problems. Qt MSVC builds use the /EHsc compiler option, which turns normal exception handling on.

Related

OPENCV: Fail to use dnn module in c++

I'm using dnn module in opencv.
Previously, it work well in python.
But when I turn to C++ version.readNetFromDarknet function report error that I don't know how to fix. Error is below:
Unhandled exception at 0x00007FFF37BE4F69 in untitled.exe: Microsoft C++ exception: std::out_of_range at memory location 0x000000BF2A53F090
My full code is :
#include <QCoreApplication>
#include <opencv2/opencv.hpp>
#include <string>
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
string classesFile, modelConfig, modelWeights;
classesFile = "D:/work/rs/model/guoxing_mac9_3535_20220224.names";
modelConfig = "D:/work/rs/model/guoxing_mac9_3535_20220224.cfg";
modelWeights = "D:/work/rs/model/guoxing_mac9_3535_20220224_last.weights";
dnn::Net m_model;
vector<string> m_classes;
ifstream ifs(classesFile.c_str());
string line;
while (getline(ifs, line))
m_classes.push_back(line);
try
{
cout << "trying" << endl;
m_model = dnn::readNetFromDarknet(modelConfig, modelWeights);
}
catch (Exception& e)
{
cout << e.msg << endl;
}
m_model.setPreferableBackend(dnn::DNN_BACKEND_OPENCV);
m_model.setPreferableTarget(dnn::DNN_TARGET_OPENCL);
return a.exec();
}
Test in Opencv3.4.2 and VS2017.
Thanks for all reply!
After update Opencv from 3.4.2 to 3.4.16, it solved. low version dnn module is not work well for some network architecture.

Why is CLion not showing exceptions?

CLion appears to be not showing me any exceptions when running my code. To test this, I've created a new project with only the following code:
#include <iostream>
int main() {
std::cout << "--- One" << std::endl;
throw 6;
std::cout << "--- Two" << std::endl;
return 0;
}
Which leads to the following output:
C:\Users\david\CLionProjects\untitled\cmake-build-debug\untitled.exe
--- One
Process finished with exit code 0
As you can see, code before the exception is executed and code following it is not executed (as you would expect). But instead of a message about the exception, it says "Process finished with exit code 0", as if no exception had occurred.
The same code compiled and executed on Ubuntu (via terminal) displayed an error message. So I assume the problem is with CLion.
How can I resolve this problem so that I can see messages for exceptions in my code?
Is there any setting that could lead to such behaviour?
I'm using CLion on Windows 10 with Cygwin. Here's a screenshot of the problem:
Throw requires also try and catch
From:
http://www.cplusplus.com/doc/tutorial/exceptions/
// exceptions
#include <iostream>
using namespace std;
int main () {
try
{
throw 20;
}
catch (int e)
{
cout << "An exception occurred. Exception Nr. " << e << '\n';
}
return 0;
}
that compiled and run under cygwin shows:
$ g++ prova1.cc -o prova1
$ ./prova1
An exception occurred. Exception Nr. 2

C++ promise.set_value fails with unknown error under linux

I'm trying to get my simulation running on our high performance server. It (unfortunately) uses CentOS Linux release 7.7.1908 (Core) instead of Win10, under which I'm developing the program. With this came a large amount of errors, one of them I was not able to fix on my on:
#include <future>
#include <iostream>
int main(int argument_count, char** arguments) {
int i = 1234;
std::cout << "Initialized i" << std::endl;
std::promise<int> promise;
std::cout << "Constructed promise" << std::endl;
promise.set_value(std::move(i));
std::cout << "Set value" << std::endl;
std::future<int> future = std::move(promise.get_future());
std::cout << "Retrieved future" << std::endl;
int j = std::move(future.get());
std::cout << "Got value: " << j << std::endl;
return 0;
}
When compiling this under Win10 with "cl test.cpp", the output looks like I'd expect:
Desktop>test.exe
Initialized i
Constructed promise
Set value
Retrieved future
Got value: 1234
On the other hand, when compiling on the server with "g++ -std=c++11 test.cpp", the output is different:
~/test_dir$ ./a.out
Initialized i
Constructed promise
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
Aborted
I do get the same error when trying this with an Ubuntu 16.04.6 LTS machine. I don't see why this happens.
Obviously, something is fishy in this line: promise.set_value(std::move(i)) since the output before is printed and the line after that statement is not executed any more. Furthermore, the compiler/ linker does find a one of the two versions "void set_value (const T& val);" or "void set_value (T&& val);" that would be appropriate for the template specification "int", i strongly suspect the later.
But why is the program aborting when setting an integer as the value of a promise? Even when inlining the value and skipping the variable all together does produce the error.
Can someone point me to where the error is?
Try compiling using the pthread flag:
g++ -std=c++11 test.cpp -pthread
Linking against both pthread and stdc++ may resolve the issue.
Example adding stdc++ in Cmake:
target_link_libraries(
# Your library or binary here
stdc++
pthread
)
Tested with the following code:
#include <iostream>
#include <future>
int main(int argc, char *argv[]) {
static_cast<void>(argc);
static_cast<void>(argv);
std::promise<int> pr;
auto fut = pr.get_future();
pr.set_value(10);
std::cout << fut.get() << std::endl;
return 0;
}

Clion: Gtest TEST_F not working with std::cin

I'm trying to send in an input value within the CLion + Gtest system (MacOS 10.14.4), but I get infinite "loading icon" and cannot type in anything into the "Run" window.
I created the tests using a tutorial. Normal tests work flawlessly, only std::cin refuses to work. std::cin works outside the test framework, when just opening a basic project and immediately using it. For this simple example, I only use one file "test.cpp", so nothing else is imported.
#include <gtest/gtest.h>
#include <iostream>
using testing::Eq;
using namespace std;
namespace {
class basicTest : public testing::Test {
public:
basicTest() {
}
};
TEST_F(basicTest, test1) {
cout << "\nWrite a number here: " << endl;
int i;
cin >> i;
cout << "You wrote " << i << endl;
}
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Running this produces the following:
The icon keeps spinning, but there is nowhere for me to write an input. In my original code, the first cout did not even output, so it's just a blank screen until I terminate it with the stop button.
Stopping early produces "Process finished with exit code 137 (interrupted by signal 9: SIGKILL)" but I don't think it's relevant.
I have not tried to use debug mode because it's not compatible with my MacOS version (that's another problem I'll fix eventually).

Compiling simple C++ SFML file with network code using MinGW throws error "undefined reference to IpAddress"

I keep getting errors when compiling a C++ project using SFML 2 on Windows using MinGW.
At C:\MinGW\include\ there is the SFML header files folder including Network.hpp and I also copied the libsfml-[...].a files into C:\MinGW\lib\ as well as the sfml-[...]-2.dll files into C:\MinGW\bin\.
This is my sample file:
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
int main(int argc, char** argv)
{
sf::TcpListener listener;
listener.listen(4444);
sf::TcpSocket client;
std::cout << "Waiting for client to connect..." << std::endl;
if (listener.accept(client) == sf::Socket::Done)
{
std::cout << "Client connected: " << client.getRemoteAddress() << std::endl;
listener.close();
}
else
{
std::cout << "Client didnt connect" << std::endl;
}
return 0;
}
If I try to compile it using the following command
g++ test.cpp -lsfml-system -lsfml-network
I get these errors:
C:\Users\me\AppData\Local\Temp\cc6XzH1R.o:test.cpp:(.text+0x28): undefined reference to `_imp___ZN2sf9IpAddress3AnyE`
C:\Users\me\AppData\Local\Temp\cc6XzH1R.o:test.cpp:(.text+0x3a): undefined reference to `_imp___ZN2sf11TcpListener6listenEtRKNS_9IpAddressE`
Thanks for any help :D
Matze
Simple , try to link correct version of library.
If you are making x86 app use the 32bit version of library and if you are making x64 app use 64bit version. I also got the same error and this fixed.