memory leak with grandchild class

Seems like I should know how to solve this, but I'm drawing a blank right 
now.  Until recently, all my sprite classes have been derived from my class 
CSprite.  Now that I have derived a class from one of the derived classes, I 
am getting this from my debugger, often with more lines than this.

Detected memory leaks!
Dumping objects ->
C:\Hand in Hand\Room 101\Sprite.cpp(111) : {207} client block at 0x003273D8, 
subtype 0, 60 bytes long.
a CBitmap object at $003273D8, 60 bytes long
C:\Hand in Hand\Room 101\Sprite.cpp(111) : {206} client block at 0x00327360, 
subtype 0, 60 bytes long.
a CBitmap object at $00327360, 60 bytes long
Object dump complete.

The line it takes me to is in the base class:

void CSprite::DrawMask()
{
  . . .
  m_dibsMask[i] = new CDIBSection(this);
  . . .
}

I also delete my dibSections in the base class destructor:

CSprite::~CSprite()
{
 int i;
 for(i = 0; i < m_dibsImage.GetSize(); i++)
  delete m_dibsImage[i];

 for(i = 0; i < m_dibsMask.GetSize(); i++)
  delete m_dibsMask[i];
}

  Any explanations? 


0
srussell2458 (497)
1/14/2005 1:46:28 AM
vc.mfc 33608 articles. 0 followers. Follow

2 Replies
460 Views

Similar Articles

[PageSpeed] 0

Steve,
you did not give us enough information to say something for sure. Do next 
two things first:
1) Make sure that m_dibsMask[i] is NULL before the assignment:
ASSERT(m_dibsMask[i] == NULL);
m_dibsMask[i] = new CDIBSection(this);
2) Make sure that destructor of CSprite or its base class is virtual to 
ensure that it is called, here an example that show when you can have a 
problem if destructor is not virtual:

class CBase
{
public:
    CBase()
    {
    };

    ~CBase()
    {
    };
};

class CDerived : public CBase
{
public:
     CDerived()
     {
         m_szMessage = strdup("Hello, World");
     }
     ~CDerived()
    {
        free(m_szMessage);
    }
private:
     char *m_szMessage;
}

int main()
{
     CBase *p = new CDerived();
     delete p;
     return 0;
};

You might think that this code is clean, but actually ~CDerived() is not 
called.
To fix the problem either make ~CBase() virtual or replace "delete p" with 
delete (CDerived*)p;

Happy debugging,
-- Vladimir Khvostov


"Steve Russell" <srussell@removethisinnernet.net> wrote in message 
news:%23K3XYrd%23EHA.2568@TK2MSFTNGP10.phx.gbl...
> Seems like I should know how to solve this, but I'm drawing a blank right 
> now.  Until recently, all my sprite classes have been derived from my 
> class CSprite.  Now that I have derived a class from one of the derived 
> classes, I am getting this from my debugger, often with more lines than 
> this.
>
> Detected memory leaks!
> Dumping objects ->
> C:\Hand in Hand\Room 101\Sprite.cpp(111) : {207} client block at 
> 0x003273D8, subtype 0, 60 bytes long.
> a CBitmap object at $003273D8, 60 bytes long
> C:\Hand in Hand\Room 101\Sprite.cpp(111) : {206} client block at 
> 0x00327360, subtype 0, 60 bytes long.
> a CBitmap object at $00327360, 60 bytes long
> Object dump complete.
>
> The line it takes me to is in the base class:
>
> void CSprite::DrawMask()
> {
>  . . .
>  m_dibsMask[i] = new CDIBSection(this);
>  . . .
> }
>
> I also delete my dibSections in the base class destructor:
>
> CSprite::~CSprite()
> {
> int i;
> for(i = 0; i < m_dibsImage.GetSize(); i++)
>  delete m_dibsImage[i];
>
> for(i = 0; i < m_dibsMask.GetSize(); i++)
>  delete m_dibsMask[i];
> }
>
>  Any explanations?
> 


