Pieces of the program, I will give a part, because it is very large and secret)

How to fix crash on the 6th line of code?

Hierarchy of connected header files: DataXY is connected to the IOController, and it to the MainWindow, and it to the Main

Kusman method from IOController:

IOController.cpp:

{//какой-то метод... this->writeToBufferWin(answer);//В этой строке стало вылетать из-за кода 3 строками ниже srand(time(0)); this->openUrl(this->linkJob); this->randomSleep(1,2); DataXY *dataScreen = new DataXY(this->searchButtonRunTask());//Вылет тут //Если ее закоменнтить, вылетать не будет, но эта строка крайне важна cout << "X: " << dataScreen->leftLowerBound.x <<endl; } DataXY IOController::searchButtonRunTask() {//Создаю что статически, что динамически - вылетает QPixmap screenButton; DataXY *dataScreenXY = new DataXY(screenButton); dataScreenXY->searchButtonRunTask(); return *dataScreenXY; //ТУТ ВЫЛЕТАЕТ ПОЧЕМУ-ТО } bool IOController::writeToBufferWin(string str) { // QClipboard *clipB = QGuiApplication::clipboard(); // clipB->clear(); // clipB->setText(QString::fromStdString(str)); //Если это расскоментировать и закомментировать то что ниже до ретерна, станет вылетать строка clipB->setText(QString::fromStdString(str)); OpenClipboard(nullptr); EmptyClipboard(); HGLOBAL hgBuffer; char* chBuffer; hgBuffer = GlobalAlloc(GMEM_MOVEABLE,(sizeof(str) * str.size()) + 1); chBuffer= (char*)GlobalLock(hgBuffer); strcpy(chBuffer, LPCSTR(str.c_str())); GlobalUnlock(hgBuffer); SetClipboardData(CF_TEXT, hgBuffer); CloseClipboard(); return true; } 

DataXY constructor that works in the code:

 DataXY::DataXY(const QPixmap &pix) { this->finishedSearchButtonStartTask = false; this->pixmap = pix; this->createScreen(); this->screenDataHeight = this->pixmap.height(); this->screenDataWidth = this->pixmap.width(); } 

Copy constructor, DataXY constructor and destructor

 void DataXY::createScreen() { QScreen *screen = QGuiApplication::primaryScreen(); if(screen) { this->pixmap = screen->grabWindow(0); } else { cerr << "Error createing Screenshot" << endl; } delete screen; } DataXY::DataXY(QObject *parent) : QObject(parent) { this->createScreen(); this->screenDataHeight=this->screenDataWidth=0; this->finishedSearchButtonStartTask = false; } DataXY::~DataXY() { } DataXY::DataXY(const QPixmap &pix) { this->finishedSearchButtonStartTask = false; this->pixmap = pix; this->createScreen(); this->screenDataHeight = this->pixmap.height(); this->screenDataWidth = this->pixmap.width(); // this->img = this->pixmap.toImage(); } DataXY::DataXY(const DataXY &dat) { this->leftLowerBound = dat.leftLowerBound; this->leftUpperBound = dat.leftUpperBound; this->rightLowerBound = dat.rightLowerBound; this->rightUpperBound = dat.rightUpperBound; this->leftLowerMargin = dat.leftLowerMargin; this->leftUpperMargin = dat.leftUpperMargin; this->rightLowerMargin = dat.leftLowerMargin; this->rightUpperMargin = dat.leftUpperMargin; this->screenDataHeight = dat.getScreenDataHeight(); this->screenDataWidth = dat.getScreenDataWidth(); this->pixmap = dat.getPixmap(); // this->img = this->pixmap.toImage(); this->finishedSearchButtonStartTask = dat.finishedSearchButtonStartTask; this->createScreen(); } 
  • Or write in the comments which way to dig? - Pospelov Daniel
  • look at the stack, this code is hard to figure out - Bearded Beaver
  • It is not known what lies in the DataXY class. Is the assignment operator / copy constructor written there? - vegorov
  • And the secret because the program to make money on the Internet like a bot? - vegorov
  • Yes, a bot for earning on the site of one - Daniel Pospelov

1 answer 1

The first bug: you allocate memory, and do not delete. This leads to a lack of memory and a system crash.

 {//какой-то метод... DataXY *dataScreen = new DataXY(this->searchButtonRunTask());//Вылет тут //Если ее закоменнтить, вылетать не будет, но эта строка крайне важна cout << "X: " << dataScreen->leftLowerBound.x <<endl; } DataXY IOController::searchButtonRunTask() {//Создаю что статически, что динамически - вылетает QPixmap screenButton; DataXY *dataScreenXY = new DataXY(screenButton); dataScreenXY->searchButtonRunTask(); return *dataScreenXY; //ТУТ ВЫЛЕТАЕТ ПОЧЕМУ-ТО } 

This is corrected if the object is stored on the stack:

 {//какой-то метод... DataXY dataScreen(this->searchButtonRunTask());//Вылет тут //Если ее закоменнтить, вылетать не будет, но эта строка крайне важна cout << "X: " << dataScreen.leftLowerBound.x <<endl; } DataXY IOController::searchButtonRunTask() { QPixmap screenButton; DataXY dataScreenXY(screenButton); dataScreenXY.searchButtonRunTask(); return dataScreenXY; } 

As for why everything crashes, you did not provide the destructor code of the DataXY class, this will greatly help to find the cause of the crash. It is advisable to show all the code, otherwise no one can help you.

  • Please give me your @AlexGlebe, I'll throw off the project, I can pay you a symbolic 50p for correcting the departure) Well or how much we agree - Daniel Pospelov
  • My Qt library is incompatible with your code. We'll have to look manually. Let's look for ... - AlexGlebe
  • In void DataXY::createScreen() you delete the screen . Feels heart for nothing. - AlexGlebe
  • But surely! It was impossible to delete it, though I did not understand why ... After all, it was created temporarily to get a QPixmap - Daniel Pospelov