Solved

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

Posted on 2006-10-28
11
213 Views
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...


thanks,

neale
0
Comment
Question by:NeonSys
11 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17825113
Please show your code.
0
 

Author Comment

by:NeonSys
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;

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

}
0
 
LVL 86

Expert Comment

by:jkr
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;
0
 
LVL 15

Accepted Solution

by:
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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:NeonSys
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.

taa.

n
0
 

Author Comment

by:NeonSys
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,

Neale
0
 

Author Comment

by:NeonSys
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!

n
0
 
LVL 86

Expert Comment

by:jkr
ID: 18063718
????????

I told you what was going wrong.
0
 

Author Comment

by:NeonSys
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.

Appologies,

n
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now