giantsmall
September 14th, 2010, 02:35 PM
"An unhandled exception of type 'System.AccessViolationException' occurred in Generator układów planetarnych.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
I have no idea what I did wrong. Weird is that error started occuring after a few succesfull runs when I added one more variable to print in console, but when I comment out latest change error still apears :/
When error occurs program is pointing for lines:
cout << this->name;
or
for(int j = 0; j < this->Satelite[i]->NoOfSatelites; i++)
Anyone has any clue?
Thanks in advance for help.
code:
#include "stdlib.h"
#include "math.h"
#include <string>
#include <iostream>
using namespace std;
using namespace System;
const double pi = 3.14159265;
enum ObjectType
{
None = -1,
Star = 0,
Planet = 1,
Moon = 2,
};
const string StrObjectType[] = {"Star", "Planet", "Moon"};
class StellarObject
{
public:
string name;
ObjectType type;
unsigned long mass;
unsigned short NoOfSatelites;
unsigned int GetMass() {return mass;}
string GetName();
__declspec(property(get = GetMass, put = SetMass)) unsigned int Mass;
__declspec(property(get = GetName, put = SetName)) string Name;
void PrintObject(unsigned short VertPos = 0, unsigned short HorPos = 0);
void PrintSystem();
StellarObject(Random^, bool);
StellarObject(Random^, bool, unsigned short Type = 0);
StellarObject** Satelite;
~StellarObject();
};
StellarObject::StellarObject(Random ^rnd, bool Satelites, unsigned short Type)
{
this->name = "aaa";
this->mass = 10;
this->type = (ObjectType)Type;
this->NoOfSatelites = 0;
Satelite = NULL;
if(Satelites && Type++ < 2)
{
this->NoOfSatelites = rnd->Next()%5 + 1;
this->Satelite = new StellarObject*[this->NoOfSatelites];
for(int i = 0; i < this->NoOfSatelites; i++)
{
Satelite[i] = new StellarObject(rnd, true, (ObjectType)Type);
}
}
}
void StellarObject::PrintObject(unsigned short VertPos, unsigned short HorPos)
{
int i = 0;
Console::SetCursorPosition(10 * i++ + HorPos, VertPos);
cout << this->name;
Console::SetCursorPosition(10 * i++ + HorPos, VertPos);
cout << StrObjectType[this->type];
Console::SetCursorPosition(10 * i++ + HorPos, VertPos);
cout << this->mass;
}
void StellarObject::PrintSystem()
{
int i = 0;
Console::SetCursorPosition(10 * i++,0);
cout << "Name";
Console::SetCursorPosition(10 * i++,0);
cout << "Type";
Console::SetCursorPosition(10 * i++,0);
cout << "Mass";
this->PrintObject(1);
unsigned short count = 2;
if(this->NoOfSatelites)
{
for(int i = 0; i < this->NoOfSatelites; i++)
{
this->Satelite[i]->PrintObject(count++, 1);
if(this->Satelite[i]->NoOfSatelites)
{
for(int j = 0; j < this->Satelite[i]->NoOfSatelites; j++)
{
this->Satelite[i]->Satelite[i]->PrintObject(count++, 2);
}
}
}
}
}
StellarObject::~StellarObject()
{
if(this->NoOfSatelites)
{
for(int i = 0; i < this->NoOfSatelites; i++)
{
if(this->NoOfSatelites)
{
for(int j = 0; j < this->Satelite[i]->NoOfSatelites; i++)
{
delete this->Satelite[i]->Satelite[j];
}
delete this->Satelite[i]->Satelite;
}
delete this->Satelite[i];
}
delete this->Satelite;
}
}
int main()
{
srand(time(0));
Random ^rnd = gcnew Random;
do{
StellarObject star (rnd, true, 0);
star.PrintSystem();
cin.get();
star.~StellarObject();
}while (true);
}
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
I have no idea what I did wrong. Weird is that error started occuring after a few succesfull runs when I added one more variable to print in console, but when I comment out latest change error still apears :/
When error occurs program is pointing for lines:
cout << this->name;
or
for(int j = 0; j < this->Satelite[i]->NoOfSatelites; i++)
Anyone has any clue?
Thanks in advance for help.
code:
#include "stdlib.h"
#include "math.h"
#include <string>
#include <iostream>
using namespace std;
using namespace System;
const double pi = 3.14159265;
enum ObjectType
{
None = -1,
Star = 0,
Planet = 1,
Moon = 2,
};
const string StrObjectType[] = {"Star", "Planet", "Moon"};
class StellarObject
{
public:
string name;
ObjectType type;
unsigned long mass;
unsigned short NoOfSatelites;
unsigned int GetMass() {return mass;}
string GetName();
__declspec(property(get = GetMass, put = SetMass)) unsigned int Mass;
__declspec(property(get = GetName, put = SetName)) string Name;
void PrintObject(unsigned short VertPos = 0, unsigned short HorPos = 0);
void PrintSystem();
StellarObject(Random^, bool);
StellarObject(Random^, bool, unsigned short Type = 0);
StellarObject** Satelite;
~StellarObject();
};
StellarObject::StellarObject(Random ^rnd, bool Satelites, unsigned short Type)
{
this->name = "aaa";
this->mass = 10;
this->type = (ObjectType)Type;
this->NoOfSatelites = 0;
Satelite = NULL;
if(Satelites && Type++ < 2)
{
this->NoOfSatelites = rnd->Next()%5 + 1;
this->Satelite = new StellarObject*[this->NoOfSatelites];
for(int i = 0; i < this->NoOfSatelites; i++)
{
Satelite[i] = new StellarObject(rnd, true, (ObjectType)Type);
}
}
}
void StellarObject::PrintObject(unsigned short VertPos, unsigned short HorPos)
{
int i = 0;
Console::SetCursorPosition(10 * i++ + HorPos, VertPos);
cout << this->name;
Console::SetCursorPosition(10 * i++ + HorPos, VertPos);
cout << StrObjectType[this->type];
Console::SetCursorPosition(10 * i++ + HorPos, VertPos);
cout << this->mass;
}
void StellarObject::PrintSystem()
{
int i = 0;
Console::SetCursorPosition(10 * i++,0);
cout << "Name";
Console::SetCursorPosition(10 * i++,0);
cout << "Type";
Console::SetCursorPosition(10 * i++,0);
cout << "Mass";
this->PrintObject(1);
unsigned short count = 2;
if(this->NoOfSatelites)
{
for(int i = 0; i < this->NoOfSatelites; i++)
{
this->Satelite[i]->PrintObject(count++, 1);
if(this->Satelite[i]->NoOfSatelites)
{
for(int j = 0; j < this->Satelite[i]->NoOfSatelites; j++)
{
this->Satelite[i]->Satelite[i]->PrintObject(count++, 2);
}
}
}
}
}
StellarObject::~StellarObject()
{
if(this->NoOfSatelites)
{
for(int i = 0; i < this->NoOfSatelites; i++)
{
if(this->NoOfSatelites)
{
for(int j = 0; j < this->Satelite[i]->NoOfSatelites; i++)
{
delete this->Satelite[i]->Satelite[j];
}
delete this->Satelite[i]->Satelite;
}
delete this->Satelite[i];
}
delete this->Satelite;
}
}
int main()
{
srand(time(0));
Random ^rnd = gcnew Random;
do{
StellarObject star (rnd, true, 0);
star.PrintSystem();
cin.get();
star.~StellarObject();
}while (true);
}