473,836 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

false memory leak?

I don't have a lot of experience with C++ so I apologize if this is a
stupid question.
I use Paul Nettle's memory manager (mmgr.cpp) which reports a memory
leak but I don't think there's one. Here is the code (i took some stuff
out to simplify):

void myFunction()
{
Sprite *pImage;
sprintf(pStr, "s04-%02d.png", i);
pData = pFile->FindFile(pSt r, &iSize);
pImage = new Sprite(GetD3DDe vice(), pData, iSize, 330, 50);
pImage->DefineFrame( 0, 0, 330, 25, 0);
pImage->DefineFrame( 0, 25, 330, 25, 0);
pImage->SetAnimFrame(0 );

Song *pSong = new Song(strdup(pSt r), pImage);
}

The leak is reported on "Sprite *pImage". The pImage object passed to
the Song constructor is being released by the Song destructor. My
understanding is that the pImage pointer is released when myFunction()
exits. Since I do not delete the object before the pointer release, I
assume this is the reason why mmgr.cpp reports the leak.

Am I right or am I missing something?

Ben

Nov 30 '06 #1
9 2348
"be*******@gmai l.com" <be*******@gmai l.comwrote in
news:11******** **************@ j44g2000cwa.goo glegroups.com:
I don't have a lot of experience with C++ so I apologize if this is a
stupid question.
I use Paul Nettle's memory manager (mmgr.cpp) which reports a memory
leak but I don't think there's one. Here is the code (i took some stuff
out to simplify):

void myFunction()
{
Sprite *pImage;
sprintf(pStr, "s04-%02d.png", i);
pData = pFile->FindFile(pSt r, &iSize);
pImage = new Sprite(GetD3DDe vice(), pData, iSize, 330, 50);
pImage->DefineFrame( 0, 0, 330, 25, 0);
pImage->DefineFrame( 0, 25, 330, 25, 0);
pImage->SetAnimFrame(0 );

Song *pSong = new Song(strdup(pSt r), pImage);
}

The leak is reported on "Sprite *pImage". The pImage object passed to
the Song constructor is being released by the Song destructor. My
understanding is that the pImage pointer is released when myFunction()
exits. Since I do not delete the object before the pointer release, I
assume this is the reason why mmgr.cpp reports the leak.

Am I right or am I missing something?
Why would the memory pointed to by pImage be released by the above code?
I don't see a delete anywhere in there (And I'm assuming the constructor
of Song doesn't delete the pointer it's passed).
Nov 30 '06 #2

be*******@gmail .com wrote:
I don't have a lot of experience with C++ so I apologize if this is a
stupid question.
I use Paul Nettle's memory manager (mmgr.cpp) which reports a memory
leak but I don't think there's one. Here is the code (i took some stuff
out to simplify):

void myFunction()
{
Sprite *pImage;
sprintf(pStr, "s04-%02d.png", i);
pData = pFile->FindFile(pSt r, &iSize);
pImage = new Sprite(GetD3DDe vice(), pData, iSize, 330, 50);
pImage->DefineFrame( 0, 0, 330, 25, 0);
pImage->DefineFrame( 0, 25, 330, 25, 0);
pImage->SetAnimFrame(0 );

Song *pSong = new Song(strdup(pSt r), pImage);
}

The leak is reported on "Sprite *pImage". The pImage object passed to
the Song constructor is being released by the Song destructor. My
understanding is that the pImage pointer is released when myFunction()
exits. Since I do not delete the object before the pointer release, I
assume this is the reason why mmgr.cpp reports the leak.

Am I right or am I missing something?

Ben
put

delete pImage;

after Song *pSong = new Song(strdup(pSt r), pImage);

