the code is really all over the place.. but here is the most relevant part from the header file.
Code: ( text )
class APerson {
public:
APerson(Person* thePerson);
virtual ~APerson();
void SetPerson(Person* thePerson)
{
xyz = thePerson;
}
private:
Person* xyz;
xyz must be get deleted somewhere, and that must be the reason why it is crashing.
If xyz is ever deleted then the APerson object is trash. This is one reason to not use pointers as data members. Use handles instead. There's an article in the C/C++ Articles forum on Handle Classes that addresses this issue.
If xyz is 0 or has a value outside your process address space (like it was never initialized) you will crash when you use it.
If xyz is deleted, the deleter has to notify all APerson objects that have the pointer that is going to be deleted. This is a big, big job. Again, a handle rather than a pointer will solve this.
Can anyone advise on this please?
I have got a pointer like this
Code: ( text )
APerson* xyz = NULL;
xyz = abc->Caller();
abc is initialized somewhere publicly. Sometimes abc->Caller() returns 0xfeeefeee value and which will cause xyz to crush a later line.
So what I would like to is, I want to
Code: ( text )
if (xyz = 0xfeeefeee)
// do something
so what is the syntax for the if above? How can I check the address of a pointer?
This looks like a case where abc->Caller() is returning an uninitialized pointer. Don't bother hard-coding an address value to check as the garbage address will be different from one execution to the next.
Your solution is to make sure that Caller() does not return garbage.
When it comes to using pointers in C++ classes:
1) you must initialize them. Period.
2) they should point to items on the heap so that you are assured that what they point to won't be deleted by the compiler when some function completes
3) they cannot be deleted unless the deleter knows it is deleting the last copy of the pointer in the program. A handle covers this condition.
Finally,
[quote=ycinar]
the code is really all over the place..
is a bad sign. Try to get all the code for a class into one source file and the class declaration into one header file. Use separate headers for different classes and separate source files for different classes.