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. Dynamic allocation in QT: why "delete" operator in openglwindow example?
Forum Update on Monday, May 27th 2025

Dynamic allocation in QT: why "delete" operator in openglwindow example?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 621 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.
  • V Offline
    V Offline
    viruca
    wrote on 20 Oct 2015, 18:40 last edited by SGaist
    #1

    I'm new in QT and was trying to figure out how the dynamic allocation works. I was reviewing the OpenGL Window Example, and noticed that:

    1. One of the class members for OpenGLWindow class - m_device *QOpenGLPaintDevice is allocated dynamically; in the destructor the memory is freed by calling delete
    2. There is another class member QOpenGLContext *m_context which is allocated dynamically as well; yet it is not freed in the destructor.

    I was wondering if anyone could explain me why is it so? Why in this particular example QOpenGLPaintDevice is chosen to be destroyed and QOpenGLContext - not?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 20 Oct 2015, 19:11 last edited by
      #2

      hi , many items in Qt are inserted into
      containers or a parent and it will take ownership of the
      object.
      Like buttons you insert into a window. They will be deleted with the Window.
      so most likely the creation of the m_context assigns it to a parent and it will be deleted with that.
      m_context = new QOpenGLContext(this);
      this being the window.

      1 Reply Last reply
      1
      • V Offline
        V Offline
        viruca
        wrote on 20 Oct 2015, 19:18 last edited by
        #3

        Thank you for fast explanation! Makes much more sense now.

        1 Reply Last reply
        0

        1/3

        20 Oct 2015, 18:40

        • Login

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