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

is this a memory leak?

Hello!

I was looking at the following piece of code:

void MyClass::myMethod( )
{
MyItem *item = NULL;
item = new MyItem( ..., iconView, ..., ...);
}

I thought it was necessary to delete the item before the end of
myMethod scope to avoid
memory leak.

Then I looked at the MyItem constructor:

class MyItem : public MyIconViewItem
{
public:
MyItem(..., MyIconView *parent, ..., ...);
}

Documentation for MyIconViewItem says:
When the MyIconView is deleted, all items in it are deleted
automatically.

Can I assume that the memory above is deallocated when MyIconView is
deleted?
Best regards.
Gianpaolo

Mar 29 '06 #1
10 1350
gi********@gmail.com wrote:
void MyClass::myMethod( )
{
MyItem *item = NULL;
item = new MyItem( ..., iconView, ..., ...);
}

I thought it was necessary to delete the item before the end of
myMethod scope to avoid
memory leak.

Then I looked at the MyItem constructor:

class MyItem : public MyIconViewItem
{
public:
MyItem(..., MyIconView *parent, ..., ...);
}

Documentation for MyIconViewItem says:
When the MyIconView is deleted, all items in it are deleted
automatically.

Can I assume that the memory above is deallocated when MyIconView is
deleted?


This depends on whether the item you created above is automatically
an item of 'iconView'. If it is, and the documentation does not lie, you
should be able to rely on it being deleted.

But what if you create an automatic variable of type 'MyItem'? Like
"MyItem item (..., iconView, ..., ...);"? If 'iconView' will 'delete
automatically' as it said, you will run into big trouble with this one.

I guess the above is a perfect example showing why code that
allocates objects should be responsible for deleting it. If you had code
like the following, you would have never had any doubt about what to
delete and what not:

iconView->insert_item (..., ..., ...);

Here, 'insert_item' would create the new item and thus is (as it
should be) also responsible for deleting it.

regards
--
jb

(reply address in rot13, unscramble first)
Mar 29 '06 #2
gi********@gmail.com wrote:
Hello!

I was looking at the following piece of code:

void MyClass::myMethod( )
{
MyItem *item = NULL;
item = new MyItem( ..., iconView, ..., ...);
}

I thought it was necessary to delete the item before the end of
myMethod scope to avoid
memory leak.
It is, MyItem should be deleted. Cases like this are ideal candidates
for std::auto_ptr.
Then I looked at the MyItem constructor:

class MyItem : public MyIconViewItem
{
public:
MyItem(..., MyIconView *parent, ..., ...);
}

Documentation for MyIconViewItem says:
When the MyIconView is deleted, all items in it are deleted
automatically.

Can I assume that the memory above is deallocated when MyIconView is
deleted?

How can it, if item was a local varable in the method MyClass::myMethod()?

--
Ian Collins.
Mar 29 '06 #3
gi********@gmail.com wrote:
Hello!

I was looking at the following piece of code:

void MyClass::myMethod( )
{
MyItem *item = NULL;
item = new MyItem( ..., iconView, ..., ...);
}

I thought it was necessary to delete the item before the end of
myMethod scope to avoid
memory leak.

Then I looked at the MyItem constructor:

class MyItem : public MyIconViewItem
{
public:
MyItem(..., MyIconView *parent, ..., ...);
}

Documentation for MyIconViewItem says:
When the MyIconView is deleted, all items in it are deleted
automatically.

Can I assume that the memory above is deallocated when MyIconView is
deleted?
Best regards.
Gianpaolo

If an object is created with new within any scope and not freed up with
delete before exiting that scope then yes you will leak memory.
More esoteric problems occur when multiple objects access some common
allocated object and it frees up before all the objects have no need to
access it. Hence garbage collection is such a difficult area in C++,
requiring a detailed and accurate understanding of code to avoid problems.

JB
Mar 29 '06 #4
Hi JB,
that's exactly what I've always thought: if there's a "new" somewhere
within a scope,
there has to be a "delete". But we had a discussion here, after reading
the documentation, and I finally decided to ask to the gurus.

Thanks everybody for answering me!

ciao

Mar 29 '06 #5
gi********@gmail.com wrote:
that's exactly what I've always thought: if there's a "new" somewhere
within a scope,
there has to be a "delete". But we had a discussion here, after


It is not true, tho. Take this piece of code (which I consider bad
practice, as I pointed out in my first reply):

