Unhandled exception..
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Unhandled exception..

  1. #1
    Join Date
    May 2010
    Location
    Poland
    Posts
    4

    Unhappy Unhandled exception..

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

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Unhandled exception..

    This is Managed C++, so you'll get more help in that forum. (Native C++ does not have a namespace "System", for one, nor does it use the ^ symbol in that way.)

    However, I do find it suspicious that a j-based for loop is incrementing i instead of j.

  3. #3
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,019

    Re: Unhandled exception..

    [ Moved thread ]

  4. #4
    Join Date
    Aug 2010
    Posts
    47

    Re: Unhandled exception..

    I agree with Lindley about the i++ in that loop being suspicious.

    It looks to me like in the loop where you're deleting your Satellite objects that since you're never incrementing j and incrementing i instead that you'll go into an infinite loop until it tries to delete something which doesn't exist... causing your crash. If you commented out that delete line, you'd probably find that it would just sit there looping forever instead of crashing.

    Long story short: try a j++ in that for loop instead of an i++.

  5. #5
    Join Date
    May 2010
    Location
    Poland
    Posts
    4

    Re: Unhandled exception..

    Quote Originally Posted by Lindley View Post
    This is Managed C++, so you'll get more help in that forum. (Native C++ does not have a namespace "System", for one, nor does it use the ^ symbol in that way.)

    However, I do find it suspicious that a j-based for loop is incrementing i instead of j.
    I have tried to use * for pointer but I had problems with passing Random variable to function.
    This is how I found it working.

    i++ in j for loop it definitely my mistake.. but even after correction it does not solve the problem. Error still occurs on one of two lines I mentioned before.

  6. #6
    Join Date
    Sep 2010
    Posts
    3

    Re: Unhandled exception..

    Did you run in Debug-Mode?

    I didn't programm C++ for a few years now, but I believe the Debugger should jump to the line where the exception is thrown, shouldn't it?
    From there you should be able to inspect the contents of your variables and maybe use breakpoints to examine what goes wrong.

    I can only recommend you to learn the debugger.

  7. #7
    Join Date
    Sep 2010
    Posts
    3

    Re: Unhandled exception..

    Your destructor looks pretty strange.

    doesn't this work?

    Code:
    StellarObject::~StellarObject()
    {
    	for(int i = 0; i < this->NoOfSatelites; i++)
    	{
    		// i suppose this should call the destructor and thus there 
    		// is no need to walk through it's children and delete them
    		delete this->Satelite[i]; 
    	}
    	delete[] this->Satelite; 
    }

    Same goes for your PrintSystem() Method. It's pretty weird.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center