Check if a pointer was deleted in Qt for GUI Application
-
Hi,
I have this scenario: My GUI Application can search for Bluetooth Low Energy devices if the user hits the "scan" button.
So I have something like this://in my .h file QBluetoothDeviceDiscoveryAgent *discoveryAgent = nullptr; //In my .cpp file discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
I also have a reset button in my Application, which does something like clear Entry fields, disable buttons and so on. And in this Slot I also want to delete all Objects, so in my case discoveryAgent:
void Test::reset() { if(discoveryAgent) { delete discoveryAgent; } }
When I start my Programm and scan for devices, than all devices are listed. Then I click on my reset Button, and my object gets deleted (I think so, btw: how can I check if it is really deleted when debugging?).
When i then press the reset button for the second time (makes no sense, but just for testing), my program crashes. So I think my if statement is not correct to check if a pointer is available or deleted. So is there (maybe a Qt-intern?) check for if a object stil exixts or if its ponter is deleted? -
That is not a Qt issue. It is simply standard C++.
try
void Test::reset() { if(discoveryAgent) { delete discoveryAgent; discoveryAgent = nullptr; } }
Typically it is better in Qt to use deleteLater()
-
@SpaceToon I guess you have several things going on here.
- Why do you want to delete your QBluetoothDeviceDiscoveryAgent object when you do a reset?
Isn't it suffice to clear all the other input fields, and then when the user fill them out again and press some (I assume) Discover button, go and ask the QBluetoothDeviceDiscoveryAgent object to start() a discovery again?
If you have everything well laid out (i.e. deviceDiscovered() or finished() signals) you'll end up with the newly discovered device(s) - Given that you do need to delete your QBluetoothDeviceDiscoveryAgent object, please do so as @koahnig suggested.
- Why do you want to delete your QBluetoothDeviceDiscoveryAgent object when you do a reset?
-
@koahnig said in Check if a pointer was deleted in Qt for GUI Application:
That is not a Qt issue. It is simply standard C++.
try
void Test::reset() { if(discoveryAgent) { delete discoveryAgent; discoveryAgent = nullptr; } }
Typically it is better in Qt to use deleteLater()
Thank you very much, that worked! Do you mean just replacing delete with deleteLater()?
Why do you want to delete your QBluetoothDeviceDiscoveryAgent object when you do a reset?
Isn't it suffice to clear all the other input fields, and then when the user fill them out again and press some (I assume) Discover button, go and ask the QBluetoothDeviceDiscoveryAgent object to start() a discovery again?
If you have everything well laid out (i.e. deviceDiscovered() or finished() signals) you'll end up with the newly discovered device(s)
@Pablo-J-Rogina
Thank you for the question, I believe it has drawn my attention to a mistake in thinking. I thought that if I didn't delete "discoveryAgent" when reset is pressed ,discoveryAgent = new QBluetoothDeviceDiscoveryAgent (this);
would be called again. Here I thought that a new object is now being created, so that I then have two differentQBluetoothDeviceDiscoveryAgent
objects . But actually it is so that my object "deviceDiscoveryAgent" is simply overwritten if I do not delete it anddiscoveryAgent = new QBluetoothDeviceDiscoveryAgent (this);
is called.So actually, I do not have to delete it at all.
-
@SpaceToon said in Check if a pointer was deleted in Qt for GUI Application:
But actually it is so that my object "deviceDiscoveryAgent" is simply overwritten if I do not delete it and discoveryAgent = new QBluetoothDeviceDiscoveryAgent (this); is called.
Wait!
I was thinking of creating (i.e. new) a QBluetoothDeviceDiscoveryAgent object just once during the lifetime of your app, i.e. in the constructor of the object using that QBluetoothDeviceDiscoveryAgentBut if you're are doing a
discoveryAgent = new QBluetoothDeviceDiscoveryAgent (this);
every time reset() is called, then you'll be piling up new objects without deleting them = memory leak! -
@Pablo-J-Rogina Ah okay, so I was right with my first thought. So I think your suggestions, of creating the object once in the constructor, is more suitable for my use case. Thank you again! I think I have to read a little bit more about C++ objects and memory allocation. Have a nice day :)
-
@SpaceToon said in Check if a pointer was deleted in Qt for GUI Application:
Thank you very much, that worked! Do you mean just replacing delete with deleteLater()?
It would be
discoveryAgent->deleteLater();
Check out the documentation under the given link in my post.