Nov 30 '06 #3
I am aware that the memory pointed by pImage is not released in this
code. What I meant was that the pointer itself is destroyed.
The memory is now being pointed to by the newly created Song object.
The destructor for the Song object does release the memory.
On Nov 30, 10:10 am, Andre Kostur <nntps...@kostu r.netwrote:
"benoit...@gmai l.com" <benoit...@gmai l.comwrote innews:11****** *************** *@j44g2000cwa.g ooglegroups.com :
I don't have a lot of experience with C++ so I apologize if this is a
stupid question.
I use Paul Nettle's memory manager (mmgr.cpp) which reports a memory
leak but I don't think there's one. Here is the code (i took some stuff
out to simplify):
void myFunction()
{
Sprite *pImage;
sprintf(pStr, "s04-%02d.png", i);
pData = pFile->FindFile(pSt r, &iSize);
pImage = new Sprite(GetD3DDe vice(), pData, iSize, 330, 50);
pImage->DefineFrame( 0, 0, 330, 25, 0);
pImage->DefineFrame( 0, 25, 330, 25, 0);
pImage->SetAnimFrame(0 );
Song *pSong = new Song(strdup(pSt r), pImage);
}
The leak is reported on "Sprite *pImage". The pImage object passed to
the Song constructor is being released by the Song destructor. My
understanding is that the pImage pointer is released when myFunction()
exits. Since I do not delete the object before the pointer release, I
assume this is the reason why mmgr.cpp reports the leak.
Am I right or am I missing something?Why would the memory pointed to by pImage be released by the above code?
I don't see a delete anywhere in there (And I'm assuming the constructor
of Song doesn't delete the pointer it's passed).
Nov 30 '06 #4
benj,

isn't that going to destroy the object pointed by pImage making it
unavailable to my Song object?

On Nov 30, 10:17 am, "benj" <hojotool...@gm ail.comwrote:
benoit...@gmail .com wrote:
I don't have a lot of experience with C++ so I apologize if this is a
stupid question.
I use Paul Nettle's memory manager (mmgr.cpp) which reports a memory
leak but I don't think there's one. Here is the code (i took some stuff
out to simplify):
void myFunction()
{
Sprite *pImage;
sprintf(pStr, "s04-%02d.png", i);
pData = pFile->FindFile(pSt r, &iSize);
pImage = new Sprite(GetD3DDe vice(), pData, iSize, 330, 50);
pImage->DefineFrame( 0, 0, 330, 25, 0);
pImage->DefineFrame( 0, 25, 330, 25, 0);
pImage->SetAnimFrame(0 );
Song *pSong = new Song(strdup(pSt r), pImage);
}
The leak is reported on "Sprite *pImage". The pImage object passed to
the Song constructor is being released by the Song destructor. My
understanding is that the pImage pointer is released when myFunction()
exits. Since I do not delete the object before the pointer release, I
assume this is the reason why mmgr.cpp reports the leak.
Am I right or am I missing something?
Benput

delete pImage;

after Song *pSong = new Song(strdup(pSt r), pImage);
Nov 30 '06 #5

be*******@gmail .com wrote:
I don't have a lot of experience with C++
In that case, you should probably steer clear of manual memory
management until you have more experience. Then you will know that you
should usually steer clear of manual memory management.
so I apologize if this is a
stupid question.
I use Paul Nettle's memory manager (mmgr.cpp) which reports a memory
leak but I don't think there's one. Here is the code (i took some stuff
out to simplify):

void myFunction()
{
Sprite *pImage;
sprintf(pStr, "s04-%02d.png", i);
pData = pFile->FindFile(pSt r, &iSize);
pImage = new Sprite(GetD3DDe vice(), pData, iSize, 330, 50);
pImage->DefineFrame( 0, 0, 330, 25, 0);
pImage->DefineFrame( 0, 25, 330, 25, 0);
pImage->SetAnimFrame(0 );

Song *pSong = new Song(strdup(pSt r), pImage);
}

The leak is reported on "Sprite *pImage". The pImage object passed to
the Song constructor is being released by the Song destructor.
Every new must be matched by exactly one delete (and every new [] must
be matched by one delete []). Your code has two occurances of new and
no occurances of delete. *IF* the Song destructor deletes the pointer
passed to it, *AND* the Song class handles holding the pointer and the
Rule of Three correctly, *AND* none of the code between allocating the
new Sprite to pImage and passing pImage to the Song constructor can
ever throw an exception, *AND* you don't do anything else with the
pImage pointer that contradicts this ownership policy, *THEN* the
memory allocated to pImage should be OK. Except that the Song object is
dynamically allocated too - is there a reason you couldn't do Song
song(strdup(pSt r), pImage);? So you need to be absolutely sure you get
all those things right for pSong as well.
My
understanding is that the pImage pointer is released when myFunction()
exits.
pImage is local to myFunction. At the end of that function, the local
pointer object pImage is destroyed. That is not the same thing as
delete pImage; The memory pointed to by pImage is not deleted
automatically at the end of myFunction.
Since I do not delete the object before the pointer release, I
assume this is the reason why mmgr.cpp reports the leak.
You would have to consult the documentation of your memory manager tool
to understand what forms of code might trigger false alarms.
Am I right or am I missing something?
You are missing the fact that new should never appear out on its own in
your code. You should be using object designed for safely handling
memory (containers and smart pointers - the standard library and boost
have plenty). Doing your own memory management is hard. Where are you
learning C++ from that doesn't tell you this?

