0

I'm allocating a struct dynamically in one of my classes in my Qt project. Then I pass the pointer to this struct to another variable in another class. And then in some situations I delete the first allocation, so I won't have the struct any more! Now here's the question:

I want my second pointer to find out if the slot it is pointing to is allocated or not, how can I do that?

Thank you in advance, please let me know if you need any part of my code

Vahid Nateghi
  • 576
  • 5
  • 14
  • same isuee was disscused in here - [LINK][1] [1]: http://stackoverflow.com/questions/3065092/check-if-a-pointer-points-to-allocated-memory-on-the-heap – Idan Yehuda Apr 08 '14 at 05:07

3 Answers3

0

Short answer, you can't.

Long answer, it depends on what memory allocation library or implementation of the allocation library or debugging replacement of the allocation library you're using and what kind of debugging features it supports.

jsantander
  • 4,972
  • 16
  • 27
0

Either a) Don't do it, i.e. never store references to dynamically allocated items, or b) Don't pass the pointer pass an index into an allocation table. Your best bet is to not directly delete it but use reference counting and only delete when there are no references left, (garbage collection), otherwise you will set to null on delete and have to check everywhere before use that it is not null. Note that if you really need to do this sort of thing a lot then switch to python which does this automatically for you all the time. There are QT bindings for python.

Take a look at these answers.

Community
  • 1
  • 1
Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
0

If Qt code cannot delete your pointer, then you can use std::shared_ptr and std::weak_ptr to help you track of these things.

#include <iostream>
#include <memory>

struct Foo { int a; };

int main()
{
        std::shared_ptr<Foo> shared = std::make_shared<Foo>();
        std::weak_ptr<Foo> weak = shared;

        std::cout << "is valid?: " << (bool)shared << weak.expired() << std::endl;
        shared.reset(); // delete the pointer   
        std::cout << "is valid?: " << (bool)shared << weak.expired() << std::endl;
        return 0;
}

results in,

is valid?: 10
is valid?: 01

meaning after the shared_ptr releases its allocated data, the weak_ptr expires as well (expired returns true).

Qt has similar classes to help you. QSharedPointer, QWeakPointer, etc. If you need to keep track if a QObject pointer is deleted, you should use QPointer

http://qt-project.org/doc/qt-5/QPointer.html

A guarded pointer, QPointer, behaves like a normal C++ pointer T *, except that it is automatically set to 0 when the referenced object is destroyed (unlike normal C++ pointers, which become "dangling pointers" in such cases). T must be a subclass of QObject.

for example,

#include <QCoreApplication>
#include <QDebug>
#include <QPointer>

int main(int a, char *b[])
{
    QCoreApplication app(a,b);
    QObject *obj1 = new QObject;
    QObject *obj2 = new QObject(obj1);
    QPointer<QObject> o(obj2);

    qDebug() << o.isNull();
    delete obj1;
    qDebug() << o.isNull();

    return 0;
}

results in,

false
true

As you can see, the child QObject pointer is invalidated.

user3427419
  • 1,769
  • 11
  • 15