class Foo
{
/......./
};
int main()
{
Foo *p = new Foo();
Foo *p1 = p;
Foo *p2 = p;
Foo *p3 = p;
delete p;
}
// FooList1.h
vector<Foo*> FooList1;
// FooList2.h
vector<Foo*> FooList2;
class Foo
{
/......./
};
int main()
{
Foo *p = new Foo();
Foo *p1 = p;
Foo *p2 = p;
Foo *p3 = p;
delete p;
}
// FooList1.h
vector<Foo*> FooList1;
// FooList2.h
vector<Foo*> FooList2;
Quote:
so what happens to the 3 other pointers pointing to the that object? Are they now pointing to NULL or to nothing at all?
Quote:
If I empty one of the containers and delete all of it's pointers , does this mean that all of the pointers in the other container will become invalid and 'dangerous' to use because they will point to nothing?
Quote:
Lastly , a pointer that points to nothing , it has it's own address and memory location. Does the OS clean up pointers like that so it wont be considered as a memory leak?
Quote:Original post by Xsy
Thank's for the quick replies!
So to prevent errors with dangling pointers I just assign them to point to NULL , or otherwise just use a reference - counting smart pointer.
Quote:Original post by Xsy
Thank's for the quick replies!
So to prevent errors with dangling pointers I just assign them to point to NULL , or otherwise just use a reference - counting smart pointer.
Quote:Original post by Fiddler
Setting them to null or another invalid memory address (e.g. -1 or 0xDEADBEEF) is a good idea. This way, if you try to dereference them, you will get an instant access violation, which may save you tons of debugging time down the road. Note that dereferencing a regular dangling pointer may or may not crash; it can cause invisible random heap corruption; and it can pose a security risk.
Using "smart" pointers is generally a good idea, but they come with their own set of problems. Using a managed, garbage collected language is usually an even better idea (but I'm going to be flamed if I suggest this, so I won't). :) Of course, the best solution would be to use a language which cannot have dangling pointers / null references by design, like Ocaml - but suggesting that on gamedev would be akin to throwing petroleum into a raging fire...