Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Qt app freeze when remove usb drive while qpainter is drwaing
Forum Updated to NodeBB v4.3 + New Features

Qt app freeze when remove usb drive while qpainter is drwaing

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
qpainter
11 Posts 3 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi and welcome to devnet,

    Are you properly deleting your painter and printer objects ? From the looks of it you are currently leaking them.

    These class are usually instanciated on the stack and not the heap.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    V 1 Reply Last reply
    0
    • SGaistS SGaist

      Hi and welcome to devnet,

      Are you properly deleting your painter and printer objects ? From the looks of it you are currently leaking them.

      These class are usually instanciated on the stack and not the heap.

      V Offline
      V Offline
      Vijay R. Gawade
      wrote on last edited by
      #3

      @SGaist How we can delete qprinter object? I have called painter->end() call usb stick is removed but after looking logs when we call painter->end() methods process freeze.
      PS O/P:
      root 1133 3.0 0.0 0 0 ? Zl 22:24 0:08 [MyApp] <defunct>

      Since we pass QPrinter as device to Qpainter and QPrinter output file path is set on usb device location which will be unavailable after removing usb stick, may be that's why paint->end() call failed becoz of resource unavailable

      jsulmJ 1 Reply Last reply
      0
      • V Vijay R. Gawade

        @SGaist How we can delete qprinter object? I have called painter->end() call usb stick is removed but after looking logs when we call painter->end() methods process freeze.
        PS O/P:
        root 1133 3.0 0.0 0 0 ? Zl 22:24 0:08 [MyApp] <defunct>

        Since we pass QPrinter as device to Qpainter and QPrinter output file path is set on usb device location which will be unavailable after removing usb stick, may be that's why paint->end() call failed becoz of resource unavailable

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #4

        @Vijay-R-Gawade said in Qt app freeze when remove usb drive while qpainter is drwaing:

        How we can delete qprinter object?

        Like any other object allocated via new:

        delete printer;
        

        But as @SGaist pointed out there is no need to allocated on the heap via new, just allocate on the stack:

        QPainter painter;
        
        V 1 Reply Last reply
        0
        • jsulmJ jsulm

          @Vijay-R-Gawade said in Qt app freeze when remove usb drive while qpainter is drwaing:

          How we can delete qprinter object?

          Like any other object allocated via new:

          delete printer;
          

          But as @SGaist pointed out there is no need to allocated on the heap via new, just allocate on the stack:

          QPainter painter;
          
          V Offline
          V Offline
          Vijay R. Gawade
          wrote on last edited by
          #5

          @jsulm I have tried to delete printer & painter with delete call and reinitialize both with new keyword, but app freezes as previous.
          root 1149 14.0 0.0 0 0 ? Zl 00:46 0:11 [MyApp] <defunct>

          jsulmJ 1 Reply Last reply
          0
          • V Vijay R. Gawade

            @jsulm I have tried to delete printer & painter with delete call and reinitialize both with new keyword, but app freezes as previous.
            root 1149 14.0 0.0 0 0 ? Zl 00:46 0:11 [MyApp] <defunct>

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @Vijay-R-Gawade Why don't you simply allocate them on the stack? Stack allocation is way faster than heap and you do not have to bother to delete the stuff again.

            Can you show your current code?

            V 1 Reply Last reply
            0
            • jsulmJ jsulm

              @Vijay-R-Gawade Why don't you simply allocate them on the stack? Stack allocation is way faster than heap and you do not have to bother to delete the stuff again.

              Can you show your current code?

              V Offline
              V Offline
              Vijay R. Gawade
              wrote on last edited by
              #7

              @jsulm

              ReportGenerator::ReportGenerator(QObject *parent) : QObject(parent)
              {
                  pdfTimer = new QTimer();
                  pdfTimer->setInterval(200);
                  connect(pdfTimer, SIGNAL(timeout()), this, SLOT(startPDFTimer()));
              
                  painter = new QPainter();
              
                  printer = new QPrinter();
                  printer->setOutputFormat(QPrinter::PdfFormat);
                  printer->setFullPage(true);
                  printer->setResolution(60);
                  printer->setOrientation(QPrinter::Portrait);
                  printer->setPaperSize(QPrinter::A3);
              }
              
              void ReportGenerator::pdfReport()
              {
              // reinitialize painter & printer  same as in constr when deleting obj in case of usb remove in startPDFTimer() func
                  printer->setOutputFileName(filename + ".pdf"); //media/sda1/EventLog.pdf
              
                  painter->begin(printer);
              
                  // drwaing some header using painter
              
                  pdfTimer->Start()
              }
              
              void ReportGenerator::startPDFTimer()
              {
                  // Read records from DB
                  // And draw using painter
              
              // Note: rm->usbIsReady: have state usb device connected or not
                  if (!globalQuery.next())
                  {
                      pdfTimer->stop();
                      painter->end();
              
                      emit reportGenerated();
                  }
                  else
                  {
                      // USB removed while some more records remaining
                      if (!rm->usbIsReady())
                      {
                          pdfTimer->stop();
              //delete painter;
                      //delete printer;
                          painter->end();
              
                          emit showErrorPopup()
                      }
                  }
              }
              
              jsulmJ 1 Reply Last reply
              0
              • V Vijay R. Gawade

                @jsulm

                ReportGenerator::ReportGenerator(QObject *parent) : QObject(parent)
                {
                    pdfTimer = new QTimer();
                    pdfTimer->setInterval(200);
                    connect(pdfTimer, SIGNAL(timeout()), this, SLOT(startPDFTimer()));
                
                    painter = new QPainter();
                
                    printer = new QPrinter();
                    printer->setOutputFormat(QPrinter::PdfFormat);
                    printer->setFullPage(true);
                    printer->setResolution(60);
                    printer->setOrientation(QPrinter::Portrait);
                    printer->setPaperSize(QPrinter::A3);
                }
                
                void ReportGenerator::pdfReport()
                {
                // reinitialize painter & printer  same as in constr when deleting obj in case of usb remove in startPDFTimer() func
                    printer->setOutputFileName(filename + ".pdf"); //media/sda1/EventLog.pdf
                
                    painter->begin(printer);
                
                    // drwaing some header using painter
                
                    pdfTimer->Start()
                }
                
                void ReportGenerator::startPDFTimer()
                {
                    // Read records from DB
                    // And draw using painter
                
                // Note: rm->usbIsReady: have state usb device connected or not
                    if (!globalQuery.next())
                    {
                        pdfTimer->stop();
                        painter->end();
                
                        emit reportGenerated();
                    }
                    else
                    {
                        // USB removed while some more records remaining
                        if (!rm->usbIsReady())
                        {
                            pdfTimer->stop();
                //delete painter;
                        //delete printer;
                            painter->end();
                
                            emit showErrorPopup()
                        }
                    }
                }
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #8
                This post is deleted!
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  What is the logic behind using that timer ?

                  You might also want to consider QPdfWriter.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  V 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    What is the logic behind using that timer ?

                    You might also want to consider QPdfWriter.

                    V Offline
                    V Offline
                    Vijay R. Gawade
                    wrote on last edited by
                    #10

                    @SGaist Timer is used to add some delay to Update Progress bar on UI by one step when each record from DB is drawn.
                    Right now I'm sure problem with paint->end() library call when usb stick is removed.
                    So I changed the O/P file path to Device temp dir and when all done copy it to usb device. And in case of usb-stick is removed at middle, paint->end() will call as normal (not crashing since Device Temp path available always)

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @Vijay-R-Gawade said in Qt app freeze when remove usb drive while qpainter is drwaing:

                      @SGaist Timer is used to add some delay to Update Progress bar on UI by one step when each record from DB is drawn.

                      That's a fishy procedure to update your progress bar.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved