The program has unexpectedly finished.
-
#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.
-
@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?
-
@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. -
@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. -
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. -
@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. -
you never pass
serviceprxy
to the class pointerAmbientlight::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.
-
@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.
-
Note: If i use Stack object i got crash.
yes of course, you manually delete a stack allocated object, what else did you expect?
-
@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.... -
@J-Hilk ,
No i am not deleting stack object.
Actually i am deleting dynamically allocated object. -
@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!
-
@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.
-
@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. -
@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.
- Stack variable => no
-
@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); -
@Rajashekara_NR
The same code in the destructor cannot handle both those cases. You candelete
if something wasnew
ed, not otherwise. You cannotdelete
if it might be a stack variable.Either stipulate in your "contract" that
Ambientlight
requires anew
edServiceproxy
which it then takes ownership of anddelete
s, or require the caller/outside world to deal with theServiceproxy
's lifetime.Or do it with
QObject
parentage.