-1

My definition of the struct are

#include <vector>
#include <Eigen/Dense>
#include "lanenet_cfg.h"
#include "mytimer.h"
#include <opencv2/opencv.hpp>   

#define N_MAX_CAMERA_LANE         (4U)
#define N_MAX_TIMING              (50U)

namespace lanenet{
enum class LaneColorType {
    WHITE_DASHED = 0,
    WHITE_SOLID,
    YELLOW_DASHED,
    YELLOW_SOLID
};

typedef struct {
double C0;
double C1;
double C2;
double C3;                                
}lanePoly_t;


typedef enum {
    THIRD_LEFT    = -3,
    ADJACENT_LEFT = -2,              
    EGO_LEFT    = -1,                
    EGO_CENTER = 0,                  
    EGO_RIGHT = 1,                   
    ADJACENT_RIGHT = 2,              
    THIRD_RIGHT = 3,
    OTHER = 6,                       
    UNKNOWN = 7                      
}LanePos_t;

typedef enum {
    LANE_REAL     = 0,
    LANE_VIRTUAL
} laneUsed_t;

typedef struct {
    bool isValid;                        
    lanePoly_t lanePoly[4];
    int num_poly_used;                   
    float prob;                          
    LanePos_t lanePos;                  
    laneUsed_t laneUsedType;             
    int laneSrc;                         
    int quality;                         
} lane_t;

class timeMea {

public:
    int i;
public:
    MyTimer  mt_detection[N_MAX_TIMING];
    MyTimer  mt_preprocess[N_MAX_TIMING];
};


typedef struct {
    lane_t  lane[N_MAX_CAMERA_LANE];        
    int  iCurrLane;                         
    double timestamp;                      
    std::vector<std::vector<Eigen::Matrix<float, 2, 1>>> coords_fit;
    std::vector<std::vector<Eigen::Matrix<float, 2, 1>>> coords_avg;  

    timeMea  time_mea;
    std::string imageFullPath;               
    cv::Mat  img_source;                     
    bool  is_video;                          
} laneList_t;

}// endof lanenet namespace 

lanenet::laneList_t  glaneList;
int main(){
    memset(&glaneList,0, sizeof(laneList_t));
    glaneList.is_video = false;
    glaneList.imageFullPath ="";
}

My environments are Ubuntu 20.04 +vscode,use C++14, and compiler is GCC9.4.0_X86_64-linux-gnu, segmentation fault happened on line

laneList.imageFullPath ="";

after memset() function. in gdb, it states

Reading symbols from laneDet...
[New LWP 7742]
[New LWP 7746]
[New LWP 7749]
[New LWP 7745]
[New LWP 7750]
[New LWP 7743]
[New LWP 7744]
[New LWP 7753]
[New LWP 7752]
[New LWP 7748]
[New LWP 7747]
[New LWP 7757]
[New LWP 7751]
[New LWP 7756]
[New LWP 7758]
[New LWP 7755]
[New LWP 7754]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `/home/xxx/laneNetCPP_ubuntu_trt/build/laneDe'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  __memmove_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:314
314 ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S: No such file or directory.
[Current thread is 1 (Thread 0x7fec1ba28000 (LWP 7742))]
(gdb)

gdb debug information prompts alignment problem, and if I remove the memset() statement in main()

memset(&glaneList,0, sizeof(laneList_t));

there is no segmentation fault, so I guess The promble seems caused by alignment related and memset() related

P.S., for a long time, this code runs well, recently when I rebuild with C++11, it prompts segmentation fault, sometime it runs successfully.

Any help will be appreciated.

hitbuyi
  • 11
  • 4
  • Minor note: there is no need to typedef your struct `lanePoly_t` in C++. – Chris Aug 11 '22 at 04:35
  • 2
    `#include int main {static_assert(std::is_trivially_copyable(laneList_t), "No good");}` -- Your application will fail to compile, as it should. You can only use `memset` on trivially-copyable types. – PaulMcKenzie Aug 11 '22 at 04:40
  • 1
    Also, it seems you are using `C` as a guide in writing C++ programs. Don't do this -- C and C++ are two different languages. -- *P.S., for a long time, this code runs well* -- Well, now those "good times" are now over. The code was broken all of this time, and the newer version of the compiler detected it. – PaulMcKenzie Aug 11 '22 at 04:42
  • sometime it compiles and runs successfully, this really puzzles me – hitbuyi Aug 11 '22 at 04:43
  • 1
    @hitbuyi -- Undefined behavior. Expect anything to happen when you make mistakes like this. Let's say you bought a rope that can hold 500 pounds, and then you tie 600 pounds to it. The rope doesn't break. You buy another rope from the same manufacturer that holds 500 pounds, you tie 600 pounds to it, and it breaks immediately. Who is at fault, you "breaking the rules" of tying 600 pounds to it, or the rope? Same thing here -- you broke the rules, and then you can expect anything to happen, including "work correctly", failing immediately, working one day and failing the next. – PaulMcKenzie Aug 11 '22 at 04:43
  • @hitbuyi I really think you need to learn some C++ first, the code you try to write is really more like "C" and those are (for a long time now) two different languages. Go over this material and then come back to this issue https://www.learncpp.com/ – Pepijn Kramer Aug 11 '22 at 04:48
  • All the guides are helpful – hitbuyi Aug 11 '22 at 05:54

1 Answers1

2

You cannot memset a non-trivial type (e.g., std::string and std::vector). The C++ way to deal with this is to write a constructor for laneList_t and initialize using that.

Stephen Newell
  • 7,330
  • 1
  • 24
  • 28