Program crash (after destructors?)

Hi everybody,

I wrote a program that runs perfectly until when it has to finish, and then it crash.
I am using some classes, so I am guessing that the problem may concerns destructors, but I am not sure. The code is quite long, and I am not sure on what to post.
Here is my main:

#include "BS.h"

using namespace std;

int main(int argc, char *argv[])
{

BS A;

A.getOperator();
A.Solver();


cout << endl << "End of the program!" << endl ;

system("PAUSE");
return EXIT_SUCCESS;
}


basically the program crashes after the "system ("PAUSE")"...any idea?
If there is need to post any other parts, just tell me, I didn't want to post everything and didn't know what to post.

Thanks!
Last edited on
are you deleting some pointer in the destructor ?
Yes...

BS::~BS()
{
cout << "calling BS destructor" << endl;
delete [] mDiag;
delete [] mUpper;
delete [] mLower;

};

where mDiag are *double inherited from another class and initialized as "mDiag = new double [size]"
Last edited on
If those memers are inherited... you mean they're not part of BS? If they're not part of BS then BS shouldn't be deleting them. Whatever class owns them should be.

But anyway... there's got to be something wrong with your memory management. Either that or you're stepping out of bounds somewhere. But it's pretty much impossible to tell with these tiny code fragments you're giving us.

You need to post more code. Show us this BS class.
Well, even if they are inherited they are part of the class, don't they?

Here is the class, as said it's qiute long:

#ifndef CLASS_BS
#define CLASS_BS

//////////BS.h/////////////
#include "PdeH.h"


class BS:public PH
{
protected:
int OptionType;
double K, sigma, r;
public:
BS();
~BS();
BS(const BS&);
BS(int);
double max(double, double);
void IC(int);
void getOperator();
void Solver();
};

#endif


/////////////////BS.cpp////////////////
#include "Black-Scholes.h"



BS::BS()
{

};

BS::~BS()
{
cout << "calling BS destructor" << endl;
delete [] mDiag;
delete [] mUpper;
delete [] mLower;

};

BS::BS(const BS& F)
{
mDiag = F.mDiag;
mUpper = F.mUpper;
mLower = F.mLower;
}

void BS::IC (int P)
{
initialize (P);

}

double BS::max(double a, double b)
{
if (a>=b)
{return a;}
else
return b;
}

void BS::getOperator()
{
sigma = 0.2;
K = 60;
r = 0.03;
cout << "Please say if you want to evaluate the price for:" << endl <<"1- CallOption" << endl;
cout << "2- PutOption" << endl;
cin >> OT;
while ((OT != 1)&&(OT != 2))
{
cout << "Error! Please choose 1 for a CallOption or 2 for a PutOption:" << endl;
cin >> OptionType;
}

cout << "Please, enter the time (in days) to maturity of your option: " << endl;
cin >> T;

cout << "How many time steps do you want to use?" << endl;
cin >> t_steps;
cout << "How many different asset prices value do you want to input?" << endl;
cin >> x_steps;
initialize (x_steps);
x_axis[0] = 10;
x_axis[size -1] = 250;
dx = (x_axis[size -1] - x_axis[0])/x_steps;
dt = T/(t_steps);

for (int i=1; i<size -1; i++)
{
x_axis[i]= x_axis[0] + i*dx; //S(i)= S(0) + i*DELTAS

};

cout << endl << "Which scheme would you like to use to solve your equation? Please enter theta." << endl;

cin >> theta;

while ((theta<0)||(theta>1))
{
cout << endl << "Error!theta must be within the interval [0,1]." << endl;
cout << "Please enter an appropriate theta:" << endl;
cin >> theta;
}

initialize(size);

};


