?
Solved

dynamic memory allocation via STL

Posted on 2000-01-13
8
Medium Priority
?
380 Views
Last Modified: 2013-12-14
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();
}
0
Comment
Question by:ipocina
  • 5
  • 2
8 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2349965
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
 
LVL 22

Expert Comment

by:nietod
ID: 2350415
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
 

Author Comment

by:ipocina
ID: 2350750
Edited text of question.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:ipocina
ID: 2350755
Adjusted points to 100
0
 
LVL 22

Expert Comment

by:nietod
ID: 2350967
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
 
LVL 22

Expert Comment

by:nietod
ID: 2351014
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
 
LVL 22

Expert Comment

by:nietod
ID: 2354006
I'm testing the new withdraw answer feature. If it works, I'll answer again.
0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 2354010
It works!  I'm answering again.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
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.

588 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