Home > Net >  Data lost after assigning object to linked list and jumping between functions (C )
Data lost after assigning object to linked list and jumping between functions (C )

Time:11-22

I am trying to implement an application that will involve creating a custom object (containing pointer to another customer object) and organising them into a linked list.

I have created some functions to attempt to add them to linked list. Based on debugging using gdb, all values are passed to Event constructor and addEvent() function without problem, but once jumping back to the main test programme, at least some values (such as timestamp and value in EventValue) are turned into gibberish, and I have no idea where in the process they are lost.

This is the info I got from gdb - as can be seen the timestamp is changed into gibberish:

(Event &) @0x7fffffffd8a0: { type = "P\222yUUU\000\000P\222yUUU\000\000\001\000\000\000\000\000\000\000\062\000\000\000UU\000\000C", '\000' <repeats 79 times>, "\220\213xUUU", '\000' <repeats 13 time s>, "\004\000\000\000\000\300C\302\367\377\177\000\000\000\000\000\000\000\000\000\000\300C\302\367\377\177\000\000`\263xUUU\000\000p\333\377\377\377\177\000\000\000\265A\3 54\070\065\256\374\337\333\377\377\377\177\000\000\001\000\000\000\000\000\000\000"..., ev = 0x7fffffffd8f0, timestamp = 93824994611792}

I have tried things like building/removing copy constructors but nothing seems to have worked so far.

I have been working on this for days without a clue so any help on this is appreciated!

Snippets of the test programme below:

  SECTION("Adding an event to an instance works") {
    ...
    application.addEventToApplication(50, {"measurement", 12345});
    application.addEventToApplication(50, {"measurement", 123456, new EventValue{200}});
    ...
  }
template <t>
void addEventToApplication(long Id, Event&& e) {
  ...
  applicationInstance.addEvent(std::move(e));
  ...
}

A brief structure of the objects and functions as follows:

class EventValue{
    private:
        long value = 0;
    public:
        EventValue(long value);
        long getValue() const;
        void setValue(long value);
};

class Event{
    private:
        std::string type;
        EventValue* ev = nullptr;
        long timestamp;
    public:
        Event& operator=(Event const& other);
        Event(Event const& other);
        ~Event();
        Event(std::string type, long timestamp, EventValue* ev);
        Event(std::string type, long timestamp);
};

class EventNode{
    public:
        Event& data;
        EventNode* next = nullptr;
        EventNode(Event& e);
};
EventNode::EventNode(Event& e) : data(e){};

Event::Event(string type, long timestamp, EventValue* ev) : type(type), timestamp(timestamp), ev(ev){};

Event::Event(string type, long timestamp) : type(type), timestamp(timestamp){};

Event& Event::operator=(Event const& other){
    if (other.ev != nullptr){
        this->ev = new EventValue(*other.ev);
    } else {
        this->ev = nullptr;
    }
    this->timestamp = other.timestamp;
    this->type = other.type;
    return *this;
}

Event::Event(Event const& other){
    *this = other;
}

Event::~Event(){
    delete ev;
}

void Roast::addEvent(Event e){
    EventNode *tmpNode = new EventNode(e);
    if (eventList != nullptr){
        tmpNode->next = eventList;
    }
    eventList = tmpNode;
}

CodePudding user response:

In addEvent, your line EventNode *tmpNode = new EventNode(e); will store a reference to e in your new node. Since you pass e by value, you've made a copy and this copy will be destroyed at the end of the function, so the reference stored in the event node will be dangling.

You likely want to pass e by reference:

void Roast::addEvent(Event &e)

then you'll want to verify that whatever value is passed in will stay alive while the event exists.

  •  Tags:  
  • c
  • Related