Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Replacing QList of objects with QLIst of pointers to these objects



  • 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..


  • Lifetime Qt Champion

    @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).


  • Lifetime Qt Champion

    @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.



  • @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.


  • Lifetime Qt Champion

    @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).