QSerialPortInfo debug crash visual studio 2015 + QT5.8 - c++

I am using QT5.8 and visual studio 2015 on win10 to control a serial port.
The following minimal code crashes in visual studio (but only in debug, release works fine) after the visualization of the messages, so when the object QList infos is destroyed, can anybody explain why?
#include <iostream>
#include <QtSerialPort/QSerialPortInfo>
void showPorts() {
QList<QSerialPortInfo> infos = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo &info : infos) {
std::cout << " detected port : " << info.portName().toStdString() << std::endl;
}
} // this code CRASHES HERE only in debug !
int main (int argc, char** argv){
std::cout<<"\n >>> test serial info <<< \n\n"<<std::endl;
showPorts();
std::cout << "\n >>> Finished, press enter to exit <<< \n\n" << std::endl;
std::cin.get();
return 0;
}

I have tested your code and I have not a crash.
PS: Windows 10x64, MSVC2015, Qt 5.8.0 32 bit

Related

GCC compiler does not output anything in Visual Studio Code or Atom

I try to compile this code
#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
}
But it outputs
nothing in the terminal
Atom was just broken for me, had to restart my PC and Visual Studio Code started working.

After compiling C++. a.exe in visual studio is not showing output but when I am running simple program it gives output

enter image description here
1.After executing a.exe is not showing output in visual studio terminal and also in powershell admin mode.It is also not giving any error.
but when running hello world program it gives output.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> g1;
for (int i = 1; i <= 5; i++)
g1.push_back(i);
cout << "Output of begin and end: ";
for (auto i = g1.begin(); i != g1.end(); ++i)
cout << *i << " ";
return 0;
}
Your code is showing but it just exits out so fast you can't tell what it returned. Advise checking this out: How to stop C++ console application from exiting immediately?
Have you set the program to the console program? Property -> Linker -> System -> Subsystem -> "Console(/SUBSYSTEM:CONSOLE)"

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.

Command processor has stopped working

I was writing a code for exception handling on Visual C++ 2010 .Here is the code
#include <iostream>
using namespace std;
// Localize a try/catch to a function.
void Xhandler(int test)
{
try{
if(test) throw test;
}
catch(int i) {
cout << "Caught Exception #: " << i << '\n';
}
}
int main()
{
cout << "Start\n";
Xhandler(1);
Xhandler(2);
Xhandler(0);
Xhandler(3);
cout << "End";
return 0;
}
The Program executed properly and the output was the as expected.But when I pressed the close button for closing the console then an error came that cmd has stopped working
.Then I ran my previous code that executed properly ,they also gave the same error
.
Can anybody tell why it is happening?Is it a problem with the Visual c++ 2010 or the code
I think your problem is not with your code. The problem is within your compiler tool chain. You probably are using Qt, and the tool chain has a problem causing this. Google the message you get when you crash with your IDE.
Here's a simple experiment to prove what I'm saying: just run this code:
int main()
{
cout << "Start\n";
cout << "End";
return 0;
}
And your program will crash, which means you have no problems with exceptions or anything else in your code, but with your tool chain.

Exception handling doesn't work with Qt on Windows

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.