Gavin Deane

Nov 30 '06 #6
Man I love those forums, I feel like I'm back in school getting
scolding from my teachers :P

Anyway, Gavin, I would love to stay away from manual memory management.
In this example where I have to create an object, change some of its
attributes and pass it to another constructor, how would you rewrite it
so I don't have to deal with those manual memory management issues?

Thanks for your help

On Nov 30, 10:26 am, "Gavin Deane" <deane_ga...@ho tmail.comwrote:
benoit...@gmail .com wrote:
I don't have a lot of experience with C++In that case, you should probably steer clear of manual memory
management until you have more experience. Then you will know that you
should usually steer clear of manual memory management.
so I apologize if this is a
stupid question.
I use Paul Nettle's memory manager (mmgr.cpp) which reports a memory
leak but I don't think there's one. Here is the code (i took some stuff
out to simplify):
void myFunction()
{
Sprite *pImage;
sprintf(pStr, "s04-%02d.png", i);
pData = pFile->FindFile(pSt r, &iSize);
pImage = new Sprite(GetD3DDe vice(), pData, iSize, 330, 50);
pImage->DefineFrame( 0, 0, 330, 25, 0);
pImage->DefineFrame( 0, 25, 330, 25, 0);
pImage->SetAnimFrame(0 );
Song *pSong = new Song(strdup(pSt r), pImage);
}
The leak is reported on "Sprite *pImage". The pImage object passed to
the Song constructor is being released by the Song destructor.Ever y new must be matched by exactly one delete (and every new [] must
be matched by one delete []). Your code has two occurances of new and
no occurances of delete. *IF* the Song destructor deletes the pointer
passed to it, *AND* the Song class handles holding the pointer and the
Rule of Three correctly, *AND* none of the code between allocating the
new Sprite to pImage and passing pImage to the Song constructor can
ever throw an exception, *AND* you don't do anything else with the
pImage pointer that contradicts this ownership policy, *THEN* the
memory allocated to pImage should be OK. Except that the Song object is
dynamically allocated too - is there a reason you couldn't do Song
song(strdup(pSt r), pImage);? So you need to be absolutely sure you get
all those things right for pSong as well.
My
understanding is that the pImage pointer is released when myFunction()
exits.pImage is local to myFunction. At the end of that function, the local
pointer object pImage is destroyed. That is not the same thing as
delete pImage; The memory pointed to by pImage is not deleted
automatically at the end of myFunction.
Since I do not delete the object before the pointer release, I
assume this is the reason why mmgr.cpp reports the leak.You would have to consult the documentation of your memory manager tool
to understand what forms of code might trigger false alarms.
Am I right or am I missing something?You are missing the fact that new should never appear out on its own in
your code. You should be using object designed for safely handling
memory (containers and smart pointers - the standard library and boost
have plenty). Doing your own memory management is hard. Where are you
learning C++ from that doesn't tell you this?

Gavin Deane
Nov 30 '06 #7
be*******@gmail .com wrote:
I am aware that the memory pointed by pImage is not released in this
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.c om/c++-faq-lite/how-to-post.html>
Nov 30 '06 #8
be*******@gmail .com wrote:
I don't have a lot of experience with C++ so I apologize if this is a
stupid question.
I use Paul Nettle's memory manager (mmgr.cpp) which reports a memory
leak but I don't think there's one. Here is the code (i took some stuff
out to simplify):

void myFunction()
{
Sprite *pImage;
sprintf(pStr, "s04-%02d.png", i);
pData = pFile->FindFile(pSt r, &iSize);
pImage = new Sprite(GetD3DDe vice(), pData, iSize, 330, 50);
pImage->DefineFrame( 0, 0, 330, 25, 0);
pImage->DefineFrame( 0, 25, 330, 25, 0);
pImage->SetAnimFrame(0 );

Song *pSong = new Song(strdup(pSt r), pImage);
}

