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. Replacing QList of objects with QLIst of pointers to these objects

Replacing QList of objects with QLIst of pointers to these objects

Scheduled Pinned Locked Moved Solved General and Desktop
memory leakmemorymanagemen
4 Posts 2 Posters 790 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.
  • S Offline
    S Offline
    SpaceToon
    wrote on 17 Nov 2020, 16:29 last edited by SpaceToon
    #1

    Hi,

    As an Qt and C++ beginner, I have developed an application and now I want to do some refactoring concerning optimization and memory management.
    Consider the following piece of code:

    //ConnectionClass.h
      QBluetoothDeviceDiscoveryAgent *discoveryAgent = nullptr;
      QList< QBluetoothDeviceInfo > devicesList;
    
    //ConnectionClass.cpp
    void ConnectionClass::startDiscovery()
    {
    
        if(!devicesList.isEmpty())
        {
            devicesList.clear();
        }
        //because this function can be called several times
       // I want to make sure that the object is deleted first
        if(discoveryAgent)
        {
            delete discoveryAgent;
            discoveryAgent = nullptr;
        }
    
        discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
        discoveryAgent->setLowEnergyDiscoveryTimeout(6000);
        connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &ConnectionClass::deviceDiscovered);
        discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
    }
    
    void ConnectionClass::deviceDiscovered(const QBluetoothDeviceInfo &device)
    {
            devicesList.append(device);
    }
    
    void ConnectionClass::connect()
    {
        for (QBluetoothDeviceInfo device : devicesList)
        {
    
            if(device.name() == "Test")
            {
                doSomething();
                break;
            }
        }
    }
    

    So with this code, I start the device discovery. When a device is found, it is stored in my devicesList but not the pointer to the object but the whole onject itself. So when my startDiscovery() function is called several times, my devicesList is cleared ( the entries) but the objects in there are still "alive" because they did not get deleted. Is this right so far? If yes, then this means that I have unnecessary which occupy memory in my application. So a solution is not to store the objects themselves but pointers to them, then calling qDeleteAll() and then clear():

    So I tried using pointers to objects but there is somewhere a bug when calling connect:

      QList<const QBluetoothDeviceInfo * > devicesList;
    
    void ConnectionClass::deviceDiscovered(const QBluetoothDeviceInfo &device)
    {
            //&device instead of device
            devicesList.append(&device);
    }
    
    void ConnectionClass::connect()
    {
        for (const QBluetoothDeviceInfo *device : devicesList)
        {
    
            if(device->name() == "Test") // here I got an exception read access violation
            {
                doSomething();
                break;
            }
        }
    }
    
    

    So what might be the problem here? My debugger shows me that my devicesList is not empty..

    C 1 Reply Last reply 17 Nov 2020, 16:39
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 17 Nov 2020, 17:46 last edited by
      #4

      @SpaceToon said in Replacing QList of objects with QLIst of pointers to these objects:

      why in my solution with the pointers an exception occurs.

      Because the pointer you store a dangling - the object behind is already deleted when deviceDiscovered() is finished (or a little bit later, don't know the code which calls this function).

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • S SpaceToon
        17 Nov 2020, 16:29

        Hi,

        As an Qt and C++ beginner, I have developed an application and now I want to do some refactoring concerning optimization and memory management.
        Consider the following piece of code:

        //ConnectionClass.h
          QBluetoothDeviceDiscoveryAgent *discoveryAgent = nullptr;
          QList< QBluetoothDeviceInfo > devicesList;
        
        //ConnectionClass.cpp
        void ConnectionClass::startDiscovery()
        {
        
            if(!devicesList.isEmpty())
            {
                devicesList.clear();
            }
            //because this function can be called several times
           // I want to make sure that the object is deleted first
            if(discoveryAgent)
            {
                delete discoveryAgent;
                discoveryAgent = nullptr;
            }
        
            discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
            discoveryAgent->setLowEnergyDiscoveryTimeout(6000);
            connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &ConnectionClass::deviceDiscovered);
            discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
        }
        
        void ConnectionClass::deviceDiscovered(const QBluetoothDeviceInfo &device)
        {
                devicesList.append(device);
        }
        
        void ConnectionClass::connect()
        {
            for (QBluetoothDeviceInfo device : devicesList)
            {
        
                if(device.name() == "Test")
                {
                    doSomething();
                    break;
                }
            }
        }
        

        So with this code, I start the device discovery. When a device is found, it is stored in my devicesList but not the pointer to the object but the whole onject itself. So when my startDiscovery() function is called several times, my devicesList is cleared ( the entries) but the objects in there are still "alive" because they did not get deleted. Is this right so far? If yes, then this means that I have unnecessary which occupy memory in my application. So a solution is not to store the objects themselves but pointers to them, then calling qDeleteAll() and then clear():

        So I tried using pointers to objects but there is somewhere a bug when calling connect:

          QList<const QBluetoothDeviceInfo * > devicesList;
        
        void ConnectionClass::deviceDiscovered(const QBluetoothDeviceInfo &device)
        {
                //&device instead of device
                devicesList.append(&device);
        }
        
        void ConnectionClass::connect()
        {
            for (const QBluetoothDeviceInfo *device : devicesList)
            {
        
                if(device->name() == "Test") // here I got an exception read access violation
                {
                    doSomething();
                    break;
                }
            }
        }
        
        

        So what might be the problem here? My debugger shows me that my devicesList is not empty..

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 17 Nov 2020, 16:39 last edited by
        #2

        @SpaceToon said in Replacing QList of objects with QLIst of pointers to these objects:

        . Is this right so far? I

        No, since these are objects and no pointers, clear() deletes them.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        S 1 Reply Last reply 17 Nov 2020, 16:47
        1
        • C Christian Ehrlicher
          17 Nov 2020, 16:39

          @SpaceToon said in Replacing QList of objects with QLIst of pointers to these objects:

          . Is this right so far? I

          No, since these are objects and no pointers, clear() deletes them.

          S Offline
          S Offline
          SpaceToon
          wrote on 17 Nov 2020, 16:47 last edited by
          #3

          @Christian-Ehrlicher Oh, I thought that's why it is preferred to store pointers in a list and not the objects themselves. So then it is probably because if the objects are too big it would "take" too many resources. So my actual way is right after all (as long as the objects are not "too big"). But I do not understand why in my solution with the pointers an exception occurs.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 17 Nov 2020, 17:46 last edited by
            #4

            @SpaceToon said in Replacing QList of objects with QLIst of pointers to these objects:

            why in my solution with the pointers an exception occurs.

            Because the pointer you store a dangling - the object behind is already deleted when deviceDiscovered() is finished (or a little bit later, don't know the code which calls this function).

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            2

            4/4

            17 Nov 2020, 17:46

            • Login

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