If you access the structure in the vector, it will fall off.

Asked 2 years ago, Updated 2 years ago, 66 views

Please help me.When I try to access the vector of the structure (elmCom) that I define, the program suddenly ends.The vector (named elcom and very confusing) is defined in the header file, then the constructor in ObdCapture.cpp has secured memory, and the init method pushes data from xml (approximately 800 lines) to elcom.After pushing_back, you can check the size by elcom.size(), but when you try to see the value of the structure inside the vector or try to access it using an iterator, it suddenly ends like that way.There was no specific error output.
If it's a segmentation error, it seems to come out normally.

The development environment is raspbian (raspberry pi).I apologize for the poor writing and code.
Thank you for your cooperation.

main.cpp

#include<stdio.h>
# include "ObdCapture.h"

int main() {

ObdCapture*oc=new ObdCapture();

oc->init();
oc->set_addr((char*) "00:00:00:08:A5:C6"); 

inta[] = {1,2,3,4,5};
if(oc->setMonitored(a,sizeof(a)/sizeof(a[0]))||oc->connectElm()){
    oc->setFile("log");
    if(oc->f_con_write()){
        std::cout<<"end"<<std::endl;
    }
}

// Memory release
deleteoc;
}

Obd_Capture.h

#ifndef_OBDCAP_H_
#define_OBDCAP_H_
# include <vector>
# include <string>
# include<sys/types.h>
# include<sys/socket.h>
# include<bluetooth/bluetooth.h>
# include<bluetooth/rfcomm.h>
# include<boot/property_tree/ptree.hpp>
# include <fstream>

typeedef structure elmCom {
            char*code;
            int num;
            int bit;
            int mode;
}elmCom;

classObdCapture
{
public:
// Constructor
    ObdCapture();
// Destructor
    ~ObdCapture();
    void init();
 // OBD code to monitor
    int setMonitored(inta[], intl_length);
// Obtaining List of OBD Codes Being Monitored
    std::vector<elmCom>getMonitored();
// Verifying Connectivity
    int connectElm();
    void set_addr(char*addr);
    std::string get_addr();
    std::string getFile();
    void setFile(std:string fname);
    // Read and write to file
    intf_con_write();

private:
    std::string fname;// filename
    time_tt;
    std::vector<elmCom>cap_register;
    std::vector<elmCom>elcom;//obdPID list (read from xml)   
    char can_buf [1025];
    sockaddr_rcaddr;
    char buf [1025]; // Receive Buffers
    ints, status, server;
    char dest [18];
    std::string path;// file path 
    std::ifstream fin;// input stream
    std::ofstream fout;// output stream
    std::string file_path;
    unsigned int flags;// flag indicating the correct order of processing
    boot::property_tree::ptreept;//xml tree
};

#endif//_OBDCAP_H_

Obd_Capture.cpp

#include<stdio.h>
# include <string.h>
# include <unistd.h>
# include<sys/socket.h>
# include<bluetooth/bluetooth.h>
# include<bluetooth/rfcomm.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <time.h>
# include<boot/foreach.hpp>
# include<boot/property_tree/xml_parser.hpp>
# include<boot/property_tree/ptree.hpp>
# include<boot/optional.hpp>
# include "ObdCapture.h"

using namespace std; 
using namespace boot::property_tree;

// FILE OPERATION AND BLUETOOTH COMMUNICATION
#define BIT(num)((unsigned int)1<<(num))

#define AVA_BLUETOOTH BIT(0)// Bluetooth connection established
#define AVA_FILE BIT(1)//Preparing Write Files
# Define AVA_OBD_CODE BIT(2) // ObD-II Retrieval Commands
#define AVA_CAP_OBD BIT(3)//OBD-II Capture
#define AVA_BT_ADDR BIT(4) // Prepare Acquisition Address
#define ERR_BLUE_NOT_READ BIT (5)


ObdCapture::ObdCapture():elcom(138), cap_register(138)
{   
    elmSwitch=0;
    addr=(sockaddr_rc){0};//sockaddr_rcaddr
    flags = 0;
    t = time (NULL);
    memset(buf, '\0', sizeof(buf)); // buf initialization
}

ObdCapture::~ObdCapture()
{
    cout<<"Destructor Call\n";
    if(fname.empty()){
        fout.close();
    }
    if(flags&AVA_BLUETOOTH){
         close(s);
    }
}

voidObdCapture::init(){
ptree ptt;
read_xml ("obd_config.xml", ptt); // Boost's xml library
std::string str;
int num;
int bit;
int mode;
inti = 0;
BOOST_FOREACH(constree::value_type&pct, ptt.get_child("pids")){                     
        str = pct.second.get<std::string>("code");
        num = pct.second.get<int>("num");
        bit = pct.second.get<int>("bit");
        mode = pct.second.get<int>("mode");
        char*code = new char [str.size()+1]; // Free memory
        std::strcpy(code,str.c_str());// copy
        elmComelc={code, num, bit, mode};
        cout<<"elcis"<<elc.code<<endl;
        elcom.push_back(elc);
        cout<elcom.size()<endl;
        cout<<i++<<endl;
// re-subs
}
if((flags&AVA_OBD_CODE)==0){
    flags + = AVA_OBD_CODE;
    cout<<"AVA_OBD_CODE"<<endl;
}
//cout<<elcom.at(0).code<<endl;<- This causes an error.  
}
/* Hereinafter abbreviated */

c++ linux pointer

2022-09-30 11:39

1 Answers

The constructor in the ObdCapture class says elcom(138), which means that the member variable elcom contains 138 elmCom objects from the beginning that already have an indefinite value before pushback. If you try to read this indefinite value of code as a string (i.e., cout<elcom.at(0).code<endl;), the indefinite value of code points to an invalid memory area, so the program crashes.
elcom(138) should be fixed by setting the elcom().

It's not directly related to the question, but let me give you some advice.
You seem to be able to write a reasonable amount of code, and you seem to have the ability to find out the libraries you need. I think it will be much easier to program than it is now if you take time to learn how to write code, techniques, etc. I recommend reading "Effective C++ and C++ Coding Standards.


2022-09-30 11:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.