0
1/14/2005 3:35:18 AM
Thank you very much for taking time to help me see the problem, which was 
something I had forgotten about and not mentioned in the post.  By directing 
me to the ASSERT statement, you enabled me to track down a function in which 
I was accidentally creating more DIBSections in subsequent calls to 
DrawMask().  And now it is solved.  Thanks!
--------------------
"Vladimir Khvostov" <vladimir.khvostov@gNoSpammailz.com> wrote in message 
news:OpdmUoe%23EHA.3120@TK2MSFTNGP12.phx.gbl...
> Steve,
> you did not give us enough information to say something for sure. Do next 
> two things first:
> 1) Make sure that m_dibsMask[i] is NULL before the assignment:
> ASSERT(m_dibsMask[i] == NULL);
> m_dibsMask[i] = new CDIBSection(this);
> 2) Make sure that destructor of CSprite or its base class is virtual to 
> ensure that it is called, here an example that show when you can have a 
> problem if destructor is not virtual:
>
> class CBase
> {
> public:
>    CBase()
>    {
>    };
>
>    ~CBase()
>    {
>    };
> };
>
> class CDerived : public CBase
> {
> public:
>     CDerived()
>     {
>         m_szMessage = strdup("Hello, World");
>     }
>     ~CDerived()
>    {
>        free(m_szMessage);
>    }
> private:
>     char *m_szMessage;
> }
>
> int main()
> {
>     CBase *p = new CDerived();
>     delete p;
>     return 0;
> };
>
> You might think that this code is clean, but actually ~CDerived() is not 
> called.
> To fix the problem either make ~CBase() virtual or replace "delete p" with 
> delete (CDerived*)p;
>
> Happy debugging,
> -- Vladimir Khvostov
>
>
> "Steve Russell" <srussell@removethisinnernet.net> wrote in message 
> news:%23K3XYrd%23EHA.2568@TK2MSFTNGP10.phx.gbl...
>> Seems like I should know how to solve this, but I'm drawing a blank right 
>> now.  Until recently, all my sprite classes have been derived from my 
>> class CSprite.  Now that I have derived a class from one of the derived 
>> classes, I am getting this from my debugger, often with more lines than 
>> this.
>>
>> Detected memory leaks!
>> Dumping objects ->
>> C:\Hand in Hand\Room 101\Sprite.cpp(111) : {207} client block at 
>> 0x003273D8, subtype 0, 60 bytes long.
>> a CBitmap object at $003273D8, 60 bytes long
>> C:\Hand in Hand\Room 101\Sprite.cpp(111) : {206} client block at 
>> 0x00327360, subtype 0, 60 bytes long.
>> a CBitmap object at $00327360, 60 bytes long
>> Object dump complete.
>>
>> The line it takes me to is in the base class:
>>
>> void CSprite::DrawMask()
>> {
>>  . . .
>>  m_dibsMask[i] = new CDIBSection(this);
>>  . . .
>> }
>>
>> I also delete my dibSections in the base class destructor:
>>
>> CSprite::~CSprite()
>> {
>> int i;
>> for(i = 0; i < m_dibsImage.GetSize(); i++)
>>  delete m_dibsImage[i];
>>
>> for(i = 0; i < m_dibsMask.GetSize(); i++)
>>  delete m_dibsMask[i];
>> }
>>
>>  Any explanations?
>>
>
> 


0
srussell2458 (497)
1/15/2005 12:08:08 AM
Reply:

Similar Artilces:

Memory usage in XP (memory leak detection)
Hi How can I found out the memory usage on XP ? My XP box uses over a week all memory and then has no resources available. It has 4GB memory (3.5 GB free) I used typeperf with the following: \Memory\Available Bytes \Memory\Committed Bytes \Memory\Pool Paged Bytes \Memory\Pool Nonpaged Bytes \Memory\Cache Bytes \Memory\Cache Bytes Peak \Process(_total)\Virtual Bytes Peak \Process(_total)\Virtual Bytes \Process(_total)\Working Set Peak \Process(_total)\Working Set \Process(_total)\Page File Bytes Peak \Process(_total)\Page File Bytes \Process(_total)\Private Bytes \Proc...