class MyItem;

class MyIconView
{
public:
std::vector <MyItem*> items;

~MyIconView ();
};

class MyItem
{
public:
MyItem (MyIconView *parent)
{
parent->items.push_back (this);
}
};

MyIconView::~MyIconView ()
{
for (std::vector <MyItem*>::iterator i = items.begin (); i !=
items.end (); ++ i)
{
delete *i;
}
}

class MyClass
{
public:

MyIconView* iconView;

MyClass ()
{
iconView= new MyIconView;
}

~MyClass ()
{
delete iconView;
}

void myMethod ();

};

void MyClass::myMethod ()
{
MyItem* item = 0;

// ...

item = new MyItem (iconView);
}

int main ()
{
MyClass mc;

mc.myMethod ();
}
In the above code, you do *not* have any memory leak at all, but it
resembles your code very closely.

regards
--
jb

(reply address in rot13, unscramble first)
Mar 29 '06 #6
Jakob Bieling <ar****************@rot13.com> wrote:
gi********@gmail.com wrote:
that's exactly what I've always thought: if there's a "new" somewhere
within a scope,
there has to be a "delete". But we had a discussion here, after

It is not true, tho.


Allow me to rephrase :) It is not true that you have to delete in
the *same* scope. Of course, there is no question about having exactly
one delete for each new. But as you can see in the little example I put
together, this delete can be far away from your code and obviously is,
in your case.

I agree that it is unclear from the documentation whether it is
truely deleted correctly, which is why I consider it a very bad design.
But in general, this is not a memory leak. You need to check the code
you have *not* posted, if the item gets deleted or not.

regards
--
jb

(reply address in rot13, unscramble first)
Mar 29 '06 #7
Ok, Jacob, I see the point now.
Thanks a lot!

regards

Mar 29 '06 #8

Jakob Bieling wrote:
I agree that it is unclear from the documentation whether it is
truely deleted correctly, which is why I consider it a very bad design.
But in general, this is not a memory leak. You need to check the code
you have *not* posted, if the item gets deleted or not.


I checked the code carefully (the method body is not so complicated),
there's no evidence of a direct delete.

By the way, I did not expect Qt library to have such a design.

ciao :)

Mar 29 '06 #9
On Wed, 29 Mar 2006 20:37:37 +1200, Ian Collins <ia******@hotmail.com>
wrote:
gi********@gmail.com wrote:
Can I assume that the memory above is deallocated when MyIconView is
deleted?

How can it, if item was a local varable in the method MyClass::myMethod()?


The constructor will most likely pass the "this" pointer to its
parent. According to the documentation, MyIconView will delete its
items.

Not the best design, of course, but it is unfortunately often done
that way. Ideally, the item should be clonable so that what the parent
has to delete is transparent to the creators of local items. Then item
could be created locally instead of with "new".

--
Bob Hairgrove
No**********@Home.com
Mar 29 '06 #10
Jakob Bieling wrote:
gi********@gmail.com wrote:

that's exactly what I've always thought: if there's a "new" somewhere
within a scope,
there has to be a "delete". But we had a discussion here, after

It is not true, tho. Take this piece of code (which I consider bad
practice, as I pointed out in my first reply):

class MyItem;

class MyIconView
{
public:
std::vector <MyItem*> items;

~MyIconView ();
};

class MyItem
{
public:
MyItem (MyIconView *parent)
{
parent->items.push_back (this);
}
};

MyIconView::~MyIconView ()
{
for (std::vector <MyItem*>::iterator i = items.begin (); i !=
items.end (); ++ i)
{
delete *i;
}
}

class MyClass
{
public:

MyIconView* iconView;

MyClass ()
{
iconView= new MyIconView;
}

~MyClass ()
{
delete iconView;
}

void myMethod ();

};

void MyClass::myMethod ()
{
MyItem* item = 0;

// ...

item = new MyItem (iconView);
}

int main ()
{
MyClass mc;

mc.myMethod ();
}
In the above code, you do *not* have any memory leak at all, but it
resembles your code very closely.

regards

Agreed, but to use 2 different scopes in this manner is not just bad
practice, IMHO it's completely insane. Unless of course you're
determined to write code that is next to impossible to understand,
develop or maintain. Hence the following:

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!

WARNING : The above memory management technique should be avoided.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!

JB
Mar 29 '06 #11

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...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.