?
Solved

Why do I get uninilize memory when deleting and cleaning up my vector queue in my desctructor?

Posted on 2006-05-01
11
Medium Priority
?
244 Views
Last Modified: 2010-04-01
     vector <CUserContextData> userDataObjects;
      vector <wstring> primaryGroups;      

//Erase all items in the group vector queueu and the user data object queue
            if(!primaryGroups.empty())
                  primaryGroups.erase(primaryGroups.begin(), primaryGroups.end());
            if(!userDataObjects.empty())
                  userDataObjects.erase(userDataObjects.begin(), userDataObjects.end());
Thanks.
0
Comment
Question by:lapucca
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 1500 total points
ID: 16579278
Hi lapucca,
> //Erase all items in the group vector queueu and the user data object queue
>           if(!primaryGroups.empty())
>                primaryGroups.erase(primaryGroups.begin(<wbr/>),
> primaryGroups.end());
>           if(!userDataObjects.empty())
>                userDataObjects.erase(userDataObjects.be<wbr/>gin(),
> userDataObjects.end());

The above code is not required.  The vector destructor will do the cleanup automatically, so you can delete the above code entirely from your destructor.

David Maisonave (Axter)
Cheers!
0
 
LVL 30

Expert Comment

by:Axter
ID: 16579305
FYI:
More then likely, there's a problem with CUserContextData destructor.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16579845
Am I right in guessing that you are using vector<wstring> primaryGroups to manage the wstrings to which you have pointers in your ComboBox DataItemPtrs, alluded to in your other questions, lapucca? If so, please can you show how you create those wstrings and put their addresses into your Combobox?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:lapucca
ID: 16580098
Hi Rstaveley, you're right.  I now have 2 version of codes.  I started out jusing using the vector to store the wstring but I'm getting Uninilize Memory(Rational Purify) everytime I push_back into that queueu and my application also randomly crashes.

I then start another version attempting to save the wstring in the comboBox SetItemData but had lots of problem setting up the pointers then casting back the pointers.  The problem with the vector is posted here already.  Please have a look at it if you would.  Thanks.  I'll happy to get any of these 2 version working.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16580140
>>I started out jusing using the vector to store the wstring but I'm getting Uninilize Memory(Rational Purify) everytime I push_back into that queueu and my
>>application also randomly crashes.

Rational Purify could be giving you a false error.
When you're code crahses, what does your stack look like.
You should check out your stack, and follow it back to your code, to see what exactly is causing the problem.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16580167
lapucca,

If you want your problem resolve, please post replies to comments posted by other experts.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16580279
Let's see the calling code where you push your strings into primaryGroups.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 16580358
See my comment in http:/Q_21833013.html 
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 16584634
>>>>    vector <CUserContextData> userDataObjects;
>>>>     vector <wstring> primaryGroups;    

You can make these container empty more easily by

    userDataObjects.resize(0);
    primaryGroups.resize(0);

However, that doesn't solve the problem you have with Purify for two reasons. First, "uninitialized memory" means that you are using variables as *input* argument or at the right side of an assignment, e. g.

(A)
   int i;
   int j = i;   // assignment of uninitialized memory

(B)
   int i;
   anyfunc(i);  // passing uninitialized memory to a function

(C)
   class MyClass
   {
        int i;
        wstring ws;
   public:
        MyClass() {}    // i doesn't get initialized
                              // ws gets initialized cause there is constructor wstring::wstring
   };

   MyClass mc;
   vector<MyClass> v;
   v.push_back(mc);    // push_back gets uninitialized memory from object mc


You problem most likely came from a case like (C). You defined LPWSTR instead of wstring and did *not* initialize these pointers to NULL in the constructor or *don't* supply a (default) constructor. Thus part(s) of the class were not initialized properly.

If you are using an a class member, the problem may occur in the class member as well

  class MyClass
   {
        int i;
        wstring ws;
   public:
        MyClass() {}    // i doesn't get initialized
                              // ws gets initialized cause there is constructor wstring::wstring
   };

   class Superior
   {
        MyClass mc;
   public:
       
   };
   
   Superior sup;
   vector<Superior> v;
   v.push_back(sup);    // push_back gets uninitialized memory from object sup.mc
   
Regards, Alex
0
 

Author Comment

by:lapucca
ID: 16593058
My apology to everyone for not getting to this site the past day or so.  My ass has been set on fire by my manager to get this done.  I've gotten rid of the 2nd vector to store the value.  I've gotten rid of trying to use the comboBox LPARAM to store my value as well.  I came up with the 3rd version of the code which doesn't require heap memory or vector or LPARAM.  I just got it working today.  

1.  I got rid of the delete code in my destructor as AXTER suggested and Rational Purify didn't complain about any memory leak that way so it works.  

2.  To rstaveley and AXTER, I still have the same problem with my CUserContextData  vector when I call for CUserContextData .Push_Back() function.  I'm still getting memory uninitilize when I run Rational Purify and it's making me very uncomfortable.  

3.  I will do more testing tomorrow to see if my application will crash to decide if Purify is putting out a false error message.  

4.  To rstaveley , I will post my code tomorrow when I get in the office.

5.  Thanks to all the expert for helping me so far.
0
 

Author Comment

by:lapucca
ID: 16601428
Ok, I think I will close this quesiton based on the fact I was asking about deleting and cleaning up the queue.  I think Axter has answered this first so I will accept that for this question.

The memory Uninitilize is still a problem and I have opened up a separate quesiton for it, please see and comment on Q_21837305.html.  

Alex, Thank you for the extensive input and example.  I will checkit out and see that applies to my problem.  Thank you to all experts's help.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question