projectchrono/chrono

Segmentation fault (Address not mapped to object [0x8])

yankaizhang20 opened this issue · 1 comments

My Idea:
I want to initialize the components of vehicle module, such as Terrain、Driver and WheelVehicle in my class.For example:

//in SimulatorWithChrono.h
class SimulatorWithChrono{
public:
    void init(){
        //step1. init system
        system_ = chrono_types::make_shared<chrono::ChSystemNSC>();
        system_->Set_G_acc(chrono::ChVector<>(0, 0, -9.81));
        //step2. init vehicle and driver
        for (/*all vehicle*/) {
            //initialize vehicle
            auto my_sedan = chrono_types::make_shared<Sedan>(system_.get());
            /*
            some code for my_sedan initialization
            */
            cars_.push_back(my_sedan);
            //initialize driver
            /*
            some code for path initialization
            */
            auto driver = chrono_types::make_shared<chrono::vehicle::ChPathFollowerDriver>(my_sedan->GetVehicle(), path,
                                                                                        lane_id, target_speed);
            /*
            some code for driver initialization
            */
            drivers_.push_back(driver);
        }
        //step3. init terrain
        terrain_ = chrono_types::make_shared<chrono::vehicle::RigidTerrain>(system_.get());
        /*
        some code init terrain
        */
    }
    void synchronize(double time) {
        for (size_t i = 0; i < drivers_.size(); i++) {
            auto driver_inputs = drivers_[i]->GetInputs();
            drivers_[i]->Synchronize(time);
            cars_[i]->Synchronize(time, driver_inputs, *terrain_);
        }
        terrain_->Synchronize(time);
    }

    void advance(double step_size) {
        terrain_->Advance(step_size);
        for (auto driver : drivers_) {
            driver->Advance(step_size);
        };
        for (auto car : cars_) {
            car->Advance(step_size);
        }
    }
    std::shared_ptr<Sedan> ego(){
        return cars_[0];
    }
private:
    std::shared_ptr<chrono::ChSystemNSC> system_;
    std::shared_ptr<chrono::vehicle::RigidTerrain> terrain_;
    std::vector<std::shared_ptr<Sedan>> cars_;
    std::vector<std::shared_ptr<chrono::vehicle::ChPathFollowerDriver>> drivers_;
    /*
        something else;
    */
}

and it looks like in my main:

int main(int argc, char* argv[]) {
    SimulatorWithChrono sim;
    sim.init();

    auto vis = chrono_types::make_shared<ChWheeledVehicleVisualSystemIrrlicht>();
    vis->SetWindowTitle("Sedan Demo");
    vis->SetChaseCamera(trackPoint, 6.0, 0.5);
    vis->Initialize();
    vis->AddLightDirectional();
    vis->AddSkyBox();
    vis->AddLogo();
    vis->AttachVehicle(&sim.ego()->GetVehicle());

    // Simulation loop
    int render_steps = (int)std::ceil(render_step_size / step_size);
    int step_number  = 0;
    int render_frame = 0;
    utils::ChRunningAverage RTF_filter(50);

    while (vis->Run()) {
        double time = sim.System()->GetChTime();
        if (time >= t_end)
            break;
        if (step_number % render_steps == 0) {
            vis->BeginScene();
            vis->Render();
            vis->EndScene();

            render_frame++;
        }


        DriverInputs driver_inputs_ego;

        sim.synchronize(time);
        vis->Synchronize("ego", driver_inputs_ego);

        sim.advance(step_size);
        vis->Advance(step_size);
        sim.System()->DoStepDynamics(step_size);

        // Increment frame number
        step_number++;
    }

    return 0;
}

Problem: Segmentation fault (Address not mapped to object [0x8])

and the stack trace is here:

Stack trace (most recent call last):
#4    Object "[0xffffffffffffffff]", at 0xffffffffffffffff, in 
#3    Object "./src/simulator/SimulatorWithChronoTest", at 0x55af25505d2d, in _start
#2    Object "/lib/x86_64-linux-gnu/libc.so.6", at 0x7f0193ba5082, in __libc_start_main
#1    Object "./src/simulator/SimulatorWithChronoTest", at 0x55af2550619b, in main
#0    Object "./src/simulator/SimulatorWithChronoTest", at 0x55af2550ad50, in chrono::vehicle::sedan::Sedan::GetVehicle() const
Segmentation fault (Address not mapped to object [0x8])

You will need to build the code in Debug and run your code through a debugger to find out where exactly it is crashing. Then work backwards from there. The code you provide is incomplete anyway, as the problem appears to be related to an incorrect initialization of the vehicle. Stepping through the code with a debugger should point where the problem is.