The leak is reported on "Sprite *pImage". The pImage object passed to
the Song constructor is being released by the Song destructor. My
understanding is that the pImage pointer is released when myFunction()
exits. Since I do not delete the object before the pointer release, I
assume this is the reason why mmgr.cpp reports the leak.

Am I right or am I missing something?
If what you say (that the Song destructor is called and in turn calls
the Sprite destructor), you're right, there is no leak.
Nov 30 '06 #9
Please don't top-post. Thanks. Rearranged.
be*******@gmail .com wrote:
On Nov 30, 10:26 am, "Gavin Deane" <deane_ga...@ho tmail.comwrote:
benoit...@gmail .com wrote:
<snip>
void myFunction()
{
Sprite *pImage;
sprintf(pStr, "s04-%02d.png", i);
pData = pFile->FindFile(pSt r, &iSize);
pImage = new Sprite(GetD3DDe vice(), pData, iSize, 330, 50);
pImage->DefineFrame( 0, 0, 330, 25, 0);
pImage->DefineFrame( 0, 25, 330, 25, 0);
pImage->SetAnimFrame(0 );
Song *pSong = new Song(strdup(pSt r), pImage);
}
<snip>
Am I right or am I missing something?You are missing the fact that new should never appear out on its own in
your code. You should be using object designed for safely handling
memory (containers and smart pointers - the standard library and boost
have plenty). Doing your own memory management is hard. Where are you
learning C++ from that doesn't tell you this?
Man I love those forums, I feel like I'm back in school getting
scolding from my teachers :P

Anyway, Gavin, I would love to stay away from manual memory management.
In this example where I have to create an object, change some of its
attributes and pass it to another constructor, how would you rewrite it
so I don't have to deal with those manual memory management issues?
Well, without knowing any more of your requirements, or anything about
the classes involved, the obvious answer is to have a Song object own
its Sprite object.

Sprite image(GetD3DDev ice(), pData, iSize, 330, 50);
image.DefineFra me(0, 0, 330, 25, 0);
image.DefineFra me(0, 25, 330, 25, 0);
image.SetAnimFr ame(0);
Song song(strdup(pSt r), image);

That does involve copying the Sprite object, which, based on its name,
I can imagine might be expensive. If so, and if, as your original code
suggests, the Sprite is owned by and exists only in the Song, then the
Song perhaps needs a constructor and/or member functions that allow you
to manipulate the Sprite as required via the Song interface. But as I
say, without knowing more about what you're trying to do, I'm
speculating and I could be spot on or I could be a million miles away.

Gavin Deane

Nov 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3419
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? In nut shell, what is/are the realtion/s between the Memory Leak and Memory Corruption. Juts Theoritical Assumtion below:
17
4824
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is easier to reproduce (e.g.: remote machine not available). After about 1 day, I get a usage of 300MB of memory. I have used .NET Memory Profiler tool to try to see where the leak is located. For all the leaky instances, I can see the following (I...
4
6095
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password"; scope.Path.Path=@"\\pc\root\cimv2";
20
8124
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex matches are called within a loop (like if or for). E.g. for(int i = 0; i < 10; i++) { Regex r = new Regex();
8
8560
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of each object within my JS app to help locate my problem? Thanks
7
6941
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports the status of the machines. Upon we follow the .NET object lifetime recommendations the application is constantly consuming more memory! The problem is on the ManagementObjectSearch, upon we Dispose the object it seems that is not releasing the...
3
5332
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". Anybody know anything about this? Does *Javascript* leak memeory, or does the *browser* leak memory?
7
15720
by: Ragnar Agustsson | last post by:
Hi all I have been wandering about the best way to sandbox memory leaks in 3rd party libraries when using them from the .Net framework. I have a 3rd party library, written in C++, that leaks a lot of memory but I still had to use it. 1. After using DLLImport and seeing the memory leak I tried to load and
22
9372
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a memory leak someplace. I can not detect the memory leak by running several reports by hand, but when I run tha app as a servrice and process few hundred reports there is significant memory leak. The application can consume over 1GB of memory where it...
0
9812
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9658
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10824
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10579
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7775
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6975
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5813
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4443
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.