Why does class member object 'dissapear', yet is not deconstructed?
Posted on 2006-10-28
Either I have a seemingly strange problem or there is somthing pretty fundamental I dont understant about c++.
I have an object (object A) that creates [one instance of] object B (which, amongst other things containing an array of ints), and objects C[100+]. During creation of C's, object A passes a pointer to object B (which is stored as a variable at class scope). Object A calls a function in C which in turn calls a function in B (via the prevoiusly set pointer to B) that accesses the integer array of B. This is working as expected...
After a number of object C's have been created (some destroyed, some not) object A creates new C [via an overwritten constructor which makes a copy based on an existing object C]. A pointer to object B is set in the newly created objects C.
Now, whenever obj A calls a function the new C object, which in turn calls a function accessing the array in object B, a segmentation fault occurs.
I dont understand why this is happening, as I know that (1) the array of ints in B is not being deconstructed (2) the first set of objects C can still run this function without causing a seg-fault (3) The pointer to B (set in the new C) is correct (4) functions in B, called from the new C which dont involve the array work fine (dont cause a seg fault).
So I know that the pointer to B in the new C is correct, and that the array member of B has not been deconstructed.
Further information: initially the int array in B was a private class member (class scope). I put in a routine in the function in object B to exit the program if the pointer to the array was NULL. It would always exit when a new C called the function, thus for some reason must have been null. I thought that perhaps the array was being deleted, so I changed this to be a dynamically created array in the constructor; in the deconstructor I put code to exit the prog if there was any deconstruction.
Following these changes, I just get a segmentation fault, without it saying the pointer to the array is NULL, or the object B (and thus the array) being deconstructed.
It just seems as if the array in B dissapears when called by an accessor function in B by a newly created C. Remeber that there is only 1 object B, which is never deconstructed. The pointers to this in both the original objects C, and the new objects C are equal.
I hope I am not being totally thick here. Solutions appreciated more than you could imagine...