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

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
9,295
Reaction score
3,265
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 ?)
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,656
Reaction score
2,376
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
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.
 

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
9,295
Reaction score
3,265
Points
203
Location
Toulouse
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*)
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,656
Reaction score
2,376
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
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:

orb

New member
News Reporter
Joined
Oct 30, 2009
Messages
14,020
Reaction score
4
Points
0
Looks like an uninitialized variable in a debug build, doesn't it?
 

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
9,295
Reaction score
3,265
Points
203
Location
Toulouse
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) ?
 

orb

New member
News Reporter
Joined
Oct 30, 2009
Messages
14,020
Reaction score
4
Points
0
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.
 

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
9,295
Reaction score
3,265
Points
203
Location
Toulouse
: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.
 

mjessick

Donator
Donator
Joined
Apr 26, 2008
Messages
174
Reaction score
0
Points
0
Location
Houston
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.
 

Enjo

Mostly harmless
Addon Developer
Tutorial Publisher
Donator
Joined
Nov 25, 2007
Messages
1,665
Reaction score
13
Points
38
Location
Germany
Website
www.enderspace.de
Preferred Pronouns
Can't you smell my T levels?
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.
 

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
9,295
Reaction score
3,265
Points
203
Location
Toulouse
I didn't get the last two posts (remember, I never followed a course of programming), but thanks anyways :) :tiphat:
 

lassombra

New member
Joined
Aug 12, 2010
Messages
26
Reaction score
0
Points
0
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.
 
Top