member deque with structs

Hi I have a problem using a class member deque <task *> m_queued_tasks that contains pointers to a struct. I try to insert new structs in the deque but when the program goes out of scope from where the new struct was created, the values in the deque gets messed up. I cannot figure out what is wrong. The class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class dispatcher
{
public:
    // ###################### Structs ######################
    struct task
    {
        char *          id;
        unsigned int    est_time;
        unsigned int    prio;
        long int        queue_time;

        // Constructor
        task();
    };
    
    // ###################### Functions ######################
    // Constructor
    dispatcher();
    
    // Destructor
    ~dispatcher();

    // Initialize
    int initialize(void);

    // Set run state
    void set_run_state(bool a_run);

    // Get run state
    bool get_run_state(void);

    // Read data from client
    int read_client(void);

    // Initialize client TCP connection
    int connect_client(const unsigned int a_port);

    int enqueue_task(task * a_task);

private:
    // Parse command read from client
    int parse_client_command(const vector<string>& a_command);

// ###################### Variables ######################
private:
    bool            m_run;
    int             m_version; // Version of this program
    net_com *       m_client;
    deque <task *>  m_queued_tasks;
    deque <task *>  m_finished_tasks;
    long int        m_start_time;
};


The place where new tasks are created ( parse_client(...) ):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
            char * l_id = new char[40];
            memset(l_id, 0, 40 * sizeof(char));
            l_id = (char*)a_command[1].c_str();
            //size_t l_len = a_command[1].copy(l_id, a_command[1].length(), 0);
            int l_est_time = atoi(a_command[2].c_str());
            int l_prio = atoi(a_command[3].c_str());
            if (l_est_time >= 1 && l_est_time <= 600 &&
                l_prio >= 0 && l_prio <= 1000)
            {
                printf("Enqueing new task with id %s, estimated time %d and prio %d\n",
                        l_id, l_est_time, l_prio);
                l_ok = true;
                // Generate new task
                task * l_task = new task;
                //task l_task;
                l_task->id = l_id;
                l_task->est_time = (unsigned int)l_est_time;
                l_task->prio = (unsigned int)l_prio;
                // Enqueue task
                int l_result = enqueue_task(l_task);
                if (0 != l_result)
                {
                    return 1;
                }
            }


Tell me if you need to see more code..
Thank you
Ok, I solved it myself..

The problem was that I allocated memory for the char (line 1) char * l_id = new char[40], but then didn't use the allocated memory. c_str() (line 3) returns a const char * which points to an internal location with the required storage space for this sequence of characters plus its terminating null-character, but the values in this array should not be modified in the program and are only granted to remain unchanged until the next call to a non-constant member function of the string object.
So, I instead copied this allocated memory to my allocated memory by replacing line 3 with memcpy(): memcpy(l_id, a_command[1].c_str(), DEFAULT_ID_LEN) and everything worked perfectly.
Topic archived. No new replies allowed.