not enough memory on excel 2008
Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel <i>run excel 2008 on mac snow leopard 2 g memory i upgrade to 3 g and same</i>&#32; message not enough memory can some one help me only word working Run Remove Office then re-install Office On 30/03/10 7:08 AM, in article 59bb629f.-1@webcrossing.JaKIaxP2ac0, "Snow_leopard@officeformac.com" <Snow_leopard@officeformac.com> wrote: > Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel > run excel 2008 on mac snow leopard 2 g memory i upgrade t...

Problem with CWnd Derived class
Hi! I'm making an sdi aplication in the client area I would like to put a child window. I have try it with a child window derived form CFrameWnd, but it doesn´t work so i'm trying it now with a window derived from CWnd, but it doesn´t work i have an asser when i try to modify the window style. Is this the right way to make a derived class from CWnd ? How can I resolve my problem? Thank you very much. PTZControlWindow::PTZControlWindow(PlayFile *pf) { CString strMyClass; strMyClass = AfxRegisterWndClass( CS_VREDRAW | CS_HREDRAW, ::LoadCursor(NULL, IDC_...

Is it a memory leak
AoA I have used standard template library in one of my applications. I belive that memory leaks are due to stl components, mainly string. i wrote a sample program in VC 6.0 that clearly shows memory leaks in stl wstring str = L""; for( int a = 0 ; a < 100000 ; a++ ) { str += L"int"; } str.str.erase( str.begin() , str.end() ); str = L"int"; i wrote this code against a button in dialog based application. Before clicking the button, the memory usage is 3MB and after this code it is 17MB although i believe it should have been bac...

Exchange memory utilization
Is there any documentation detailing the exchange 2003 memory utilization , especially in coordination with the max store size. thanks CR What do you want to know? Exchange will use all the memory it can in the box, up to 4GB, for caching, and this isn't a function of store size. It will release memory so that other processes can get what they need. -- Ed Crowley MVP - Exchange "Protecting the world from PSTs and brick backups!" "MSNews" <Craig@nowhere.org> wrote in message news:etwPVF%23RHHA.4832@TK2MSFTNGP03.phx.gbl... > Is there any documentati...

XmlSerializer with base classes from other assemblies
Firstly, sorry I didn't mean to cross post, my first post should have been here, Secondly: I have a derived class that serializes nicely when run in a dot net client (v1.1). However when run from a COM client the serializer constructor produces a FileNotFound exception when trying to locate the base class' assembly. The binding log shows that the serializer probes all the COM clients folders etc but The serialiser seems to ignore the obvious codebase of the base class as the source of the assembly. If I manually copy the base class assembly to every COM client's directory th...

Out of Memory
I am using Excel 2000. Operating system is MS 2000. Ram = 512. I am running a large Excel file 65,536 rows by 24 columns. The columns may expand somewhat; say 5 extra rows as the Macro runs. The Excel file links to another Excel file to do some vlookups. Some formatting is going on as well as Paste Special Value. I get an out of error message and Excel stops. If I only have 50,000 rows it works fine. I have increased the virtual memory to the maximum, same thing. Any suggestions???? Hi, There are only 65536 rows in a worksheet. You cannot use all the rows. Split your sheet into sevra...

memory growing spreadsheet
Hi Again I have another query- For some reason when working on a spreadsheet it can start with say, 925kb in size,I do a couple of changes, then "Wolla" 10+mb It only happens every now and again, but I have tried copy then paste special, taken all macro's out, save as a different name,copy sheet into different book, but still can't bring it back down in size, "Hu" I even took my changes back out What is going on? John .. Hi John, What are you doing? Whatever it is, I think I'd want to do something else. But I haven't any hint what to avoid doing &...

Is this a memory leak?
Hi, I am trying to figure out why a program keep consuming more memory the longer that it runs. I tried detecting a memory leak using the CMemoryState object. I find that the program is using more memory after executing this line: PullComma(sData); The source for PullComma is: //======================================================================= void PullComma(CString& string) // Remove comma from start of string //======================================================================= { Strip(string); if (string.IsEmpty()) return; if (string.GetAt(0) == ',') string =...

CPU usage, memory and virtual memory
Hello, On certain occasions, some applications can consume close to 100% cpu usage and freeze up the machine. But some apps can consume 100% cpu, but allow you to work on other applciations. What is the difference? Is it the memory and virtual memory consumption? Number ofpage faults? I am working on a multithreaded MFC application, which freezes up the machine (when it does some computations). A look at the task manager tells me it consumes 99% cpu and a lot of memory and virtual memory space. I would like to make sure that when my applciation computes, it does it as fast as possible and it...

CWinThread and Memory Leaks.
I want to have a simple progress bar displayed whilst my MFC application is doing a lengthy operation. To do this, I am creating a user interface thread which displays a dialog containing a progress bar control. This is implemented using two classes: class CProgressDlg : public CDialog class CProgressThread : public CWinThread The CProgressThread class has a member variable: CProgressDlg m_Dlg; At the start of the lengthy operation I create a new instance of CProgressThread, and set the m_bAutoDelete member TRUE before calling CProgressThread::CreateThread(); In CProgressThread::InitInst...

Accessing physical memory through PC/104 bus
Hello all. We need to access (read/write) memory on devices (cards) attached to PC/104 bus. Operating system: Microsoft Windows Embedded CE 6.0 Target processor: Advantech PCM-3375 (x86 architecture) http://www.advantech.com/products/PCM-3375/mod_1-2JKGWQ.aspx We were using operating system QNX 4.25 before, now we plan to migrate our code to WinCE6. Under QNX, we were accessing the memory using block of code similar to this one: { int addr =3D 0xd9080; // physical address on card unsigned long size =3D 0x1000; // size of block to access unsigned char...

Memory leak?
Hello Group, How can I be sure that I don't have memory leak in my dialog based app? Thanks in advance, Geo Hi > > How can I be sure that I don't have memory leak in my > dialog based app? > > Thanks in advance, > Geo you can use CMemoryState MFC class. Regards, Robert Thank you Robert. >-----Original Message----- >Hi >> >> How can I be sure that I don't have memory leak in my >> dialog based app? >> >> Thanks in advance, >> Geo > >you can use CMemoryState MFC class. > >Regards, Robert > > >....

Outlook 2000 memory error
Hi, When I deleted an item in Calendar, I got error message that "There is not enough memory available to perform the operation." I am using Outlook 2000. Thanks in advance. Grace ...

Edit story in MS Word
I am unable to use the "edit story in Word" feature with MS Pub '03. However, I *can* open MS Word while Pub is running and do copy/paste editing. I have tried disabling script blocking in NAV to no avail. Thanks in advance for any help! After managing to set up OE-QuoteFix on his new PC, Ed reads a message from Wayne Wilson <anonymous@discussions.microsoft.com>... > I have tried disabling script blocking in NAV to no avail. Have you disabled the Microsoft Office PlugIn in NAV? -- Ed Bennett - MVP Microsoft Publisher http://www.mvps.org/the_nerd/ Before reading t...

Database in memory
Hi, I have a large amount of information which I store in memory. I store this information as array of structures using CPtrArray. The structure is pretty complex, has subarrays. I normally iterate through all of the array and do some checks and process the needed information only. Speed is the keypoint. My question is: Is it possible to create a small database having several tables completely in memory and query it with SQL? If it is possible to manage that, will it be faster than my method of query? Thanks! Have a look at http://sqlite.org/ I use it in a couple of the things I work on ...

Serialize class to string?
How do you use the XmlSerializer to serialise a class instance to a string as opposed to a file or stream? Thanks Ben Fidge was answered for example in this thread [0] -- HTH Christoph Schittko [MVP] Software Architect, .NET Mentor [0] http://dbforums.com/arch/73/2002/10/516420 "Ben Fidge" <ben.fidge@btopenworld.com> wrote in message news:#007uUmxDHA.2304@TK2MSFTNGP12.phx.gbl... > How do you use the XmlSerializer to serialise a class instance to a string > as opposed to a file or stream? > > Thanks > > Ben Fidge > > ...

Pass Message to Base Class Message Handler
Can I relay message to base class message handler ? If I have base class derived from some other class e.g CWnd then how can I handle base class message handlers ?? If I call base class PreTranslateMessage manually its not getting invoked...... e.g In My derived class PreTranslateMessage if I invoke CBaseClass::PreTranslateMessage(pMsg).... base class pretranslatemessage is not getting invoked....? -Akshay Did you make the function "virtual" in your derived class? You could also try calling with GetParent() if it is a window. Tom <akshay4friend@yahoo.com> wrote in mes...

Virtual Memory Fragmentation #3
I get this message in the Event Viewer: The virtual memory necessary to run your Exchange server is fragmented in such a way that normal operation may begin to fail. It is highly recommended that you restart all Exchange services to correct this issue. Can't resolve the problem with restarting the Exchange 2K services or rebooting the server. Is this a critical issue or can it be left alone? And how to fix? Hi Hassan, Exchange server requires some amount of virtual memory and one thing is for sure that Exchange is memory greedy application... every application requires some amou...

memory.dmp
if windows crashes and memory gets dumped into memory.dmp, what can I as a user do with this file? do I need it? can there be anything in there that would help me to find out why windows is crashing? Using Wordpad, after 1 minute, it seems like it has only opened 1%. rr@nospam.edu wrote: > if windows crashes and memory gets dumped into memory.dmp, what can I as > a user do with this file? do I need it? can there be anything in there > that would help me to find out why windows is crashing? Using Wordpad, > after 1 minute, it seems like it has only opened 1%. You n...

Unable to generate a temporary class
I followed the article for "[creating] a service account for an ASP.NET 2.0 application" (http://msdn.microsoft.com/en-us/library/ff649309.aspx). However, when I run my application I receive the error message "Unable to generate a temporary class". I've tried implementing a custom account to use as the Identity for a separate Application Pool, because I need the application to connect to an SQL Server database on a separate server using Windows Authentication. I've read articles suggesting that granting limited permissions on C:\WINNT\Temp will wo...

CDC::BitBlt, memory 2 memory.
This is a multi-part message in MIME format. ------=_NextPart_000_0017_01C4044E.48372430 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable I need to bitblt a bitmap onto a "ball" DC, then from the "ball" DC to a = memory DC, then from the memory DC to the window DC. But I am very = difficulties! What am I missing? It seemed so simple on paper. = Arrrhhhh... CBitmap bmp; bmp.LoadBitmap(IDB_BALL); BITMAP bmpInfo; bmp.GetBitmap(&bmpInfo); CDC DCMemory, DCBall; CBrush CBWhite(RGB(255, 255, 255)); DCMemory.CreateCompat...

Out of memory
Hello, I am working on a macro with excel, which crunches a lot of data and plots graphs. I am running into issues with Out of memory problems. I am currently using 512 MB Ram computer with Excel 2000. Any suggestions or pointers to solve this problem would be appreciated. Thanks. The amount of physical memory you have is of little consequence as Excel only uses it's own pool. You may wish to check this out? http://www.decisionmodels.com/memlimitsc.htm -- HTH Nick Hodge Microsoft MVP - Excel Southampton, England nick_hodgeTAKETHISOUT@zen.co.uk.ANDTHIS "fobinus" <h...

not enough memory to get more office assisants why?
When I try to change my office assistant I get a message on screen saying "there is not enough memory available to complete this operation. How do I change this situation so that I can get different office assistants??? Someone please help! I am using Microsoft Works Suite 2003 See http://uksbsguy.com/blogs/doverton/archive/2007/07/23/how-to-get-the-office-assistant-office-2003-to-work-in-windows-vista-i-e-remove-the-quot-not-enough-memory-quot-error-messages.aspx -- <>>< ><<> ><<> <>>< ><<> <>>< <&g...

referenced memory error
Receiving the following error when shutting down Outlook Express, can anyone help? the instruction at "0x77f83187" referenced memory at "0x00924830", the memory could not be "written" Click on OK to terminate the program ...