473,387 Members | 1,532 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,387 software developers and data experts.

Memory Leak in deque ?

stp

Hello,

I declare the following types:

typedef pair<long, string> CEventPair;

typedef deque<CEventPair*> EventPairCont;

// here I add some new elements

// l is a long

// value is a string

// events is EventPairCont

if ((npair = new CEventPair (l, value)) != NULL) {

events.push_back (npair);

}

// here I do something with this

...

// at the end I use clear-method to destroy all

events.clear ();

After Shutdown Boundschecker reports me Memory leaks allocated in the
line with new CEventPair. If I step throught code I do allocation
memory for string. But I dont step into the destructor of the string
down from clear ()

Where is the problem ?

Thanks to all.
--
Posted via http://dbforums.com
Jul 19 '05 #1
11 6171
stp wrote:
Hello,

After Shutdown Boundschecker reports me Memory leaks allocated in the
line with new CEventPair. If I step throught code I do allocation
memory for string. But I dont step into the destructor of the string
down from clear ()

Where is the problem ?


*You* are allocating memory for CEventPair, so *you* should clean it up. You
cannot expect deque to delete the pointers you store in it (after all, it
cannot know if you still have a reference lying around somewhere). You
either need to store non-pointers, but the objects themselves (that'll work
fine with the copy-constructor supplied by pair) in the deque or delete the
pointers you allocated yourself.

--
Unforgiven

"Most people make generalisations"
Freek de Jonge

Jul 19 '05 #2

"stp" <me*********@dbforums.com> wrote in message news:33****************@dbforums.com...
// at the end I use clear-method to destroy all

events.clear ();


That doesn't destory any CEventPairs. Your container only
stores pointers. Clearing or deleting the container only
destroys the pointers stored in it, it doesn't call delete on
each element. You need to iterate over the deque and
delete each element.

C++ design is usually symetrical. You delete things in
a complementary manner than they were created.
Jul 19 '05 #3

"stp" <me*********@dbforums.com> wrote in message
news:33****************@dbforums.com...

Hello,

I declare the following types:

typedef pair<long, string> CEventPair;

typedef deque<CEventPair*> EventPairCont;

// here I add some new elements

// l is a long

// value is a string

// events is EventPairCont

if ((npair = new CEventPair (l, value)) != NULL) {

events.push_back (npair);

}

// here I do something with this

..

// at the end I use clear-method to destroy all

events.clear ();

After Shutdown Boundschecker reports me Memory leaks allocated in the
line with new CEventPair. If I step throught code I do allocation
memory for string. But I dont step into the destructor of the string
down from clear ()

Where is the problem ?

Thanks to all.
--
Posted via http://dbforums.com

Try using autoptr or write a loop in your destructor to clear the memory in
CEventPair etc

Jul 19 '05 #4
Govindan wrote:


Try using autoptr or write a loop in your destructor to clear the memory in
CEventPair etc


Do *not* use auto_ptr!! It is not suitable for standard containers.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #5
stp wrote:

if ((npair = new CEventPair (l, value)) != NULL) {
This test can never be true. The 'new' operator either returns a valid
pointer or throws an exception.


// at the end I use clear-method to destroy all

events.clear ();


As others pointed out, this does not delete any of your dynamic objects.
You need to do that yourself. You could make it automatic by using a
smart pointer, but you may not use auto_ptr for this purpose, as it does
not meet the requirements for standard containers.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #6
Kevin Goodsell wrote:
stp wrote:

if ((npair = new CEventPair (l, value)) != NULL) {

This test can never be true.


Excuse me, I meant "This test can never be false", of course.

To put it another way, 'new' can never return a null pointer.

The 'new' operator either returns a valid
pointer or throws an exception.


-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #7
Kevin Goodsell wrote:
To put it another way, 'new' can never return a null pointer.


While I'd like to agree with you (and certainly do in the context of the C++
standard), for many programmers, new certainly can return a NULL pointer.
Visual C++ 6 and older have a (non-standards compliant) non-throwing
operator new. In Visual C++ .NET 2002 and 2003 you get the non-throwing new
if you explicitly ask for it (by using a special form of new) or if the CRT
happens to be linked before the Standard C++ Library (which you can
prevent).

This is the related article from the VS.NET 2003 documentation:
http://msdn.microsoft.com/library/en..._Operators.asp

I realise this group is about Standard C++, and what you said is true for
Standard C++, I'm not debating that. I'm simply pointing out that we
shouldn't lose sight of (however sad) reality there might be out there, and
I'm also trying to prevent the OP from adopting behaviour that might not
apply to his/her compiler.

--
Unforgiven

"Most people make generalisations"
Freek de Jonge

Jul 19 '05 #8
Unforgiven wrote:
Kevin Goodsell wrote:
To put it another way, 'new' can never return a null pointer.


While I'd like to agree with you (and certainly do in the context of the C++
standard), for many programmers, new certainly can return a NULL pointer.
Visual C++ 6 and older have a (non-standards compliant) non-throwing
operator new.


The workaround is to set_new_handler a routine that throws bad_alloc like it should.
Jul 19 '05 #9
On Tue, 9 Sep 2003 17:38:38 -0400, "Ron Natalie" <ro*@sensor.com> wrote:
Unforgiven wrote:
Kevin Goodsell wrote:
To put it another way, 'new' can never return a null pointer.


While I'd like to agree with you (and certainly do in the context of the C++
standard), for many programmers, new certainly can return a NULL pointer.
Visual C++ 6 and older have a (non-standards compliant) non-throwing
operator new.


The workaround is to set_new_handler a routine that throws bad_alloc like it should.


Bad advice.

Will break many libraries.

Use a throwing wrapper instead (note: MSVC 6 still got a problem with
instantiating a template function on a nested class, macros can help).

Jul 19 '05 #10
Alf P. Steinbach wrote:

Will break many libraries.


Haven't found it breaks anything. The majority of things are ill-prepared to deal with new
failing regardless of how the error is indicated.
Jul 19 '05 #11
Unforgiven wrote:
Kevin Goodsell wrote:
To put it another way, 'new' can never return a null pointer.

While I'd like to agree with you (and certainly do in the context of the C++
standard), for many programmers, new certainly can return a NULL pointer.
Visual C++ 6 and older have a (non-standards compliant) non-throwing
operator new.


You are absolutely right and I agree completely with the point you are
making.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #12

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

Similar topics

3
by: Jeremy Lemaire | last post by:
Hello, I am working on cross platform code that is displaying a huge memory leak when compiled on 11.00 HPUX using the aCC -AA flag. It is not leaking on NT, LINUX, Solaris, or HPUX without the...
9
by: R.Z. | last post by:
i was wondering whether it pays off in terms of memory use to maintain lots of empty deques (it would be convenient for my algorithms but memory use is more important). and does the size of a deque...
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 ? ...
4
by: mast2as | last post by:
Hi guys I wrote a short program to test a Tree class... I plan to use it a lot in my application so thought I would do a memory leak check before I start using it. The code seems good to me in a...
4
by: mast2as | last post by:
Hi again I was still debugging some code and check for memory leaks with valgrind and found out that valgrind finds a leak when i use deque<Somethingaqueue ?! I am compiling under Linux for...
9
by: Randy Yates | last post by:
Hi Folks, This may be somewhat off-topic, but it sorta fits. This is a C++ application that will be built using gcc and targeted (via the wonderful wxWidgets library) for both Windoze and...
18
by: happyvalley | last post by:
Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the...
2
by: tikcireviva | last post by:
Hi Guys, I've done a mulithread queue implementation on stl<queue>, my developement environment is on VC6 as well as FC3. Let's talks about the win32 side. The suspected memory leak is find...
1
by: katem | last post by:
Here's my problem: I have a class A that uses new to create objects of class B and push them onto a deque, like this (code stripped down for clarity): void ImageOperations::FormMask() { ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.