Solved

Dynamically creating an array of structures

Posted on 2000-05-05
5
124 Views
Last Modified: 2013-11-20
Hi all
Win 95; VC 5.0 SP3

I am trying to create an array dynamically. this sort of works but I am
having trouble with memory leaks when destrying the array. any ideas.

CScaleThread **  ScThread;

in OnInitialUpdate()
{
....
 if (pDoc->iNumScales)
  {
  ScThread = new CScaleThread *[pDoc->iNumScales];
  }

 for (int y = 0; y < pDoc->iNumScales; y++)
  {
  ScThread[y] = 0;
  }
....
 for (int x =0; x < pDoc->iNumScales; x++)
  {
  GetDialogSize(&R);
  ScThread[x] = new CScaleThread;
  ScThread[x]->CreateThread(CREATE_SUSPENDED);
// Other stuff here
  ScThread[x]->ResumeThread();
  }
...
}

In OnDestroy()
{
 CTestscrollDoc* pDoc = GetDocument();
 if (ScThread)
  {
  for (int x =0; x < pDoc->iNumScales; x++)
   {
   if (ScThread[x])
    delete ScThread[x];
   }
  delete [] ScThread;
  }
 ScThread = 0;
}

This leaves me with a whole host of memory leaks. Why?
0
Comment
Question by:tcss
  • 3
  • 2
5 Comments
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
You did not provide the definition of a CScaleThread.  It is possible that the ctor of this object allocates but the dtor does not free.

Also make sure that your OnDestroy() is in fact being called and when (put a break point there).  If it is not being called, put the delete stuff in the the dtor of the window object or call it from some other point in the program.

0
 
LVL 1

Author Comment

by:tcss
Comment Utility
Hi Dan
Thank you for your input. By commenting you "took the blinkers" off my thinking. I found that the memory leak is comming from a "tested and working" portion of the code.

Though you did not answer the question directly (because the answer lay beyound the question asked) your invaluble comment helped be find the problem. Should you want the points, please replay with an answer indicating so. If not comment as such and I can delete the question.

Many thanks
Hylton
0
 
LVL 1

Author Comment

by:tcss
Comment Utility
This question has a deletion request Pending
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
This question no longer is pending deletion
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 100 total points
Comment Utility
Yes, I want points otherwise I would not bother answering questions.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now