OK, so I had this model that was working perfectly for a few days now and all of a sudden I get this error : access violation reading location. Since it was running "fine" last night, I can only assume I've been fortunate and undefined behavior has been working in my "favor" since I've gotten this model "working." Furthermore, the debugger points to the first line of my constructor for my LengthDistribution object.
Initially I was thinking that something had gone out of scope before it could get used, but I changed the scope of the LD object (I think) and still the error persists. Here's most of my main().
int main(int argc, char** argv)
{
int h_maxlength;
int h_mass;
int h_pause=0;
int h_showFutures=0;
int h_showPathways=0;
int h_aggModel=0;
int h_runModel=0;
int h_pathwayTrace=0;
int h_showCurrent=0;
int h_numberOfIterations=15;
int h_debugMode = 0;
int* initialConditions;
int h_hostIterations = 1;
int threads = 8;
//LD object representing initial conditions of the Stochastic Model
LengthDistribution* LD;
//Array of stochastic models that holds the results of each model run.
StochasticModel** resultHolder;
std::cout << "Run model? (1 - Yes, 2 - Enumerate, 3 - Integer Partition, 4- Test Operator) ";
std::cin >> h_runModel;
if(h_runModel == 1)
{
std::cout << "Enter number of iterations: ";
std::cin >> h_numberOfIterations;
std::cout << "Which type of aggregation model? (0 - Generalized, 1 - Becker-Doring, 2 - Knowles/Chou): ";
std::cin >> h_aggModel;
std::cout << "Enter desired mass (M): " ;
std::cin >> h_mass;
std::cout << "Enter desired maxlength (N): ";
std::cin >> h_maxlength;
std::cout << "Enter how many models to run: ";
std::cin >> h_hostIterations;
std::cout << "Enter how many threads to run: ";
std::cin >> threads;
//Initialize result holding array.
resultHolder = new StochasticModel* [h_hostIterations*threads];
//Create initial distribution from initial mass and desired maxlength.
temp_initialConditions = new int [h_maxlength];
for(int i = 0; i < h_maxlength; i++)
{
if(i == 0)
temp_initialConditions[i] = h_mass;
else
temp_initialConditions[i] = 0;
}
//Create LD object based on initial conditions.
LD = new LengthDistribution(temp_initialConditions, h_maxlength, h_debugMode);
srand(1);
//Allocate a stochastic model and give it a LD object as initial conditions.
resultHolder[0] = new StochasticModel(LD, h_numberOfIterations, h_showFutures, h_showPathways, h_pause, h_aggModel, h_pathwayTrace, h_showCurrent, h_debugMode);
//StochasticModel* model = new StochasticModel(LD, h_numberOfIterations, h_showFutures, h_showPathways, h_pause, h_aggModel, h_pathwayTrace, h_showCurrent, h_debugMode);
}
}
The appropriate constructor for the LD object that is triggering this error follows :
LengthDistribution::LengthDistribution(int* initialDistribution, int maxlength, int debug)
{
m_maxlength = maxlength;
m_debug = debug;
m_nonZeroCount = 0;
m_nonZerosIndicesCrop = new int[m_nonZeroCount];
m_baseStateHolder = new int*[m_nonZeroCount];
m_futureStateCount=0;
m_aggStateCount=0;
m_fragStateCount=0;
m_base=new int[maxlength];
m_myFreeEnergy=0;
m_r=0;
m_s=0;
m_q=0;
m_stateOrigin=0;
m_partitionFunction=1;
m_futureLDHolder= new LengthDistribution*[m_nonZeroCount];
m_futureStateHolder=new int*[m_nonZeroCount];
m_myTransitProb = 0;
m_lengthDistribution = new int[m_maxlength];
memcpy(m_lengthDistribution, initialDistribution, m_maxlength*sizeof(int));
m_lengthDistributionDouble = new double[m_maxlength];
for (int i = 0; i < m_maxlength; i++)
m_lengthDistributionDouble[i] = (double) m_lengthDistribution[i];
m_mass = h_calcMass(m_lengthDistribution, maxlength);
int* temp_lengthDistribution = new int[m_mass];
for(int i = 0; i < m_mass; i++)
{
if(i < maxlength)
temp_lengthDistribution[i] = m_lengthDistribution[i];
else
temp_lengthDistribution[i] = 0;
}
//m_lengthDistribution = temp_lengthDistribution;
for (int i = 0; i < maxlength; i++)
{
m_base[i] = 0;
}
for (int i = 0; i < m_nonZeroCount; i++)
{
m_nonZerosIndicesCrop[i] = 0;
m_baseStateHolder[i] = 0;
m_aggStateHolder[i] = 0;
m_fragStateHolder[i] = 0;
m_futureStateHolder[i] = 0;
m_aggLDHolder[i] = 0;
m_fragLDHolder[i] = 0;
m_futureLDHolder[i] = 0;
}
m_mlThreads = 8;
if ( (maxlength % m_mlThreads) ==0 )
m_mlBlocks = maxlength/m_mlThreads;
else
m_mlBlocks = maxlength/m_mlThreads+1;
m_transitProb = new double[m_futureStateCount];
m_transitRanges = new double[m_futureStateCount];
h_calcFreeEnergy();
}
I'd really appreciate some ideas and suggestions. Self-taught C++ has been fraught with distress.