C++ SIGSEGV problem

I'm using Netbeans for my C++ project. The code crashed at the beginning of a function call. Trying to understand the cause of the crash, I did a 'Debug Core File' with gdb; the below is the message I came across. I'm trying to interpret what this means -

~"Program terminated with signal 11, Segmentation fault.\n"
~"[New process 10325]\n"
~"#0 0x0000000000ee00cc in CARRotorAerodynamics::Trim_Innerloop (this="
&"Cannot access memory at address 0x7fff71a70868\n"
~")\n"
~" at ../src/models/CARRotorAerodynamics.cpp:994\n"
~"994\tvoid CARRotorAerodynamics::Trim_Innerloop(CARHelicopter Helic)\n"


If I understand it correctly, the 'this' pointer of the instance of CARRotorAerodynamics is pointing to a faulty address 0x7fff71a70868. Is this so ?

The code I have is very long. A summary of the code is as below.

CARHelicopter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
class CARHelicopter : public CARBase
{
public:
CARHelicopter();
virtual ~CARHelicopter();

CARRotorAerodynamics Rotor;
:
:
void Trim_HubLoads();
:
:
}


CARHelicopter.cpp
1
2
3
4
5
6
7
8
#include "CARHelicopter.h"
void CARHelicopter::Trim_HubLoads()
{
:
:
Rotor.Trim_Innerloop(*this);

}


CARRotorAerodynamics.h
1
2
3
4
5
6
7
class CARHelicopter
class CARRotorAerodynamics : public CARBase
{
:
:
void Trim_Innerloop(CARHelicopter);
}

CARRotorAerodynamics.cpp
1
2
3
4
5
6
7
8
9
#include "CARRotorAerodynamics.h"
#include "CARHelicopter.h"
:
:
void CARRotorAerodynamics::Trim_Innerloop(CARHelicopter Helic) // PROGRAM CRASHES AT THIS LINE
{

}


CARMain.cpp
1
2
3
4
5
6
7
8
void main()
{
CARHelicopter Helicopter;
:
:
Helicopter.Trim_Hubloads();
:
}
Last edited on
The problem of Trim_Innerloop() is that it makes a copy of the passed object. If you have pointer within the passed object those pointers a copied and might be deleted.

This

void Trim_Innerloop(const CARHelicopter &Helic) // Note: const and &

will solve the problem (if the problem is the copy)
Thankyou for the response coder777,

but if I were to define the function as

void Trim_Innerloop(const CARHelicopter &Helic)

how do I pass the 'this' pointer to the function from CARHelicopter::Trim_HubLoads() ?
(The 'this' pointer is of the type CARHelicopter * const)
how do I pass the 'this' pointer to the function from CARHelicopter::Trim_HubLoads() ?
There's no difference. You can pass it just as before

Tried as you suggest, but still getting the error.

The problem of Trim_Innerloop() is that it makes a copy of the passed object. If you have pointer within the passed object those pointers a copied and might be deleted.


Under what circumstances might the pointers of the passed object be deleted ?
Under what circumstances might the pointers of the passed object be deleted ?
When the class contains pointer which are deleted in the destructor.

In this case
1
2
3
4
void CARRotorAerodynamics::Trim_Innerloop(CARHelicopter Helic) // PROGRAM CRASHES AT THIS LINE
{

}

the destructor of Helic is called and it might delete the objects the original object passed is still pointing to.

But that's only a guess. You didn't provide enough code to determine the true reason.
I don't have any statements within the destructor.
For testing purpose, I modified the code a bit so that I didn't have to pass any object to the Trim_Innerloop function. Even in this case, the code crashed at the same line number. Which, perhaps, means that there is something wrong within the 'Rotor' object itself ?

The files are large, so I'm unable to post it here. Maybe later, I can send the files privately to you ? Thankyou for taking the time to look at the problem; really appreciate it.
The problem of Trim_Innerloop() is that it makes a copy of the passed object. If you have pointer within the passed object those pointers a copied and might be deleted.

This

void Trim_Innerloop(const CARHelicopter &Helic) // Note: const and &

will solve the problem (if the problem is the copy)


Thanks a lot, coder777. Your suggestion above worked. Apart from the function above, I have a lot of other functions where I have to pass objects as arguments. For eg.

void CARBlade::Response(CARHelicopter Heli, CARRotorAerodynamics Rotor);

I modified ALL these function prototypes in every class as you suggested

void CARBlade::Response(const CARHelicopter &Heli, const CARRotorAerodynamics &Rotor);

I also explicitly added a copy constructor to every class I defined..

And now the code works!

Thankyou
see? You learn the most when you solve it yourself. const reference definitely raise the safety level

But for the learn effect it may be good to figure out what's the problem really is.
Best: Debugging. Second best: Debug output
Topic archived. No new replies allowed.