0

I am using QSharedPointer for an object which contains slot function. My expectation is that even though the object that contains this pointer get destroyed but it would live on and process the QProcess::finished signal but it doesn't.

I create a QProcess and connect it to slot. The Pdf object will soon get destroyed though so I using QSharedPointer so m_symLinksobject stays alive and process the finished signal but it dies too before processing it.

void PDf::createPDF()
{
    // ....
    QProcess *myProcess= new QProcess();

    connect(myProcess, SIGNAL(finished(int)), m_symlinks.data(), SLOT(createPdfLink(int)) );

    myProcess->start("sh",args); // yes args is valid variable defined (now shown)

    // note if I waitForFinished() here than my slot does get called but the slot object seems to die if I don't wait here.
} 

The header file is:

class Pdf: public QObject
{
    Q_OBJECT

    Pdf() : QObject(parent)
    {
       m_symlinks = (QSharedPointer <Symlinks>) (new Symlinks);    
    }

    QSharedPointer <Symlinks> m_symlinks;

}
demonplus
  • 5,613
  • 12
  • 49
  • 68
zar
  • 11,361
  • 14
  • 96
  • 178
  • Why is it your expectation that the QSharedPointer will continue to exist here in the first place? – Alex P Nov 03 '15 at 22:28
  • It is correct behavior that all class members are destroyed if the object is destroyed. Does it make sense to use object pointed by `m_symlinks` after deletion of `Pdf`? What is use case? – Orest Hera Nov 03 '15 at 22:30
  • I do not understand why `m_symlinks` should be managed by `Pdf`. Is it possible to reuse the same `m_symlinks` for different calls `PDf::createPDF()` with different `myProcess` isntances? If the symlink is used only for one process, then it makes sense to create `new Symlinks` in `createPDF()` and set symlink as child of `myProcess`. So, it will be deleted together with `myProcess`. – Orest Hera Nov 03 '15 at 22:41
  • @OrestHera Yes that make more sense to me. Basically I need my slot object to survive and be called when `QProcess` finishes than destroy it and I think what you said make sense. I do use `m_symlinks` in the same class for other purposes as well (it creates txt link as well but in plain functions not in another `QProcess`) – zar Nov 03 '15 at 22:57
  • @OrestHera So to `symlinks` child of `QProcess`, I just need to pass the pointer to constructor like `new symlinks( myProcess)` and it will be deleted automatically when `QProcess` finishes? – zar Nov 04 '15 at 01:11
  • In general deletion of any object should not be mananaged by different owners. `QSharedPointer` should not be used if an object can be deleted by another mechanism, for example by its `QObject` parent. If `QObject` has parent, it is deleted once its parent is deleted. So, `symlinks` may be deleted when `QProcess` is deleted (not just finished!). You should think about relations between object and their lifetime. If you need help with such design of the project you need to update the question by clear requirements and usage of those objects. – Orest Hera Nov 04 '15 at 01:33
  • @OrestHera It changed the context of the question quite a bit so I posted a new one http://stackoverflow.com/questions/33530592/how-to-destroy-qprocess-when-it-finishes-and-the-class-than-contaed-the-slot – zar Nov 04 '15 at 19:41

0 Answers0