A
A
Alexey2014-06-18 21:34:33
C++ / C#
Alexey, 2014-06-18 21:34:33

Is there a mechanism for checking the validity of pointers in C++?

Good day!
Is it possible somehow in C++ to check the pointer for the availability of the memory area it points to?
The situation is the following. There is a dll that defines the Delegate class, which is a representation of a function of some class (used for callbacks). Its constructor has the following signature:

template<typename T, typename ... Args> 
explicit Delegate(T* owner, (T::*function), Args ... args);

Operator() is also overloaded, which allows you to do something like this:
class Foo
{
public:
    void func() {}
};

Foo* f = new Foo;
Delegate d(f, &Foo::func);
d(); // вызовется f->func();

In the Delegate class, in the operator overload (), how to check the validity of the passed pointer in the constructor? Now, in the following scenario, the program crashes:
Foo* f = new Foo;
Delegate* d = new Delegate(f, &Foo::func);
delete f;
(*d)(); // здесь будет необработанное исключение

On the last line, an inaccessible memory area will be accessed. In this part, I'm only developing the Delegate class, so I can't be sure that the user will nullify the pointer after deleting it. Moreover, in the following situation (which is the target) it seems to be impossible:
class A
{
public:
    A() : delegate_(this, &A::funcA) {}
    Delegate callback() const { return delegate_; }
    void funcA() {}
private:
    Delegate delegate_;
};
class B
{
public:
    void setCallback(const Delegate& d)
    { delegate_ = d; }
    void event()
    { delegate_(); }
private:
    Delegate delegate_;
};
// -------------------------------------
A* classA = new A;
B* classB = new B;
classB->setCallback(classA->callback());
delete classA;
classB->event(); // печаль-беда

Actually, how can I check its validity in the Delegate class before calling the function on the pointer passed in the constructor? The program crashes with an "unhandled exception" due to "accessing an unreachable memory area", but even the try{} catch(...){} block does not catch anything... How do I handle this situation and find out that the pointer is pointing to garbage ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tsarevfs, 2014-06-18
@Renzo

For debugging, there is such a thing as a debug heap . As far as I understand, in the release of an adequate option to check the validity of the pointer can not be. You can try to get around this somehow. For example, to receive a shared_ptr, which will hint to the user that it is not necessary to delete the object ahead of time.

X
xandox, 2014-06-19
@xandox

What you want to do is not possible, and it is not necessary, if the user gave you a pointer and then freed it, then he himself is a fool and there is nothing to be done about it. The only thing I would advise you to use is shared_ptr, so that it would be easier for the user to keep track of the lifetime of his object. In principle, you can store weak_ptr and, when called, check whether it is rotten.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question