dynamic memory allocation via STL

Hi guys.  It's me again (the "dynamic memory allocation problem using STL" problem.  Well, here is my test program in its entirety.  Test this program and tell me what's wrong with it.
Note:  Keep on a sharp lookout for destructors being called before end of scope!!!

/* main.cpp */

#include <assert.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class CThing
{
   public:
      CThing(TCHAR* psz= "NULL");
     ~CThing();

      string m_sz;
      int*   m_pData;
};

CThing::CThing(TCHAR* psz):
m_sz(psz),
m_pData(NULL)
{
   cout << "Calling ctor\n";
   m_pData= new int[100];
   assert(m_pData != NULL);
}

CThing::~CThing()
{
   cout << "Calling dtor\n";
   if (m_pData)
   {
      delete [] m_pData;
      m_pData= NULL;
   }
}

//globals
vector<CThing> g_vecThings;

void main()
{
   g_vecTextures.push_back(CThing("blablabla"));
   g_vecTextures.push_back(CThing("jabadabado"));
   g_vecTextures.push_back(CThing("scooby doooooo"));
   g_vecTextures.push_back(CThing("more blablabla"));
   g_vecTextures.push_back(CThing("whazup"));

   //display some stats.
   cout << "g_vecTextures.size()= " << g_vecTextures.size() << 
   "\n";
   for (int i= 0; i < g_vecTextures.size(); i++)
   {
      cout << g_vecTextures[i].m_sz << "\n";
      cout << g_vecTextures[i].m_pData << "\n";
   }

   g_vecTextures.clear();
}
ipocinaAsked:
Who is Participating?
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.

KangaRooCommented:
First time I here you can not use new ad delete with STL. But some STL implementation may allocate your objects (and for instance nodes that contain them) from a 'central' pool and some containers could possible postpone calling destructors. This could save time. Does the leak persist after the container is 'out of scope'?
0
nietodCommented:
All the sTL containers have allocator tempaltes that default to a template that uses new/delete.  So if you don't specify an allocator object, it will end up using new and delete.

So this is not your problem.

Most likely it is something else in the class you've written (CThing).  For example, does Cthing have a working copy constructor?  a working operator =?  You will almost certainly have to write your own copies of these functions.  The default ones provided by the compiler are almost certainly wrong.
0
ipocinaAuthor Commented:
Edited text of question.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

ipocinaAuthor Commented:
Adjusted points to 100
0
nietodCommented:
First of all, please don't edit the question once a dialog has begin.  It makes it impossible for new participants to follow the history fot he discussion.

2nd of all.  Its exactly what I said.  You need to define a copy constructor and a operator =.

continues.
0
nietodCommented:
This is a classic mistake and I recommend you read the Scott Meyers books, "Effective C++" and "More Effective C++" as they will cover this and abotu 50 other common mistakes.  If you don't read the books, you are assured of making almost every single one of these mistakes.

The current problem is that when an object in your class is copied either using the copy constructor or using operator = , like in

CThing A("A");
CThing B(A);  // Error 1.
CThing C("C");

C = A; // Error 2.

You get an error.  Or I shoudl say, you lay the foundation for an error.  There error will not be detected until later.  The problem is that the code generated automatically by the compiler to handle the copying process will COPY THE POINTER, and will not COPY THE DATE THE POINTER POINTS TO.  For example, in

CThing B(A);  // Error 1.

Both object B and A will have m_pData members that contain the SAME POINTER VALUE.  That is both point to the same string.  At first that is okay.  But when B is destroyed, it will delete the memory that this points to.  Still okay.  However A still has a pointer to this memory and does not know it was deleted.  So when A is destroyed, it will also delete this memroy.  That causes problems since the memory has already been deleted.

Same in

C = A; // Error 2.

After operator = is done, the two objects, C and A, have m_pData members with the same pointer value.  When the first one is destroyed you are okay.  When the 2nd one is destroyed you are in trouble.  (The problem may manidest itself in other ways to.  But the root of the problem is that two objects share dynamically allocated memory that they were not designed to share.

Let me know if you have any questions.
0
nietodCommented:
I'm testing the new withdraw answer feature. If it works, I'll answer again.
0
nietodCommented:
It works!  I'm answering again.
0

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
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
Editors IDEs

From novice to tech pro — start learning today.