Select to view content in your preferred language

Is it possible to delete graphic object from the GraphicsOverlay? (remove from the heap!)

1437
1
01-15-2018 06:15 AM
GeorgeArnaut
Emerging Contributor

Hi everyone!

ArcGIS ideology uses an overlay model for representing  maps and objects on them. 

If I want to create some object allocated on the heap, I write:

Graphic* obj = new Graphic(this); (and something inside)

Next step, I put this object to overlay:

m_graphicsOverlay->graphics()->append(graphic);

If I have a pointer to the object, I can manually delete it, if I want!

for example: delete obj;
I did it and all my application has crashed!


How can I delete an interesting item from overlay and also from the heap,  I want to avoid memory leackege ! 

0 Kudos
1 Reply
LukeSmallwood
Esri Contributor

Hi George,

Thanks for getting in touch. The behavior you are seeing is expected because, as you say, the GraphicListModel does not take ownership of the Graphics which you append to it. You do need to manage the lifetime of these objects yourself - one approach is to use another QObject as the parent of the Graphic, rather than "this".

So, for example, you could have a member variable (QObject* m_graphicsParent) on your class which you allocate prior to creating the graphics. Then each graphic would be created like so:

Graphic* obj = new Graphic(m_graphicsParent);

Then when you come to remove the Graphics you can just call:

delete m_graphicsParent;

m_graphicsOverlay->clear();

The above approach is for the case where you are adding and removing all of the graphics in the overlay in each pass - otherwise you may need to delete the graphics separately and call GraphicListModel::removeOne etc.

I hope that helps,

Luke