jeudi 21 mars 2019

Odd behavior after adding exceptions

I added some exception handling into some functions in my Project. Before adding the error handling the program was working normally. After adding the exceptions I now get garbage values in the constructor and mutator parameters and I'm not sure where its coming from. Some of the functions are called in no other location than the constructor, so I have no idea where they are getting the garbage data from.

I also checked for memory leaks with valgrind and some debugging with gdb, but didn't find anything. I'm at my wits end. I have attached a sample of the constructor cpp below and one of the mutators with the exception handling.

Protofield.cpp

ProtoField::ProtoField(std::string t_name, std::string t_abbreviation, FieldType t_type, Base t_base, int t_mask, std::string t_description, int t_offset, int t_length){
    try{
        setName(t_name);
        setAbbreviation(t_abbreviation);
        setType(t_type);
        setBase(t_base);
        setMask(t_mask);
        setDescription(t_description);
        setOffset(t_offset);
        setLength(t_length);
        m_valueString = std::map<int, std::string>();
    }
    catch(std::runtime_error e){
        std::cerr<<"Error in ProtoField Constructor"<<std::endl;
        std::cerr<<e.what()<<std::endl;
        return;
    }
}
/*Removed for brevity*/
void ProtoField::setMask(int t_mask){
    if(mask != 0){
        std::stringstream ss;
        ss<<"Field " << m_abbreviation << " mask previously set: "<<m_mask;
        throw std::runtime_error(ss.str());
    }
    else{
        m_mask = t_mask;
    }
    return;
}
/*Removed for brevity*/

ProtoField.hpp

class ProtoField{
    private:
        std::string m_name;
        std::string m_abbreviation;
        FieldType m_type;
        Base m_base;
        int m_mask;
        std::string m_description;
        int m_offset;
        int m_length;
        std::map<int, std::string> m_valueString;
    public:
        ProtoField(
            std::string t_name = "",
            std::string t_abbreviation = "",
            FieldType t_type = FieldType::ft_invalid,
            Base t_base = Base::invalid,
            int t_mask = 0,
            std::string t_description = "", 
            int t_offset = -1,
            int t_length = -1
        );

        std::string getName();
        std::string getAbbreviation();
        FieldType getType();
        Base getBase();
        int getMask();
        std::string getDescription();
        int getOffset();
        int getLength();
        std::map<int, std::string> getValueString();

        void setName(std::string t_name);
        void setAbbreviation(std::string t_abbreviation);
        void setType(FieldType t_type);
        void setType(std::string t_typestr);
        void setBase(Base t_base);
        void setBase(std::string t_basestr);
        void setMask(int t_mask);
        void setDescription(std::string t_description);
        void setOffset(int t_offset);
        void setLength(int t_length);
        void addValueString(int t_key, std::string t_value);
        void removeValueString(int t_key);

        //other functions
        std::string to_string();
    };

I feel as though its also worth mentioning, only integers appear to be affected. The other values including strings and enums seem to remain consistent with their previous behaviors. So in the class shown, only the mask, offset, and length show odd behaviors.

Aucun commentaire:

Enregistrer un commentaire