Link to home
Start Free TrialLog in
Avatar of NeonSys
NeonSys

asked on

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

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...


thanks,

neale
Avatar of AlexFM
AlexFM

Please show your code.
Avatar of NeonSys

ASKER

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;

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

      return;
}

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;

      switch(reactions[product][reactant]){

            case 0:{
                  break;
            }

            case 1:{
                  modifier = 0.99;

                  break;
            }

            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);

      }

      return(modifier);

}

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;
      }

}
Avatar of jkr
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;
ASKER CERTIFIED SOLUTION
Avatar of efn
efn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NeonSys

ASKER

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.

taa.

n
Avatar of NeonSys

ASKER

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,

Neale
Avatar of NeonSys

ASKER

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!

n
????????

I told you what was going wrong.
Avatar of NeonSys

ASKER

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.

Appologies,

n