Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. The program has unexpectedly finished.

The program has unexpectedly finished.

Scheduled Pinned Locked Moved Unsolved C++ Gurus
17 Posts 4 Posters 2.3k Views
  • 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.
  • R Offline
    R Offline
    Rajashekara_NR
    wrote on last edited by Rajashekara_NR
    #1

    #ifndef AMBIENTLIGHT_H
    #define AMBIENTLIGHT_H
    #include <QDebug>
    #include "serviceproxy.h"
    class Ambientlight
    {
    public:
    Ambientlight(Serviceproxy *serviceprxy = new Serviceproxy);
    ~Ambientlight();
    private:
    Serviceproxy *serviceprxy;
    };

    #endif // AMBIENTLIGHT_H
    #include "ambientlight.h"

    Ambientlight::Ambientlight(Serviceproxy *serviceprxy)
    {
    qDebug() << Q_FUNC_INFO;
    }

    Ambientlight::~Ambientlight()
    {
    delete serviceprxy;
    qDebug() << Q_FUNC_INFO;
    }
    #ifndef SERVICEPROXY_H
    #define SERVICEPROXY_H
    #include <QDebug>

    class Serviceproxy
    {
    public:
    Serviceproxy();
    ~Serviceproxy();
    };

    #endif // SERVICEPROXY_H
    #include "serviceproxy.h"

    Serviceproxy::Serviceproxy()
    {
    qDebug() << Q_FUNC_INFO;
    }

    Serviceproxy::~Serviceproxy()
    {
    qDebug() << Q_FUNC_INFO;
    }

    #include "widget.h"

    #include <QApplication>
    #include "ambientlight.h"
    #include "serviceproxy.h"
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Widget w;
    Ambientlight Al;
    Serviceproxy *serprxy = new Serviceproxy;
    Ambientlight AL2(serprxy);
    w.show();
    return a.exec();
    }


    output:
    Serviceproxy::Serviceproxy()
    Ambientlight::Ambientlight(Serviceproxy*)
    Serviceproxy::Serviceproxy()
    Ambientlight::Ambientlight(Serviceproxy*)
    Serviceproxy::~Serviceproxy()
    03:25:05: The program has unexpectedly finished.

    My question how to delete the serviceproxy object created in main.

    jsulmJ 1 Reply Last reply
    0
    • R Rajashekara_NR

      #ifndef AMBIENTLIGHT_H
      #define AMBIENTLIGHT_H
      #include <QDebug>
      #include "serviceproxy.h"
      class Ambientlight
      {
      public:
      Ambientlight(Serviceproxy *serviceprxy = new Serviceproxy);
      ~Ambientlight();
      private:
      Serviceproxy *serviceprxy;
      };

      #endif // AMBIENTLIGHT_H
      #include "ambientlight.h"

      Ambientlight::Ambientlight(Serviceproxy *serviceprxy)
      {
      qDebug() << Q_FUNC_INFO;
      }

      Ambientlight::~Ambientlight()
      {
      delete serviceprxy;
      qDebug() << Q_FUNC_INFO;
      }
      #ifndef SERVICEPROXY_H
      #define SERVICEPROXY_H
      #include <QDebug>

      class Serviceproxy
      {
      public:
      Serviceproxy();
      ~Serviceproxy();
      };

      #endif // SERVICEPROXY_H
      #include "serviceproxy.h"

      Serviceproxy::Serviceproxy()
      {
      qDebug() << Q_FUNC_INFO;
      }

      Serviceproxy::~Serviceproxy()
      {
      qDebug() << Q_FUNC_INFO;
      }

      #include "widget.h"

      #include <QApplication>
      #include "ambientlight.h"
      #include "serviceproxy.h"
      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      Widget w;
      Ambientlight Al;
      Serviceproxy *serprxy = new Serviceproxy;
      Ambientlight AL2(serprxy);
      w.show();
      return a.exec();
      }


      output:
      Serviceproxy::Serviceproxy()
      Ambientlight::Ambientlight(Serviceproxy*)
      Serviceproxy::Serviceproxy()
      Ambientlight::Ambientlight(Serviceproxy*)
      Serviceproxy::~Serviceproxy()
      03:25:05: The program has unexpectedly finished.

      My question how to delete the serviceproxy object created in main.

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

      @Rajashekara_NR First thing to do in such a situation is running through debugger.
      So, please do so and post the stack trace after crash here.

      Also, why do you allocate Serviceproxy on the heap?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      JonBJ 1 Reply Last reply
      1
      • R Offline
        R Offline
        Rajashekara_NR
        wrote on last edited by Rajashekara_NR
        #3

        @jsulm,
        My requirement is In AmbientLight i had created a heap object of serviceProxy and deleted in destructor. If someone had created an object of Ambientlight and they will pass Serviceproxy object then how to delete the serviceproxy object.

        JonBJ 1 Reply Last reply
        0
        • R Rajashekara_NR

          @jsulm,
          My requirement is In AmbientLight i had created a heap object of serviceProxy and deleted in destructor. If someone had created an object of Ambientlight and they will pass Serviceproxy object then how to delete the serviceproxy object.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @Rajashekara_NR
          I don't really understand what you are saying here, but @jsulm is saying it's simpler not to have to delete the object if instead you use:

          Serviceproxy serprxy;
          Ambientlight AL2(&serprxy);
          

          and serviceprxy no longer needs to be a class member variable.

          JonBJ 2 Replies Last reply
          2
          • R Offline
            R Offline
            Rajashekara_NR
            wrote on last edited by
            #5

            Hi @JonB,

            You are right if we create in stack and passing is fine. But still my program is crashing.

            Serviceproxy::Serviceproxy()
            Ambientlight::Ambientlight(Serviceproxy*)
            Serviceproxy::Serviceproxy()
            Ambientlight::Ambientlight(Serviceproxy*)
            Ambientlight::~Ambientlight()
            Serviceproxy::~Serviceproxy()
            Serviceproxy::~Serviceproxy()
            04:30:41: The program has unexpectedly finished.

            1 Reply Last reply
            0
            • jsulmJ jsulm

              @Rajashekara_NR First thing to do in such a situation is running through debugger.
              So, please do so and post the stack trace after crash here.

              Also, why do you allocate Serviceproxy on the heap?

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #6

              @Rajashekara_NR

              @jsulm said in The program has unexpectedly finished.:

              @Rajashekara_NR First thing to do in such a situation is running through debugger.
              So, please do so and post the stack trace after crash here.

              1 Reply Last reply
              0
              • J.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                you never pass serviceprxy to the class pointer

                Ambientlight::Ambientlight(Serviceproxy *serviceprxy) //passing on missing
                {
                     qDebug() << Q_FUNC_INFO;
                }
                
                Ambientlight::~Ambientlight()
                {
                      delete serviceprxy;
                      qDebug() << Q_FUNC_INFO;
                }
                

                so you call delete on a pointer pointing to some random memory address.

                Looks like thats your problem.

                Always initialise your variables! a nullptr would work just fine

                private:
                    Serviceproxy *serviceprxy{nullptr};
                

                or better, since you have a default heap allocation in the constructor anyway pass it on.

                Ambientlight::Ambientlight(Serviceproxy *serviceprxy) : serviceprxy(serviceprxy)
                {
                    qDebug() << Q_FUNC_INFO;
                }
                

                btw please rename your member class pointer this is highly confusing.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                3
                • R Offline
                  R Offline
                  Rajashekara_NR
                  wrote on last edited by Rajashekara_NR
                  #8

                  @J-Hilk
                  class Ambientlight
                  {
                  public:
                  Ambientlight(const QPointer<Serviceproxy> &serviceProxies = new Serviceproxy());
                  ~Ambientlight();
                  void test();
                  private:
                  QPointer<Serviceproxy> m_serviceprxy;

                  };
                  Ambientlight::Ambientlight(const QPointer<Serviceproxy> &serviceProxies):m_serviceprxy(serviceProxies)
                  {
                  qDebug() << Q_FUNC_INFO;
                  }

                  Ambientlight::~Ambientlight()
                  {
                  //if(m_serviceprxy!= nullptr)
                  qDebug() << Q_FUNC_INFO ;
                  delete m_serviceprxy;
                  }
                  int main(int argc, char *argv[])
                  {
                  QApplication a(argc, argv);
                  Widget w;
                  // Serviceproxy *serprxy = new Serviceproxy;
                  // Ambientlight Al(serprxy);
                  // Serviceproxy *serprxy2 = new Serviceproxy;
                  // Ambientlight AL2(serprxy2);
                  // Serviceproxy *serprxy3 = new Serviceproxy;
                  // Ambientlight AL3(serprxy3);
                  Serviceproxy serprxy;
                  Ambientlight Al(&serprxy);
                  Serviceproxy serprxy2;
                  Ambientlight AL2(&serprxy2);

                  // delete serprxy;
                  // delete serprxy2;
                  // delete serprxy3;
                  // serprxy->test();
                  // serprxy2->test();
                  // serprxy3->test();
                  w.show();
                  return a.exec();
                  }
                  output:
                  Serviceproxy::Serviceproxy()
                  Ambientlight::Ambientlight(const QPointer<Serviceproxy>&) QObject(0x7ffd1fb752d0)
                  Serviceproxy::Serviceproxy()
                  Ambientlight::Ambientlight(const QPointer<Serviceproxy>&) QObject(0x7ffd1fb752f0)
                  Ambientlight::~Ambientlight() QObject(0x7ffd1fb752f0)
                  virtual Serviceproxy::~Serviceproxy()
                  double free or corruption (out)
                  07:47:46: The program has unexpectedly finished.
                  Note: If i use Stack object i got crash.


                  output: If i use Serprxy as heap object.
                  Serviceproxy::Serviceproxy()
                  Ambientlight::Ambientlight(const QPointer<Serviceproxy>&)
                  Serviceproxy::Serviceproxy()
                  Ambientlight::Ambientlight(const QPointer<Serviceproxy>&)
                  Serviceproxy::Serviceproxy()
                  Ambientlight::Ambientlight(const QPointer<Serviceproxy>&)
                  virtual Serviceproxy::~Serviceproxy()
                  virtual Serviceproxy::~Serviceproxy()
                  virtual Serviceproxy::~Serviceproxy()
                  void Serviceproxy::test()
                  void Serviceproxy::test()
                  void Serviceproxy::test()


                  one quick update if you want i will run through debugger mode.

                  J.HilkJ 1 Reply Last reply
                  0
                  • R Rajashekara_NR

                    @J-Hilk
                    class Ambientlight
                    {
                    public:
                    Ambientlight(const QPointer<Serviceproxy> &serviceProxies = new Serviceproxy());
                    ~Ambientlight();
                    void test();
                    private:
                    QPointer<Serviceproxy> m_serviceprxy;

                    };
                    Ambientlight::Ambientlight(const QPointer<Serviceproxy> &serviceProxies):m_serviceprxy(serviceProxies)
                    {
                    qDebug() << Q_FUNC_INFO;
                    }

                    Ambientlight::~Ambientlight()
                    {
                    //if(m_serviceprxy!= nullptr)
                    qDebug() << Q_FUNC_INFO ;
                    delete m_serviceprxy;
                    }
                    int main(int argc, char *argv[])
                    {
                    QApplication a(argc, argv);
                    Widget w;
                    // Serviceproxy *serprxy = new Serviceproxy;
                    // Ambientlight Al(serprxy);
                    // Serviceproxy *serprxy2 = new Serviceproxy;
                    // Ambientlight AL2(serprxy2);
                    // Serviceproxy *serprxy3 = new Serviceproxy;
                    // Ambientlight AL3(serprxy3);
                    Serviceproxy serprxy;
                    Ambientlight Al(&serprxy);
                    Serviceproxy serprxy2;
                    Ambientlight AL2(&serprxy2);

                    // delete serprxy;
                    // delete serprxy2;
                    // delete serprxy3;
                    // serprxy->test();
                    // serprxy2->test();
                    // serprxy3->test();
                    w.show();
                    return a.exec();
                    }
                    output:
                    Serviceproxy::Serviceproxy()
                    Ambientlight::Ambientlight(const QPointer<Serviceproxy>&) QObject(0x7ffd1fb752d0)
                    Serviceproxy::Serviceproxy()
                    Ambientlight::Ambientlight(const QPointer<Serviceproxy>&) QObject(0x7ffd1fb752f0)
                    Ambientlight::~Ambientlight() QObject(0x7ffd1fb752f0)
                    virtual Serviceproxy::~Serviceproxy()
                    double free or corruption (out)
                    07:47:46: The program has unexpectedly finished.
                    Note: If i use Stack object i got crash.


                    output: If i use Serprxy as heap object.
                    Serviceproxy::Serviceproxy()
                    Ambientlight::Ambientlight(const QPointer<Serviceproxy>&)
                    Serviceproxy::Serviceproxy()
                    Ambientlight::Ambientlight(const QPointer<Serviceproxy>&)
                    Serviceproxy::Serviceproxy()
                    Ambientlight::Ambientlight(const QPointer<Serviceproxy>&)
                    virtual Serviceproxy::~Serviceproxy()
                    virtual Serviceproxy::~Serviceproxy()
                    virtual Serviceproxy::~Serviceproxy()
                    void Serviceproxy::test()
                    void Serviceproxy::test()
                    void Serviceproxy::test()


                    one quick update if you want i will run through debugger mode.

                    J.HilkJ Online
                    J.HilkJ Online
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @Rajashekara_NR

                    Note: If i use Stack object i got crash.

                    yes of course, you manually delete a stack allocated object, what else did you expect?


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    1
                    • JonBJ JonB

                      @Rajashekara_NR
                      I don't really understand what you are saying here, but @jsulm is saying it's simpler not to have to delete the object if instead you use:

                      Serviceproxy serprxy;
                      Ambientlight AL2(&serprxy);
                      

                      and serviceprxy no longer needs to be a class member variable.

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #10

                      @JonB said in The program has unexpectedly finished.:

                      but @jsulm is saying it's simpler not to have to delete the object if instead you use:
                      Serviceproxy serprxy;
                      Ambientlight AL2(&serprxy);

                      @Rajashekara_NR
                      [My bold now.] If you change to stack object you (must) no longer delete it....

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        Rajashekara_NR
                        wrote on last edited by
                        #11

                        @J-Hilk ,
                        No i am not deleting stack object.
                        Actually i am deleting dynamically allocated object.

                        J.HilkJ 1 Reply Last reply
                        0
                        • R Rajashekara_NR

                          @J-Hilk ,
                          No i am not deleting stack object.
                          Actually i am deleting dynamically allocated object.

                          J.HilkJ Online
                          J.HilkJ Online
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #12

                          @Rajashekara_NR said in The program has unexpectedly finished.:

                          No i am not deleting stack object.

                          Serviceproxy serprxy;
                          Ambientlight Al(&serprxy);
                          ...
                          Ambientlight::Ambientlight(const QPointer<Serviceproxy> &serviceProxies):m_serviceprxy(serviceProxies)
                          {
                          qDebug() << Q_FUNC_INFO;
                          }
                          
                          Ambientlight::~Ambientlight()
                          {
                          //if(m_serviceprxy!= nullptr)
                          qDebug() << Q_FUNC_INFO ;
                          delete m_serviceprxy;
                          }
                          

                          you're clearly deleting a stack allocated object by hand,

                          this is c++ you can shoot yourself in the food, and you did!


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          1 Reply Last reply
                          2
                          • R Offline
                            R Offline
                            Rajashekara_NR
                            wrote on last edited by Rajashekara_NR
                            #13

                            @J-Hilk ,

                            one quick question here:
                            Ambientlight::~Ambientlight()
                            {
                            //if(m_serviceprxy!= nullptr)
                            qDebug() << Q_FUNC_INFO ;
                            delete m_serviceprxy;
                            }

                            Is there a way to handle m_serviceprxy in destructor. Please let me know. It should work for both stack and heap objects.

                            jsulmJ 1 Reply Last reply
                            0
                            • R Rajashekara_NR

                              @J-Hilk ,

                              one quick question here:
                              Ambientlight::~Ambientlight()
                              {
                              //if(m_serviceprxy!= nullptr)
                              qDebug() << Q_FUNC_INFO ;
                              delete m_serviceprxy;
                              }

                              Is there a way to handle m_serviceprxy in destructor. Please let me know. It should work for both stack and heap objects.

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

                              @Rajashekara_NR said in The program has unexpectedly finished.:

                              Is there a way to handle m_serviceprxy in destructor

                              What do you want to "handle" in the destructor?
                              There is no need to delete it in destructor.

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • JonBJ JonB

                                @Rajashekara_NR
                                I don't really understand what you are saying here, but @jsulm is saying it's simpler not to have to delete the object if instead you use:

                                Serviceproxy serprxy;
                                Ambientlight AL2(&serprxy);
                                

                                and serviceprxy no longer needs to be a class member variable.

                                JonBJ Online
                                JonBJ Online
                                JonB
                                wrote on last edited by JonB
                                #15

                                @JonB said in The program has unexpectedly finished.:

                                @Rajashekara_NR
                                I don't really understand what you are saying here, but @jsulm is saying it's simpler not to have to delete the object if instead you use:
                                Serviceproxy serprxy;
                                Ambientlight AL2(&serprxy);

                                and serviceprxy no longer needs to be a class member variable.

                                I feel the OP is not reading anything here:

                                • Stack variable => no delete
                                • Using this code => no member variable.
                                1 Reply Last reply
                                0
                                • R Offline
                                  R Offline
                                  Rajashekara_NR
                                  wrote on last edited by Rajashekara_NR
                                  #16

                                  @jsulm ,
                                  Actually i had a task like earlier m_serviceprxy use normal with out Qpointers. Now i had done all the changes with Qpointer need to delete the m_serviceprxy in destructor. Becuase heap object is there in the AmbientLight constructor. We not restricting the AmbientLight object creation with serviceproxies like stack or heap. so ultimately we should delete m_serviceprxy should be there in ambienltLight destructor. It should work for both the use cases like this:

                                  Serviceproxy *serprxy = new Serviceproxy;
                                  Ambientlight Al(serprxy);
                                  Serviceproxy serprxy;
                                  Ambientlight Al(&serprxy);

                                  JonBJ 1 Reply Last reply
                                  0
                                  • R Rajashekara_NR

                                    @jsulm ,
                                    Actually i had a task like earlier m_serviceprxy use normal with out Qpointers. Now i had done all the changes with Qpointer need to delete the m_serviceprxy in destructor. Becuase heap object is there in the AmbientLight constructor. We not restricting the AmbientLight object creation with serviceproxies like stack or heap. so ultimately we should delete m_serviceprxy should be there in ambienltLight destructor. It should work for both the use cases like this:

                                    Serviceproxy *serprxy = new Serviceproxy;
                                    Ambientlight Al(serprxy);
                                    Serviceproxy serprxy;
                                    Ambientlight Al(&serprxy);

                                    JonBJ Online
                                    JonBJ Online
                                    JonB
                                    wrote on last edited by
                                    #17

                                    @Rajashekara_NR
                                    The same code in the destructor cannot handle both those cases. You can delete if something was newed, not otherwise. You cannot delete if it might be a stack variable.

                                    Either stipulate in your "contract" that Ambientlight requires a newed Serviceproxy which it then takes ownership of and deletes, or require the caller/outside world to deal with the Serviceproxy's lifetime.

                                    Or do it with QObject parentage.

                                    1 Reply Last reply
                                    0

                                    • Login

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