use QT with DLL compiled with -MT crash - crash

I compiled QT dynamic with -MT( modify config file )
my code is like this( my program is release mode with MT too ):
int main( int argc, char *argv[] )
{
int ret = 0;
if( 0 == ret )
{
QApplication a( argc, argv );
{
test01 w;
w.show();
ret = a.exec();
}
}
return ret;
}
test01 is very simple like this:
class test01 : public QMainWindow
{
Q_OBJECT
public:
test01(QWidget *parent = 0);
~test01();
private:
Ui::test01Class ui;
};
everything is ok, but when leave main function, it crashed:
[enter image description here][1]
Anyone will help me?

Related

Arduino - passing values by reference from lamda to singleton

Hello i am bigginer in programing and i have specific problem.
I have been learning a new ways to write a code in small Arduino project.
that project have multiple objects like distance measuring Senzor, led diods , temperature senzor, etc. And all this objects have its own menu where you can, for example, start a calibration or just get values.
What i need is singleton class that has a function enter_esc() that need a int (*funct)() parameter basically function pointer.
That enter_esc(int (*funct)()) function just looping function until you press escape pin which is defined.
function Calibration() have inside some private: object data types like value or cali_value.
so i tried to insert function Calibration() right into enter_esc(Calibration) but it won't compile becouse i didnt pass that vlaues by reference or copy.
but what i found is lambda.
i made a lamda similar to a Calibration() function and i passed values by reference &{//domething;}
but i had to use enter_esc(std::function<int()>& funct) whitch is only int C++ standard library and not in Arduino C/C++ so my qestion is:
[is there some way how to pass values by reference by using lambda to a singleton class in Arduino ?]
(i konw it can be done differently but like i said i want to learn some new ways to program, also if you have some different way to make it i will by very happy to see it)
10Q for your time :)
//Class.h
#pragma once
class events {
private:
static events e_instance;
int p_menu, p_enter, p_esc, p_up, p_down;
int menuValue;
events();
public:
events(const events&) = delete;
static events& Get();
int ArrowUpDown(int maxVal);
int ArrowUpDown(int p_up, int p_down, int maxVal);
int enter_esc(const std::function<int()>& funct);
};
events events::e_instance;
class deviceBase : public Printables
{
public:
const char* a_pin;
int d_pin;
String type;
String deviceName;
bool inUse;
int actualCount;
public:
String getType() override;
int getActualCount() override;
String getName() override;
String getInUse() override;
};
class senzor : public deviceBase
{
private:
int Value;
int triggValue;
public:
int p_triggValue = 10;
static int allSenzors;
friend events;
senzor();
~senzor();
public:
int getValue();
int Calibration();
void changeTriggVal(int x);
void Reset();
void nullCalibration();
void Menu(int x);
void setName(String deviceName);
void setInUse(bool x);
int getPin();
};
int senzor::allSenzors = 0;
if you have some good advice to my code writing i will be also very glad
//Class.cpp
#include <iostream>
#include <string>
#include <functional>
#define LOG(x) std::cout << x << std::endl;
#define PINMENU 12
#define PINENTER 8
#define PINESC 9
#define PINUP 11
#define PINDOWN 13
using String = std::string;
struct Printables
{
virtual String getType() = 0;
virtual int getActualCount() = 0; ;
virtual String getName() = 0;
virtual String getInUse() = 0;
};
#include "Class.h"
events& events::Get() {
return e_instance;
}
int events::ArrowUpDown(int maxVal) {
if (maxVal) {
menuValue = menuValue < maxVal ? menuValue++ : menuValue;
}
if (maxVal) {
menuValue = menuValue > 0 ? menuValue-- : menuValue;
}
return menuValue;
}
int events::enter_esc(const std::function<int()>&funct) {
if (1) {
while (!p_esc) {
auto f = funct;
}
}
return 1;
}
int events::ArrowUpDown(int p_up, int p_down, int maxVal) { return 666; }
events::events() {};
String deviceBase::getType() { return type; }
int deviceBase::getActualCount() { return actualCount; }
String deviceBase::getName() { return deviceName; }
String deviceBase::getInUse() {
String Status;
Status = inUse == 1 ? "Active" : "Deactive";
return Status;
}
senzor::senzor() : Value(0), triggValue(1) {
a_pin = "xx";
type = "[SENZOR]";
deviceName = "[UNKNOWN]";
inUse = 0;
allSenzors++;
actualCount = allSenzors;
a_pin = 0;
}
senzor::~senzor() {
allSenzors = 0;
}
int senzor::getValue() {
Value = 4;
return Value;
}
int senzor::Calibration() {
triggValue = triggValue < getValue() ? getValue() : triggValue;
p_triggValue = triggValue;
return p_triggValue;
}
void senzor::changeTriggVal(int x) {
p_triggValue = x;
}
void senzor::Reset() {
p_triggValue = triggValue;
}
void senzor::nullCalibration() {
triggValue = 1;
}
void senzor::setName(String deviceName) {
this->deviceName = deviceName;
}
void senzor::setInUse(bool x) {
inUse = x;
}
int senzor::getPin() {
return 4;
}
int printsss() {
return 1;
}
////////////////////////////////this what i was writing about//////////////////////////////
void senzor::Menu(int x) {
events::Get().enter_esc([&]() { triggValue = triggValue < getValue() ? getValue() : triggValue;
p_triggValue = triggValue;
return p_triggValue; });
}
but if i use lambda in arduino with enter_esc(int (*funct)()) i get this kind of error
no matching function for call to 'events::enter_esc(senzor::Menu(int)::<lambda()>)'

C++: Crash when compiling with gcc 9.1, 9.2 with any level of optimization

The following code crashes if compiled with gcc.9.1 and 9.2. If compiled with older gcc everything is ok.
#include <cstdlib>
struct A
{
A()
{
set( nullptr );
}
std::ptrdiff_t offset_;
void set( void* ptr )
{
offset_ = reinterpret_cast<char*>( ptr ) - reinterpret_cast<char*>( this );
}
void* get()
{
return reinterpret_cast<char*>( this ) + offset_;
}
};
int main()
{
A a;
int *b = static_cast<int*>( a.get() );
if( !b )
a.set( b = new int{ 10 } );
return *b;
}
Link to the code on the coliru: http://coliru.stacked-crooked.com/a/3f5b4623a98fd0b5
Looking to the assembler I see that gcc eliminates the if statement. Link to the godbolt: https://godbolt.org/z/HrX3Uc
So the question is:
Is it a bug in the gcc which needs to be reported or my program contains some UB ?
struct A
{
A()
{
set( this );
}
std::ptrdiff_t offset_;
void set( void* ptr )
{
offset_ = reinterpret_cast<char*>( this ) - reinterpret_cast<char*>( ptr );
}
bool hasValue()
{
return offset_ != 0;
}
void* get()
{
return reinterpret_cast<char*>( this ) - offset_;
}
};
int main()
{
A a;
int *b = static_cast<int*>( a.get() );
// nullptr != 0
if(!a.hasValue())
a.set( b = new int{ 10 } );
return *b;
}

How to send signal to a child process created via fork and execl?

Assume I have a program foo that simply prints forever but is stopped before then,
int main(int argc, char * argv[])
{
kill(getpid(), SIGSTOP);
while(1) {
printf("foo\n");
}
return 0;
}
And then I have a program bar that forks and runs foo:
int main(int argc, char * argv[])
{
pid_t pid = fork();
if (pid == 0)
{
execl("foo", "foo", NULL);
}
return 0;
}
Now, I want bar to be able to send SIGCONT to foo so it can go on its printing duties, and then later send SIGSTOP again, and even later send SIGCONT again, and so on.
I couldn't figure out how to do that. Any help?
EDIT: I figured out my problem. Apparently, the foo program isn't ready to accept signals immediately when it runs. When I did
sleep(5);
int rc = kill(pid, SIGCONT);
it worked.
pid_t pid = fork();
if (pid == 0)
{
execl("foo", "foo", NULL);
}
int rc = kill(pid, 18);
return 0;
Suggest: don't forget to handle errors codes from system calls! Work without handling of error codes is worse style! This two actions you can do in one program. In this case it's will be more effective:
#include <errno.h>
int main(int argc, char * argv[])
{
int rc = 0;
pid_t pid = fork();
if (pid == 0) /* child */
{
kill(getpid(), SIGSTOP);
if (rc == -1) {
perror("Something wrong while kill() in child");
}
while (1) {
printf("foo\n");
}
} else if (pid > 0) { /* father */
rc = kill(pid, SIGCONT);
if (rc == -1) {
perror("Something wrong while kill() in parent");
}
} else if (pid < 0) {
perror("Something wrong while fork()");
return -1;
}
return 0;
}

How do I setup Using Systems::IO::Ports in DLL so that it can be called from a loadlibrary function

