473,326 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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(pStr, &iSize);
pImage = new Sprite(GetD3DDevice(), 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(pStr), 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 2325
"be*******@gmail.com" <be*******@gmail.comwrote in
news:11**********************@j44g2000cwa.googlegr oups.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(pStr, &iSize);
pImage = new Sprite(GetD3DDevice(), 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(pStr), 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(pStr, &iSize);
pImage = new Sprite(GetD3DDevice(), 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(pStr), 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(pStr), 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...@kostur.netwrote:
"benoit...@gmail.com" <benoit...@gmail.comwrote innews:11**********************@j44g2000cwa.google groups.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(pStr, &iSize);
pImage = new Sprite(GetD3DDevice(), 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(pStr), 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...@gmail.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(pStr, &iSize);
pImage = new Sprite(GetD3DDevice(), 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(pStr), 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(pStr), 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(pStr, &iSize);
pImage = new Sprite(GetD3DDevice(), 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(pStr), 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(pStr), 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...@hotmail.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(pStr, &iSize);
pImage = new Sprite(GetD3DDevice(), 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(pStr), 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(pStr), 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.com/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(pStr, &iSize);
pImage = new Sprite(GetD3DDevice(), 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(pStr), 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...@hotmail.comwrote:
benoit...@gmail.com wrote:
<snip>
void myFunction()
{
Sprite *pImage;
sprintf(pStr, "s04-%02d.png", i);
pData = pFile->FindFile(pStr, &iSize);
pImage = new Sprite(GetD3DDevice(), 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(pStr), 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(GetD3DDevice(), pData, iSize, 330, 50);
image.DefineFrame(0, 0, 330, 25, 0);
image.DefineFrame(0, 25, 330, 25, 0);
image.SetAnimFrame(0);
Song song(strdup(pStr), 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
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 ? ...
17
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...
4
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";...
20
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...
8
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...
7
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...
3
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". ...
7
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...
22
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.