C++ Question Stuck again with pointers/variable 0xC0000005 infamous error

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
10,002
Reaction score
4,418
Points
203
Location
Toulouse
Hello, :hello:

As the title says, I'm experiencing the "0xC0000005 Access Violation at 0x0000000" error.

I fear I'm losing myself again into confusion between variables and pointers.

Help with this will considerably speed up the Taïga Lander developpement process. Thanks in advance. :tiphat:

Code:
// In the header file

char* Name;
...

// In the main.cpp file
...
// --------------------------------------------------------------------------
// METHOD FOR EVA
// --------------------------------------------------------------------------
void RLM::UmmuEvaCrewMember (void) 
{
    if (Crew.GetAirlockDoorState() == TRUE)
    {
        Name = Crew.GetCrewNameBySlotNumber(SelectedUmmuMember);
        Crew.EvaCrewMember(Name);
        if (Crew.EvaCrewMember(Name) == 2)
        {
            sprintf(SendHudMessage(), "Transfer successful");
        }

    } else {

        sprintf(SendHudMessage(), "Transfer hatch closed, unable to proceed");
    }
}
...


Note : Crew.GetCrewNameBySlotNumber returns a char * (pointer to a char value ?)
 
First of all, you call EvaCrewMember twice, that is wrong. Call it once and store the result in a variable: int ret = Crew.EvaCrewMember(Name);

Next, you should better test of Name is a valid pointer to a string and not NULL and that SelectedUmmuMember is also an index to an existing crew member. Remember: if you EVA, the number of crew members of your spacecraft drops. Make sure you change the selection to a valid index or a deselected state after an successful EVA.
 
After modifying the code according to your suggestion :

Code:
// --------------------------------------------------------------------------
// METHOD FOR EVA
// --------------------------------------------------------------------------
void RLM::UmmuEvaCrewMember (void) 
{
	if (Crew.GetAirlockDoorState() == TRUE)
	{
		Name2 = Crew.GetCrewNameBySlotNumber(SelectedUmmuMember);
		int ret = Crew.EvaCrewMember(Name2);
		if (ret == 2)
		{
			sprintf(SendHudMessage(), "Transfer successful");
		}

	} else {

		sprintf(SendHudMessage(), "Transfer hatch closed, unable to proceed");
	}
}

I get :

- SelectedUmmuMember = 0 (which is the "slot" of our EVAed cosmonaut)
- Name = "Maj A A Skvorstov" (which is the name of the cosmonaut)
- ret = -858993460 (*beep* Houston we have a problem *beep*)
 
It is 0xCCCCCCCC in hexadecimal. I wouldn't freak out there. I have a few minutes left before I depart to my parents for watching the champions league final with my family, I will check the API documentation for it.

EDIT: According to the API docs, this is an undefined return code... any linking problems or so?
 
Last edited:
Looks like an uninitialized variable in a debug build, doesn't it?
 
So there is probably a variable I declared in the header file, and that I forgot to initialize (assign a value for exemple in ClbkSetClassCaps) in the main.cpp (and that "contaminates" the memory adress I use when EVAing a crewmember) ?
 
When did you check the value of ret? On the line assigning value to it or on the following? If on the line, then it's still uninitialized. Put a breakpoint after that line. EvaCrewMember shouldn't return 0xcccccccc in any condition.
 
:blush: uh yeah I put it on the line... :blush:

Now the EVA transfer works, I can freely transfer cosmonauts back and forth between the Soyuz (with crex's UmmuFA) and my Taïga lander. Thanks, that helps greatly.
 
Checking that every member variable in a C++ class is initialized to some valid value, in every constructor, is critical in C++. It even doesn't matter very much what value that is.

What is most important to me is that the value not be random. (If you don't initialize it specifically, some compilers and build types will leave whatever value just happened to be in the memory in there.) Most people like their computer programs to provide the same answer each time they run the exact same program. To have this comfortable feature, you have to initialize all your member variables. Bugs caused by accessing random memory are some of the worst to track down, because they can be different on different runs on the same computer, or between runs that should be identical on different computers.

CppCheck is a cross platform open source tool that can be used to perform "static analysis" on C++ code: it analyzes the source code without running the program. It can detect uninitialized variables, and many other probable errors.
 
Additionally to that I'll add, that in the newest C++ standard, the C++11, you can initialize all variables in class' declaration. You don't need to initialize the variables in every constructor of one class separately. Helps, also because you may declare in the header a variable and forget to initialize it in the implementation file.
 
I didn't get the last two posts (remember, I never followed a course of programming), but thanks anyways :) :tiphat:
 
Additionally to that I'll add, that in the newest C++ standard, the C++11, you can initialize all variables in class' declaration. You don't need to initialize the variables in every constructor of one class separately. Helps, also because you may declare in the header a variable and forget to initialize it in the implementation file.

Ha, finally!

I wouldn't celebrate TOO quickly. They finalized that standard a year ago. It'll be at least one more before we have a compiler that meets most of it, and at least 3 more before we can use that compiler on Orbiter.
 
Back
Top