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. QHash & dynamic_cast & memory leak

QHash & dynamic_cast & memory leak

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 1.7k Views 3 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.
  • ask4A Offline
    ask4A Offline
    ask4
    wrote on last edited by kshegunov
    #1

    Hi
    I have dynamic creation of widgets that I appending in QHash<int,QWidget*>
    is this good valid approach?

    bool CLASS::IsColorGraph(const QWidget * widget)
    {
       return dynamic_cast<const ColorGraph*>(widget) != 0;
    }
    ...
    if(IsColorGraph(qhash[ID]){
      ColorGraph * b = dynamic_cast<ColorGraph *>(qhash[ID]);
      b->SetColor(QColor(Qt::red));
    }
    

    what will heppen with b pointer? is it memory leak?

    [Added code tags ~kshegunov]

    kshegunovK 1 Reply Last reply
    0
    • m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by
      #2

      Hi,

      it's just a cast, so no leak.

      -Michael.

      1 Reply Last reply
      4
      • ask4A ask4

        Hi
        I have dynamic creation of widgets that I appending in QHash<int,QWidget*>
        is this good valid approach?

        bool CLASS::IsColorGraph(const QWidget * widget)
        {
           return dynamic_cast<const ColorGraph*>(widget) != 0;
        }
        ...
        if(IsColorGraph(qhash[ID]){
          ColorGraph * b = dynamic_cast<ColorGraph *>(qhash[ID]);
          b->SetColor(QColor(Qt::red));
        }
        

        what will heppen with b pointer? is it memory leak?

        [Added code tags ~kshegunov]

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @ask4 said in QHash & dynamic_cast & memory leak:

        what will heppen with b pointer? is it memory leak?

        The pointer will be freed when the stack unwinds, and as a consequence the pointer can't be leaked.

        ColorGraph * b = dynamic_cast<ColorGraph *>(qhash[ID]);
        

        As you already know that the type of that element is ColorGraph you can reinterpret_cast the pointer safely at that point, although the "gain" would be that you skip one lookup through the virtual table, which is practically nothing.

        Read and abide by the Qt Code of Conduct

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

          Hi,

          To add to my fellows, since you are dealing with QObject based classes, you should use qobject_cast.

          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
          2
          • ask4A Offline
            ask4A Offline
            ask4
            wrote on last edited by
            #5

            thanks lads!

            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