Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SingleShot-connection: will I get memory leaks like this?

SingleShot-connection: will I get memory leaks like this?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.3k 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.
  • T Offline
    T Offline
    themts
    wrote on last edited by
    #1

    Hey guys,

    I've implemented a single-shot connection with timeout and I'm not completely sure if I will run into memory leaks.

    //weak-connect to a functor with timeout
    template <typename Func1, typename Func2, typename Func3>
    static inline typename QtPrivate::QEnableIf<QtPrivate::FunctionPointer<Func2>::ArgumentCount == -1, QMetaObject::Connection>::Type
            weakConnect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, Func2 slot, uint timeout, Func3 timeoutSlot)
    {
        QMetaObject::Connection conn_normal = QObject::connect(sender, signal, sender, slot, Qt::DirectConnection);
        QMetaObject::Connection* conn_delete = new QMetaObject::Connection();
        QTimer* timer = NULL;
    
        *conn_delete = QObject::connect(sender, signal, [conn_normal, conn_delete](){
            if (timer) {
                timer->stop();
                timer->deleteLater();
            }
    
            QObject::disconnect(conn_normal);
            QObject::disconnect(*conn_delete);
            delete conn_delete;
        });
    
        if (timeout > 0) {
            timer = new QTimer;
            timer->setSingleShot(true);
            timer->setInterval(timeout);
    
            QObject::connect(timer, &QTimer::timeout, [conn_normal, conn_delete, timer, timeoutSlot] () {
                QObject::disconnect(conn_normal);
                QObject::disconnect(*conn_delete);
                delete conn_delete;
    
                timer->deleteLater();
                timeoutSlot();
            });
            timer->start();
        }
    
        return conn_normal;
    }
    

    you can call it like that

        weakConnect(engine, &Engine::positionReached, [&](bool reached) {
            ///fired if signal is emitted within 1000ms
        }, 1000, [&] {
            //fired if signal is NOT emitted within 1000ms
        });
    

    The idea was to have a simple single-use connection with lamda-expressions.
    I'm not completely sure if I will run into memory-leaks?
    deleteLater() needs an eventloop. What if it's called outside an event-loop? Could I delete the timer the timeout-slot by delete(timer)?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      taedium
      wrote on last edited by
      #2

      @themts said in SingleShot-connection: will I get memory leaks like this?:

      deleteLater() needs an eventloop. What if it's called outside an event-loop? Could I delete the timer the timeout-slot by delete(timer)?

      Yes, you could (I don't see why necessarily using a deleteLater here). Save that it looks good to me.

      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