Using vc2012 express c++
I am a little confused on how a runtime library works, but I had needed to create one for a driver from some hardware I have so that it can be used in a SDK.
My source code is as follows
#include "PhantomAdapter.h"
#include <stdexcept>
int ready()
{
//return Comms::SerialPort::check();
return 1;
}
int open()
{
int flag=0;
//flag=Comms::SerialPort::openPort();
return flag;
}
int close()
{
Comms::SerialPort::closePort();
return 1;
}
int angle(double& angle)
{
angle = Comms::SerialPort::read();
return 0;
}
int torque(double torque)
{
Comms::SerialPort::send((Byte)torque);
return 1;
}
namespace Comms
{
//static p1 = gcnew System::IO::Ports::SerialPort();
int SerialPort::openPort()
{
bool check=0;
p1 = gcnew System::IO::Ports::SerialPort();
p1->BaudRate = 57600;
p1->PortName = "COM3";
if(p1->IsOpen)
return 0;
else {
p1->Open();
return 1;
}
}
int SerialPort::check()
{
array<String^>^ serialPorts = nullptr;
bool flag = true;
serialPorts = p1->GetPortNames();
for each(String^ port in serialPorts)
{
if(port=="COM3")
flag= true;
}
return flag;
}
void SerialPort::closePort()
{
p1->Close();
}
void SerialPort::send(Byte data)
{
array<unsigned char>^ buffer = gcnew array<Byte>(1);
buffer[0] = (char)data;
p1->Write(buffer,0,1);
}
double SerialPort::read()
{
double data;
data = p1->ReadByte();
return data;
}
}
header
#define PHANTOMADAPTER_API __declspec(dllexport)
#else
#define PHANTOMADAPTER_API __declspec(dllexport)
#endif
#using <mscorlib.dll>
#using <system.dll>
using namespace System;
using namespace System::IO::Ports;
using namespace System::Threading;
extern "C" {
PHANTOMADAPTER_API int ready();
PHANTOMADAPTER_API int open();
PHANTOMADAPTER_API int close();
PHANTOMADAPTER_API int angle(double& angle);
PHANTOMADAPTER_API int torque(double torque);
}
namespace Comms
{
public ref class SerialPort
{
private:
static System::IO::Ports::SerialPort^ p1;
public:
static int openPort();
static void closePort();
static double read();
static void send(Byte data);
static int check();
};
}
I am getting the following error when I call the angle DLL function or any function that requires the Comms namespace.
System.NullReferenceException: Object reference not set to an instance of an object.
at System.IO.Ports.SerialPort.get_IsOpen()
at System.IO.Ports.SerialPort.ReadByte()
at angle(Double* angle)
can someone please point me in the right direction, I feel as if the serialPort class can't be open from runtime library unless I import it somehow

Changing QIcon size in QStandardItemModel

I am trying to make QTableView/QStandardItemModel with arbitrary sized qIcons. In the MWE below, I have successfully changed the height of a row using a delegate. I can't figure out how to make it use a larger icon size in the larger row. Any help appreciated. Note that all rows can be the same height as long as I can set that height. For example, how do I make the icons 50x50 in the example below.
#include <QtGui>
#include "test.h"
//////////////////////////////////////////////////////////////////////////
// TEST.H file:
//#include <QtGui>
//
//class MainWindow : public QMainWindow {
// Q_OBJECT
//
// public:
// MainWindow(QWidget *parent = 0);
//};
class ItemDelegate : public QItemDelegate {
public:
ItemDelegate() {}
QSize sizeHint ( const QStyleOptionViewItem &, const QModelIndex & ) const {
return QSize(50,50);
}
};
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
QImage iconImage(100, 100, QImage::Format_RGB32);
iconImage.fill(0xF08080);
const int ARRAY_HEIGHT = 2;
const int ARRAY_WIDTH = 2;
QStandardItemModel *model = new QStandardItemModel( ARRAY_HEIGHT, ARRAY_WIDTH );
model->setHeaderData( 1, Qt::Horizontal, QString("Icon") );
model->setHeaderData( 2, Qt::Horizontal, QString("Text") );
for (int yy=0; yy < ARRAY_HEIGHT; yy++ ) {
QStandardItem *newIconItem = new QStandardItem;
newIconItem->setIcon( QPixmap::fromImage( iconImage ) );
model->setItem( yy, 0, newIconItem );
QStandardItem *newTypeItem = new QStandardItem( QString("Foo") );
model->setItem( yy, 1, newTypeItem );
}
QTableView *table = new QTableView;
ItemDelegate *delegate = new ItemDelegate();
table->setModel(model);
table->setItemDelegate(delegate);
table->setSelectionMode(QAbstractItemView::SingleSelection);
table->setSelectionBehavior(QAbstractItemView::SelectRows);
table->verticalHeader()->setVisible(false);
table->verticalScrollBar()->setVisible(false);
table->resizeRowsToContents();
table->resizeColumnsToContents();
QHeaderView *headerView = table->horizontalHeader();
headerView->setStretchLastSection(true);
setCentralWidget( table );
}
int main( int argc, char *argv[] ) {
QApplication app(argc, argv);
MainWindow win;
win.show();
return app.exec();
}
try
ListView->setIconSize(QSize(100,100));