memory/pointer question

I'm trying to do some beginner learning here and just wanted to see if someone can mentor me on this matter. Are the two "delete"s in here needed?  And why or why not? And as a side if there's anything obviously wrong with my code please let me know.

Thanks!


typedef struct {
      uint8                  m_nodeId;
      list<OValue>      m_values;
} NodeInfo;
static list<NodeInfo*> g_nodes;

typedef struct {
      int                        time;
      OValue*                  m_value;
} ValueChange;
static list<ValueChange*> g_changes;


// deletes one of the elements from the array of ValueChange's if the OValue is in that element
void function DeleteChange(OValue v){

    for (list<ValueChange*>::iterator it = g_changes.begin(); it != g_changes.end(); ++it)
    {
        ValueChange* change = *it;
        OValue* v_i = change->m_value;

        if (v_i->GetId() == v.GetId())
        {
            g_changes.erase(it);
            delete v_i;
            delete change;
        }
    }

}
LVL 2
rustycpAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Yes, the two 'delete' operations are absolutely necessary, since you would create memory leaks if you omitted them. You could elimiate the last of the two by providing a destructor to 'ValueChange' that takes care of that, e.g.

typedef struct {
      ~ValueChange() { if (m_value) delete m_value;}
      int                        time;
      OValue*                  m_value;
} ValueChange;

Open in new window


That's the downside of using STL containers with pointers. If possible, try to avoid such situations. But, sometimes that simply is not possible...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
phoffric\Commented:
>> if there's anything obviously wrong with my code please let me know.
Without seeing a small buildable program, it's hard to say whether anything is obviously wrong. For example, I don't know how g_nodes is used. And I don't know how your constructors and destructors are defined.

Another downside of using pointers (whether in STL containers or not) is that it is possible that the pointers are not pointing to objects that were dynamically allocated with the new operator; in that case, using delete would be wrong. Also, a pointer may be pointing to objects created with the new[] operator, in which case, you would need to use the delete[] operator.
phoffric\Commented:
Another concern, in general, is whether you are deleting objects that another object is still referencing.
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

rustycpAuthor Commented:
So here's what I'm not quite getting. I thought that since I declared those two pointer variables inside this function, that they only exist inside the function and would ni longer exist when the function exits. For that reason I don't understand why the delete is needed. I realize the OValue object that the ValueChange object points to will still exist because it is still pointed to inside one of the NodeInfos in the NodeInfo list, and thats ok because I want to keep it there. But I do want to remove that one ValueChange object from the ValueChange list and not have it taking up memory anymore. In other words since I removed the only reference to the ValueChange obj by using erase(it) from the ValueChange list, plus when the function exits and the local "change" pointer goes out of scope, then the ValueChange object should be able to be  "garbage collected"?
rustycpAuthor Commented:
To phoffrics last point - I do NOT want to delete the OValue object that the ValueChange object it pointing to. I use that elsewhere. So I should not do "delete v_i"? Even though v_i is a pointer and not the actual object?
phoffric\Commented:
Both g_nodes and NodeInfo are not used in your OP. If you post a short buildable program (and preferably one that gives correct results), then it will be easier to address your comments.
rustycpAuthor Commented:
g_nodes is a list of 10 NodeInfo. Inside each NodeInfo is a list of 10 OValue. So there are 100 OValue. This never changes.

Occasionally one of the 100 OValues changes, and a handler function creates a new ValueChange object with a pointer to the changed OValue in it as described and adds that to the ValueChange list. The program later comes back and removes that ValueChange from the list by calling this function, giving only the OValue object that has changed.

Hopefully that describes that there will always be the same 100 OValues objects, but a changing number of ValueChange objects. In that function I have a call to delete both pointers to a OValue and a ValueChange. I guess in particular I'm not clear why the call to delete the ValeChange pointer is needed? Or is it implicit when the function ends? Or will I create a memory leak without it?

I'll also try to write this into a basic program tomorrow.
phoffric\Commented:
There may be some missing details, but roughly speaking, if you have a new Ovalue for a key, ID, I am not sure why you are not replacing it in the list rather than doing any deletion. Your OP can be interpreted in different ways, and it is focused primarily on deletion rather than replacement.

If m_value is actually pointing to an OValue which is in the m_values list of Ovalue, then since you say that you always have a list of 10 Ovalue entries in each NodeInfo struct, then deleting it is not right. You could delete it and then insert the new Ovalue entry back into the list; but replacement seems like an easier option. (And I am assuming that this is single-threaded.)

Your comment reads
>> deletes one of the elements from the array of ValueChange's ...
yet your structures are lists. I am sure that this will be cleared up when you write a very small program to illustrate your points.
phoffric\Commented:
Your current description of your question seems to me to be very different from your OP description. I suggest that you close this question, and open a new one with your first attempt at solving the problem.

If possible, in the new question, please include a link or a verbatim description of the exercise that you are trying to solve to avoid any possible misinterpretations on our part.

(BTW, on weekends, I sometimes have time for a program type question; during the week, other experts will be able to help you at greater length.)
sarabandeCommented:
since I removed the only reference to the ValueChange obj by using erase(it) from the ValueChange list, plus when the function exits and the local "change" pointer goes out of scope, then the ValueChange object should be able to be  "garbage collected"?
c and c++ don't have garbage collection beside you would create smart pointers which are objects managing a pointer.

so, generally if you create a pointer with new (on the heap), you have to delete it. otherwise there is a memory leak (see comment of jkr).

g_changes.erase(it);
in your erase loop is a bug. after erasing the iterator it is no longer valid and because you didn't break from loop, the ++it at begin of next loop cycle would operate with an invalid iterator. if using a vector, only the next item after erasing was skipped. but if you erase the last item, the ++it statement will crash. for a list which chains items by pointer, it most likely will crash immediately at ++it. to come out of it, you may do:

list<ValueChange*>::iterator itnext; 
 for (list<ValueChange*>::iterator it = g_changes.begin(); it != g_changes.end(); it = itnext)
     {
         ValueChange* change = *it;
         OValue* v_i = change->m_value;

         if (v_i->GetId() == v.GetId())
         {
             itnext = g_changes.erase(it);  // erase returns a reference to the next iterator
             delete v_i;
             delete change;
         }
         else 
         {
              itnext = ++it;  // increment
         }    
     }

Open in new window


generally, you should only use pointers in collections, if the pointers are baseclass pointers for virtual use, or if you have more than one collection using the same objects, for example a vector for indexed access and a map for access by key.

if you do so, one of the collections must made responsible for deletion, while the other collection(s) may not delete theirselves. you would have all collections using the same set of pointers as members in one class and use the destructor to safely delete the pointers:

class X
{
     std::vector<Data*> v;
     std::map<std::string, Data*> m;
     std::list<Data*> l;
public:
     CreateData(int i, std::string k) 
     {
            v.push_back(new Data(i, k);
            m[k] = v.back();
            l.push_back(v.back());
       } 
     ~X()
     {
          for (size_t n = 0; n < v.size(); ++n)
               delete v[n];
          // just for readability ...
          v.clear();
          m.clear();
          l.clear();
      }
};

Open in new window


Sara
rustycpAuthor Commented:
Thanks man, this pointed me in the direction I needed to understand what the problems are that I'd had to deal with
phoffric\Commented:
You awarded all the points to my post http:#a40389351 and since that was not an answer, it was unaccepted. Since you have expressed satisfaction, it is time for you to accept one or more posts that have helped you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.