GameMonkey crashes

Discussion about everything. New games, 3d math, development tips...
Post Reply
cypher543
Posts: 78
Joined: Sat Apr 15, 2006 5:24 pm
Location: Missouri, USA
Contact:

GameMonkey crashes

Post by cypher543 »

Ok, I posted about this on the GM forums... but no one has posted there in a month. So, I'll ask here...

I'm using GameMonkey in my project for scripting. When I try to run the game, it gives me a "Has encountered a problem" error. Here's what my debugger spits out:
#0 0x00406b9c gmMachine::AddSourceCode
#1 0x00406fa8 gmHooks::Begin
#2 0x004101fc gmCodeGenPrivate::Lock
#3 0x00404659 gmMachine::ExecuteString
#4 0x0040132d main
Here's my header (without all of the Irrlicht functions):

Code: Select all

#include "gmThread.h"
class GameManager : public IEventReceiver
{
    public:
        gmMachine* getgmMachine();
        gmMachine* gm;
};
And the code for the getgmMachine() function:

Code: Select all

#include "gmThread.h"
gmMachine* GameManager::getgmMachine()
{
    return gm;
}
and the code to run a script in my main() function:

Code: Select all

#include "GameManager.h"

int main()
{
    GameManager game;

    game.getgmMachine()->ExecuteString( "print( \"Hello, world!\" );" );
}
What is going on? If I leave out the class stuff and just follow the tutorial on the GameMonkey forum, everything works great. It has to be something with my class... but I honestly don't see it. :(
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

From the code you posted, it doesn't look like anything initializes gmMachine* gm.

The GameManager has no user defined constructor, so the gm member is left uninitialized. After you instantiate the GameManager in main() you don't assign the game.gm member to point to anything, so it is still uninitialized. Then you call ExecuteString() on a bad pointer and it crashes.

The code you've written boils down to this

Code: Select all

#include "gmThread.h"
int main(void)
{
  gmMachine* gm;

  // calling a function on an bad pointer.. bad!!!
  gm->ExecuteString( "print( \"Hello, world!\" );" );

  return 0;
}
cypher543
Posts: 78
Joined: Sat Apr 15, 2006 5:24 pm
Location: Missouri, USA
Contact:

Post by cypher543 »

Thanks. I think I understand what the problem is now. I just don't know how to fix it. I've only been using C++ for about 3 days and the only reason I've gotten this far is becuase I had someone on MSN help me through fixing errors. But, he's offline atm.

I added a constructor:

Code: Select all

GameManager::GameManager()
{
        gmMachine* gm;
}
But then I get this error in the getgmMachine() function:
error: `gm' undeclared (first use this function)
I must have misunderstood what I was supposed to do. Sorry. :(
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

The best thing that I can tell you to do at this point is to go find a C/C++ tutorial. Providing assistance to you at this point would only make you dependent on it.

Travis
cypher543
Posts: 78
Joined: Sat Apr 15, 2006 5:24 pm
Location: Missouri, USA
Contact:

Post by cypher543 »

I got it working... but FYI, I don't learn well with tutorials. I should know, becuase I've been trying to follow C++ tutorials for almost 2 years now. I learned more from talking to someone on MSN in 2 days than I ever learned from reading tutorials for 2 years. So, before you just go a refuse to help, maybe you should think about how some people may actually try to learn, but can't do it using tradition methods.
Maize
Posts: 163
Joined: Sat Oct 29, 2005 12:12 am
Location: In a cave...
Contact:

Post by Maize »

The best way to learn is to keep trying different things to see what works and what doesn't. Never give up on anything...no matter what.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

I got it working...
Then you proved my point perfectly. You are smart enough to figure out how to solve the problem on your own. My helping you out would only have given you the answer and you would not have learned anything.

Travis
Post Reply