Link to home
Start Free TrialLog in
Avatar of HPBSM
HPBSM

asked on

Deleting std::vector Array in C++

Hi,

I have the following below, is this code legal? can this code can cause to some heap corruption?
The main issue here if there is a double deletion of the vector objects.
The desctructor of class dd_sample_t checks if the members are not null, if there not null it deletes them and then puts a null in them.

I'll happy to provide any other data needed.

Thanks.

At the beggining:

{
      auto_ptr<CWtSampleCtor> sample_ctor(sample_constructor);

      // construct the samples
      vector<dd_sample_t *> samples_arr;

      sample_ctor->fillSamplesArr(samples_arr);

      //send the samples, do some actions....

      sample_ctor->freeSamplesArr(samples_arr);

}

The fuction fillSamplesArr(vector<dd_sample_t *> &samples_arr):

{
...
   
m_trc_general_sample.fill(...);

m_trc_general_sample.fill(...);

...

// The definition of m_trc_general_sample is:
// dd_sample_t      m_trc_general_sample;

samples_arr.push_back(&m_trc_general_sample);

}

The function freeSamplesArr(vector<dd_sample_t *> &samples_arr):
{
      // note - avoid deleting the last m_trc_general_sample sample

      for (size_t i = 0; i < samples_arr.size() -1; ++i)
      {
            if(samples_arr/[i/] != NULL)
            {
                  delete samples_arr/[i/];
                  samples_arr/[i/] = NULL;
            }
      }
}
Avatar of Infinity08
Infinity08
Flag of Belgium image

>> samples_arr.push_back(&m_trc_general_sample);

How is m_trc_general_sample defined ? This looks like the object has not been allocated using new, so you should not use delete to de-allocate it !!


Also : your freeSamplesArr function will try to delete all pointers in the vector, and then set them to NULL, but the size of the vector will still be the same. is that intentional ?
Avatar of HPBSM
HPBSM

ASKER

As I've written, m_trc_general_sample is defined as: dd_sample_t      m_trc_general_sample;

Indeed, it was not allocated by new but the details were created by create function (appears in definition.h attached)

Actually fillSamplesArr(vector<dd_sample_t *> &samples_arr) is defined as follows:


{

m_trc_general_sample.create(...); // defined in definition.h attached
...
   
m_trc_general_sample.fill(...); // defined in definition.h attached

m_trc_general_sample.fill(...); // defined in definition.h attached

...


samples_arr.push_back(&m_trc_general_sample); // std::vector function.

}

The class dd_sample_t definition is attached.

The fact is that from time to time this process hangs up and stops working, it is not consistent and can happen only after few days of running.

1. Can this code can cause to some heap corruption which can explain the facts?

2. Do you have any recommendation how to get close to the core of the problem? This process contians a huge amount of code and I've just suspected this area.

3. Is there any need to remove the code of freeSamplesArr?

Thanks a lot!
Avatar of HPBSM

ASKER

I'm attaching class dd_sample_t  definition.
definition.h
Avatar of HPBSM

ASKER

Regarding your remark:

"Also : your freeSamplesArr function will try to delete all pointers in the vector, and then set them to NULL, but the size of the vector will still be the same. is that intentional ?"

What are the implications of leaving the vector size the same?
SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> What are the implications of leaving the vector size the same?

That you haven't actually removed anything from the vector. Depending on how the vector is used in the rest of the code, this might or might not be an issue.
Avatar of HPBSM

ASKER

(b) dereferencing a dangling pointer, and using the object it's pointing to, is also likely to corrupt the heap.

1. Can you please explain what does it mean dereferencing  and a dangling pointer?

2. m_trc_general_sample is a class member and the vector is local in the function. Do you think that the life span of m_trc_general_sample is shorter than that of the vector?

3. In a debugger, the process was hang up as usual and no exception was thrown (all C++ exceptions were marked) I've breaked in Visual C++ 2005 and checked the call stacks of the threds but there was no connection to the vector. If the process is hang up and not crashes, how the debugger can help me?

Thanks
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HPBSM

ASKER

O.K. Thanks, I'll check it out and write again.
Let me know if you have further questions about this :)
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.