Why does class member object 'dissapear', yet is not deconstructed?

Posted on 2006-10-28
Last Modified: 2010-04-01
Hi All,
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...


Question by:NeonSys
LVL 48

Expert Comment

ID: 17825113
Please show your code.

Author Comment

ID: 17825162
Thanks. there is lots of code - I hope this is sufficient.

this is the calling function (in "object C"):

void operon::transcribe(protein* proteome, int pSize, double toxins, double food)

      // must make some arbitrary rules to determine what influences what
      // create RN between 0:100; this is to decide whether to create protein from CODONS
      // we will decide whether to make uniform amount or not (i.e. made or not made)
      double dice,
            pProb = 1.0;            // this is probability of the protein being produced -- initially set at 1.0

      // loop through proteome to determine probabilities
      for (int i = 0; i < pSize; i++){                        // go through proteins in order (one entry per protein)

            // this is where we determine the likliness of the protein surviving, given the states of the cell and environment

            for (int j = 0; j < proteome[i].instances; j++){                        // do for every instance of protein i

                  pProb = 1.0;
                  dice = (double)(rand())/(double)(RAND_MAX);                  // generate random num

                  for (int k = 0; k < pSize; k++){                                    //get interactions with all other chemicals
                        pProb *= chemistryPtr->getProbability(proteome[i].identity,proteome[k].identity,proteome[k].concentration);
//cout <<"transcribed...\n";
                  if (dice < pProb){
                        proteome[i].concentration += 2;

// subtract an amount
                        proteome[i].concentration -= 2;
                        proteome[i].concentration = (proteome[i].concentration < 0) ? 0 : proteome[i].concentration;


this is the called function (in "object B" - note that the dissapearing matrix is reactions[][])

double chemistry::getProbability(int product, int reactant, double concentration)

      double modifier = 1.0;
      double epsilon = 25.0;


            case 0:{

            case 1:{
                  modifier = 0.99;


            case 2:{

                  modifier = (1.0 / (1.0 + exp((-1.0 * concentration) + epsilon)));

            case 3:{

                  modifier = (1.0 / (1.0 + exp( concentration - epsilon)));

            cout << "Illegal interaction: product " << product << " - reactant " << reactant << endl;
            exit (1);




this is the constructor for "object B" n.b. the values of reactions[][] are set to 0 <= x < 4 after construction and prior to creating "objects c"

chemistry::chemistry()                  // constructor
// set up the reactions matrix

      reactions = new int*[100];
      for (int i = 0;i < 100; i++){
            reactions[i] = new int[100];
            for (int j = 0;j < 100; j++)
                  reactions[i][j] = 100;

LVL 86

Expert Comment

ID: 17825794
You need to explicitly deallocate your 'reactions matrix', since that does not happen automatically in the destructor, i.e.

     for (int i = 0;i < 100; i++)
          delete [] reactions[i];

     delete [] reactions;
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

LVL 15

Accepted Solution

efn earned 125 total points
ID: 17826596
A few suggestions:

Show the code for the operon copy constructor.  Since the original objects work and the copied objects don't, the finger of suspicion points at the copy constructor.

Check the product and reactant parameters in the chemistry::getProbability function to make sure they are within the bounds of the reactions array.  Maybe the array has not disappeared, but the proteome array in the copied operon is messed up so the product or reactant parameter is out of bounds.

Try to construct a minimal, compilable demonstration of the problem and post it.  This exercise itself may lead you to a solution.

Author Comment

ID: 17828389
Thanks for your comments-

jkr - yes, I just didnt get round to putting in the reactions matrix deconstructor yet ;-) - i just wanted to see if there was a difference in behaviour when dynamically creating the matrix. thanks for pointing it out though.

efn - thanks, some good ideas to try;

> Check the product and reactant parameters in the chemistry::getProbability function to make sure they are within the bounds of the  >  reactions array.  Maybe the array has not disappeared, but the proteome array in the copied operon is messed up so the product > or reactant parameter is out of bounds.

Yes, I did check this (should have mentioned in the first post!) and they are within bounds.
I will indeed try to create an isolated version of the problem and post it...thats what I will do next.



Author Comment

ID: 18063056
Apologies to all for the seeming abandoment :-$   I totally forgot about the posting.

I was able to discover the cause of the problem, which was rather trivial. There was an 'intermediate' class in which a pointer was passed to. Rather embarrasingly this class was not setting the pointer correctly. Silly me indeed....

I will give the points to JKR as he made the most [usefull] suggestions. Thanks everyone else for your suggestions.

Embarrasingly yours,


Author Comment

ID: 18063060
oops, sorry JKR - I meant to say I would give the points to EFN.
As you can see, it is not my day,

best wishes!

LVL 86

Expert Comment

ID: 18063718

I told you what was going wrong.

Author Comment

ID: 18063799
JKR, sorry, no you didnt tell me what was going wrong. It wasnt the result of the reactions matrix not being deallocated. Of course you were quite right in saying the memory should be freed, but that was not what was causing *this* problem.

>> EFN: "Try to construct a minimal, compilable demonstration of the problem..."
doing this was what lead me to discovering the problem.

Sorry if you feel you should have had the points. I am grateful for your suggestion, and I probably should have somehow split the points.



Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question