Click here to Skip to main content
16,007,126 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: allocate memory Pin
Joaquín M López Muñoz12-Sep-01 0:27
Joaquín M López Muñoz12-Sep-01 0:27 
GeneralRe: allocate memory Pin
Gérald Mercet12-Sep-01 0:35
Gérald Mercet12-Sep-01 0:35 
GeneralRe: allocate memory Pin
Bernhard12-Sep-01 0:27
Bernhard12-Sep-01 0:27 
GeneralRe: allocate memory Pin
Gérald Mercet12-Sep-01 0:36
Gérald Mercet12-Sep-01 0:36 
GeneralRe: allocate memory Pin
Tomasz Sowinski12-Sep-01 0:57
Tomasz Sowinski12-Sep-01 0:57 
GeneralRe: allocate memory Pin
Steen Krogsgaard12-Sep-01 2:36
Steen Krogsgaard12-Sep-01 2:36 
GeneralRe: allocate memory Pin
Gérald Mercet12-Sep-01 3:15
Gérald Mercet12-Sep-01 3:15 
GeneralRe: allocate memory Pin
Steen Krogsgaard12-Sep-01 3:59
Steen Krogsgaard12-Sep-01 3:59 
This reply got longer than I intended, sorry about that. Anyway, here goes:

I'm not too sure what you mean. But if you delete the array of dates then all the information contained in the date objects are gone.
void CMyClass::SomeFunction()
{
   COleDateTime* pDates = AllocTwoDates();
   // set first date object to current date
   pDates[0] = COleDateTime::GetCurrentTime();
   // set second date object to same day and time next year
   pDates[1].SetDate(pDates[0].GetYear()+1 ,pDates[0].GetMonth() ,pDates[0].GetDay());
   // now we delete the date objects
   delete[] pDates;
   // now every information in the date objects are gone.
   CString cs = pDates[1].Format(%d %b %Y); // illegal as pDates[1] is deleted!!!!

If you want to keep the date objects after SomeFuntion() returns you should make pDates a member variable instead, do the allocation in the constructor (if possible) and the deletion in the destructor. If the number of objects in the date array changes in CMyClass' lifetime you should at least delete the pointer in the destructor:
// in CMyClass.h

class CMyClass
{
public:
   CMyClass();
   ~CMyClass();

   void SomeFunction();
   void UseTheDates();
protected:
   void AllocTwoDates();
   void DeleteDates();
   const COleDateTime* GetDates();
private:
   COleDateTime* m_pDates;
};
   
// in CMyClass.cpp
CMyClass::CMyClass()
{
   m_pDates = NULL;
}

CMyClass::~CMyClass()
{
   DeleteDates();
}

void CMyClass::SomeFunction()
{
   AllocTwoDates();
   // don't delete dates
}

void CMyClass::UseTheDates()
{
   // use the date objects in m_pDates, they are still valid if m_pDates != NULL
   if (GetDates() != NULL) {
      // do something
   }
}

void CMyClass::AllocTwoDates()
{
   DeleteDates(); // can be called even if dates are already deleted
   m_pDates = new COleDateTime[2];
} 

void CMyClass::DeleteDates()
{
   if (m_pDates != NULL) { // this is where the protection for double-deletion is
      delete[] m_pDates;
      m_pDates = NULL; // important!
   }
}

Note that I've changed AllocTwoDates so it works directly on the member variable instead of returning the pointer. There is no need to return a pointer to a private member - it's accessible via the member variable from any member function (including members of derived classes) and shouldn't be accessibel from outside CMyClass. This way you're sure that only methods in CMyClass can modify m_pDates. No derived classes or outside classes can reach m_pDates directly, but has to get the pointer through GetDates(). This method returns a pointer to const COleDateTime objects and this pointer cannot be an argument for delete[]. In effect, only DeleteDates() can do the deletion, and only AllocTwoDates() can do the allocation. Voila, no memory leaks!

Anyway, you should store the size of your m_pDates array somewhere (e.g. in a member variable, m_iSizeDates). This way you can avoid accessing illegal memory (e.g. m_pDates[2] in the example above).

Finally, the STL vector class or the MFC CArray template class may prove more suitable for you - they take care of allocation/reallocation and deletion, and you can pass them instead of a COleDateTime* to functions.

Cheers
Steen.

"To claim that computer games influence children is rediculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"
GeneralRe: allocate memory Pin
Gérald Mercet12-Sep-01 5:03
Gérald Mercet12-Sep-01 5:03 
GeneralRe: allocate memory Pin
Steen Krogsgaard12-Sep-01 21:37
Steen Krogsgaard12-Sep-01 21:37 
GeneralRe: allocate memory Pin
Gérald Mercet12-Sep-01 23:32
Gérald Mercet12-Sep-01 23:32 
GeneralRe: allocate memory Pin
Steen Krogsgaard12-Sep-01 23:57
Steen Krogsgaard12-Sep-01 23:57 
GeneralRe: allocate memory Pin
Gérald Mercet13-Sep-01 5:56
Gérald Mercet13-Sep-01 5:56 
GeneralRe: allocate memory Pin
Steen Krogsgaard13-Sep-01 21:20
Steen Krogsgaard13-Sep-01 21:20 
GeneralRe: allocate memory Pin
Gérald Mercet13-Sep-01 22:51
Gérald Mercet13-Sep-01 22:51 
GeneralRe: allocate memory Pin
Steen Krogsgaard13-Sep-01 23:00
Steen Krogsgaard13-Sep-01 23:00 
GeneralRe: allocate memory Pin
Gérald Mercet13-Sep-01 23:39
Gérald Mercet13-Sep-01 23:39 
GeneralRe: allocate memory Pin
Steen Krogsgaard13-Sep-01 23:48
Steen Krogsgaard13-Sep-01 23:48 
GeneralRe: allocate memory Pin
Gérald Mercet14-Sep-01 2:37
Gérald Mercet14-Sep-01 2:37 
GeneralRe: allocate memory Pin
Steen Krogsgaard14-Sep-01 2:53
Steen Krogsgaard14-Sep-01 2:53 
GeneralRe: allocate memory Pin
Gérald Mercet14-Sep-01 3:24
Gérald Mercet14-Sep-01 3:24 
GeneralRe: allocate memory Pin
Steen Krogsgaard14-Sep-01 3:27
Steen Krogsgaard14-Sep-01 3:27 
GeneralRe: allocate memory Pin
Gérald Mercet12-Sep-01 4:00
Gérald Mercet12-Sep-01 4:00 
GeneralRe: allocate memory Pin
Steen Krogsgaard12-Sep-01 4:07
Steen Krogsgaard12-Sep-01 4:07 
Questionwhile statement probleam, please help? Pin
Tea CH11-Sep-01 17:31
Tea CH11-Sep-01 17:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.