void BS::Solver()
{
///////////////Initial Conditions//////////////////////////////////////

cout << endl << "Now we will apply the initial conditions:" << endl;
field temp2(size);

switch (OT)
{
case 1:
for (int i=0;i<size;i++)
{
temp2.data[i] = max(K-x_axis[i], 0);
}
break;
case 2:
for (int i=0;i<size;i++)
{
temp2.data[i] = max(x_axis[i]-K, 0);
}
break;

}

cout << "initial conditions correctly applied." << endl<< endl;


//////////////////Left hand size//////////////////////////////////
switch (OT)
{
case 1:

for (int i = 0; i<size-1; i++)
{
mDiag[i] = 1 + theta*dt*(r + ((sigma*x_axis[i])/dx)*(sigma*x_axis[i])/dx);
mLower[i]= -(theta*dt*(x_axis[i])/(2*dx))*(r+(sigma*sigma)*(x_axis[i])/dx);
mUpper[i]= (theta*dt*(x_axis[i])/(2*dx))*(r-(sigma*sigma)*(x_axis[i])/dx);
}

mDiag[size-1] = 1 + theta*dt*(r + ((sigma*x_axis[size-1])/dx)*(sigma*x_axis[size-1])/dx);

break;

case 2:

for (int i = 0; i<size-1; i++)
{
mDiag[i] = 1 + theta*dt*(r + ((sigma*x_axis[i])/dx)*((sigma*x_axis[i])/dx));
mLower[i]= -(theta*dt*(x_axis[i])/(2*dx))*(r+(sigma*sigma)*(x_axis[i])/dx);
mUpper[i]= (theta*dt*(x_axis[i])/(2*dx))*(r-(sigma*sigma)*(x_axis[i])/dx);
}

mDiag[size-1] = 1 + theta*dt*(r + ((sigma*x_axis[size-1])/dx)*((sigma*x_axis[size-1])/dx));
break;
}

cout << "Left hand side correctly computed."<< endl << endl;

//////////////////Right hand size//////////////////////////////////

TriMatrix M2(size);

switch (OT)
{
case 1:

for (int i = 0; i<size-1; i++)
{
M2.mDiag[i] = 1 - (1-theta)*dt*(r + ((sigma*x_axis[i])/dx)*(sigma*x_axis[i])/dx);
M2.mLower[i]= ((1-theta)*dt*(x_axis[i])/(2*dx))*(r+(sigma*sigma)*(x_axis[i])/dx);
M2.mUpper[i]= ((1-theta)*dt*(x_axis[i])/(2*dx))*(r-(sigma*sigma)*(x_axis[i])/dx);
}

M2.mDiag[size-1]= 1 - (1-theta)*dt*(r + ((sigma*x_axis[size-1])/dx)*(sigma*x_axis[size-1])/dx);

break;

case 2:

for (int i = 0; i<size-1; i++)
{
M2.mDiag[i] = 1 - (1-theta)*dt*(r + ((sigma*x_axis[i])/dx)*((sigma*x_axis[i])/dx));
M2.mLower[i]= ((1-theta)*dt*(x_axis[i])/(2*dx))*(r+(sigma*sigma)*(x_axis[i])/dx);
M2.mUpper[i]= ((1-theta)*dt*(x_axis[i])/(2*dx))*(r-(sigma*sigma)*(x_axis[i])/dx);
}

M2.mDiag[size-1] = 1 - (1-theta)*dt*(r + ((sigma*x_axis[size-1])/dx)*((sigma*x_axis[size-1])/dx));

break;
}

cout << "Right hand side correctly computed."<< endl << endl;

TriMatrix H(size);

field temp1(size);

solution.dimension = size;
solution.data = new double [size];

for (int i=0; i<(size-1); i++)
{
H.mUpper[i] = mUpper[i];
H.mLower[i]= mLower[i];
H.mDiag[i]= mDiag[i];
}

H.mDiag[size-1]= mDiag[size-1];

for (int j=0; j < t_steps; j++)
{

for (int i=0; i < size; i++)
{
temp1 = M2*temp2;
}

temp2= H / temp1; /
switch (OpT)
{
case 1:
temp2.data[0]= 0;
temp2.data[size-1] = x_axis[size-1] -K*exp(-r*j*dt);
break;
case 2:
temp2.data[0]= K*exp(-r*j*dt) - x_axis[size-1];
temp2.data[size-1] = 0;
break;
}

if (j == t_steps-1)
{
// cout << "inside"<< endl;
solution.dimension = size;
for (int h=0; h < size; h++)
{
solution.data[h] = temp2.data[h];

}
}
}
cout << endl << "Here is our solution:" << endl;
solution.printarray();

}


Thanks

code tags would help... http://cplusplus.com/articles/firedraco1/

Anyway

Well, even if they are inherited they are part of the class, don't they?


You're kind of missing the point of OOP design. The idea is to have a class manage itself and not have users of the class be responsible for managing it. A class should "just work"

Here, PH does not "just work" because BS is assuming the responsibility for its members.

Case in point: this problem you're having could be anywhere in this BS class or it could be in your PH class. So now in order to find the problem, we basically have to scour your entire program looking for bugs rather than just limiting ourselvse to a single class.

Anywho... I don't see where these members are allocated with new (I'm assuming that's in PH somewhere) so I can't tell whether or not you're going out of bounds.

I do see an problem that might be related though:

Your copy ctor is faulty. You're just copying the pointers. If you do something like this:

1
2
3
4
BS a;
BS b(a);  // make a copy;

// BANG YOU'RE DEAD 


when a is destroyed, it will delete[] the buffers. Then when b is destroyed it will delete[] the same buffers (because all you did was copy the pointer, not create a new buffer). Deleting the same buffer multiple times = explode.

Plus -- why is BS constructing PH's members? PH should be constructing PH's members!


Also the names of your classes could use some improvement. What are BS and PH supposed to be?


In the code

1
2
3
4
5
6
BS::BS(const BS& F)
{
mDiag = F.mDiag;
mUpper = F.mUpper;
mLower = F.mLower;
}
.
way of assining the array is faulty .
Topic archived. No new replies allowed.