473,503 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Basic question about destroying objects.

Hello,

I have a class Adjazenzmatrix with the following method and destructor:

private:
Adjazenzmatrix *wegmatrix;
Adjazenzmatrix *distanzmatrix;
..
..
..
Adjazenzmatrix::~Adjazenzmatrix() {
delete wegmatrix;
delete distanzmatrix;
}
Oct 28 '07 #1
7 1663
Markus Pitha wrote:
Hello,

I have a class Adjazenzmatrix with the following method and destructor:

private:
Adjazenzmatrix *wegmatrix;
Adjazenzmatrix *distanzmatrix;
.
.
.
Adjazenzmatrix::~Adjazenzmatrix() {
delete wegmatrix;
delete distanzmatrix;
}
.
.

void Adjazenzmatrix::berechneEigenschaften() {
wegmatrix = new Adjazenzmatrix();
distanzmatrix = new Adjazenzmatrix();
.
.
.
}

Did I understand it right, that every time when I execute
berechneEigenschaften() wegmatrix and distanzmatrix produce a memory
leak? Do I have to call "delete" already in berechneEigenschaften() or
will these objects become destroyed after berechneEigenschaften()'s
execution is finished?

Correct! Everytime adjazenzmatrix::berechneEigenschaften is invoked it
gives rise to a potential memory leak. Objects that are allocated
throught the operator new will NOT be automatically reclaimed (unless
you are running on a platforms that performs automatic garbage collection.)

There is no particular reason why the inner member objects should be
dynamically allocated. It would be much easier and simpler to just write

class Adjazenzmatrix
{

Adjazenzmatrix wegmatrix;
Adjazenzmatrix distanzmatrix;

// ...
};
>

Markus
Regards,
Ben
Oct 28 '07 #2
Hello,

understood, thanks for your answer.

Markus
Oct 28 '07 #3
Hello,

James Kanze wrote:
Objects allocated with new are only destroyed by delete. If you
only need them in berechneEigenschaften, then they should
probably be local variables of the function, not members at all.
Actually I also need it out of this method and I'm not sure at the
moment why I used them as pointers. When my program is finished, I will
examine my code more closely again.
Markus

Oct 28 '07 #4
Markus Pitha wrote:
Hello,

I have a class Adjazenzmatrix with the following method and destructor:

private:
Adjazenzmatrix *wegmatrix;
Adjazenzmatrix *distanzmatrix;
.
.
.
Adjazenzmatrix::~Adjazenzmatrix() {
delete wegmatrix;
delete distanzmatrix;
}
.
.

void Adjazenzmatrix::berechneEigenschaften() {
wegmatrix = new Adjazenzmatrix();
distanzmatrix = new Adjazenzmatrix();
.
.
.
}

Did I understand it right, that every time when I execute
berechneEigenschaften() wegmatrix and distanzmatrix produce a memory
leak?
Looks like it.
Do I have to call "delete" already in berechneEigenschaften()
That will leave the two member variables dangling.
or
will these objects become destroyed after berechneEigenschaften()'s
execution is finished?
They will not be destroyed.
Your design looks very fishy. Why does a matrix have two pointers to
matrices as members? Effectively, you are defining a binary tree of
matrices here (although that is not reflected in the name of the class).
That just doesn't look right.

What are you trying to do? I have the feeling that you might be stuck
because you took a wrong turn a while ago and now you look for local
remedies to a global problem.
Best

Kai-Uwe Bux
Oct 28 '07 #5
Hello,

Kai-Uwe Bux wrote:
Your design looks very fishy. Why does a matrix have two pointers to
matrices as members? Effectively, you are defining a binary tree of
matrices here (although that is not reflected in the name of the class).
That just doesn't look right.

Yes, in the beginning I made some design mistakes.I thought about a MVC
concept, but now it became something different. I have all data
management in Adjazenzmatrix as well as every calculations. My
controller prepares the data from Adjazenzmatrix for the GUI and the GUI
class displays it.
And you are right. The name of the class "Adjazenzmatrix" is misleading
now. I will rename it after my program is finished.
Markus
Oct 28 '07 #6
On Oct 28, 12:39 pm, Markus Pitha <newsgroupsNOS...@pithax.netwrote:
James Kanze wrote:
Objects allocated with new are only destroyed by delete. If you
only need them in berechneEigenschaften, then they should
probably be local variables of the function, not members at all.
Actually I also need it out of this method and I'm not sure at the
moment why I used them as pointers. When my program is finished, I will
examine my code more closely again.
I fear that that (and your response to Kai-Uwe) have it
backwards. These are things you have to do before even starting
to write the code. Define carefully what the class is to do,
and don't write code until you have that done.

As Kai-Uwe pointed out, if a class X has pointers to other class
X, then you have effectively defined a graph. Which may not be
what you meant to do.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 28 '07 #7
benben wrote:
Markus Pitha wrote:
>Hello,

I have a class Adjazenzmatrix with the following method and destructor:

private:
Adjazenzmatrix *wegmatrix;
Adjazenzmatrix *distanzmatrix;
.
.
.
Adjazenzmatrix::~Adjazenzmatrix() {
delete wegmatrix;
delete distanzmatrix;
}
.
.

void Adjazenzmatrix::berechneEigenschaften() {
wegmatrix = new Adjazenzmatrix();
distanzmatrix = new Adjazenzmatrix();
.
.
.
}

Did I understand it right, that every time when I execute
berechneEigenschaften() wegmatrix and distanzmatrix produce a memory
leak? Do I have to call "delete" already in berechneEigenschaften() or
will these objects become destroyed after berechneEigenschaften()'s
execution is finished?


Correct! Everytime adjazenzmatrix::berechneEigenschaften is invoked it
gives rise to a potential memory leak. Objects that are allocated
throught the operator new will NOT be automatically reclaimed (unless
you are running on a platforms that performs automatic garbage collection.)

There is no particular reason why the inner member objects should be
dynamically allocated. It would be much easier and simpler to just write

class Adjazenzmatrix
{

Adjazenzmatrix wegmatrix;
Adjazenzmatrix distanzmatrix;

// ...
};
Oops! I didn't realise the members are of the same type as the class
being defined. My bad! The above will simply not compile.

>

>>

Markus

Regards,
Ben
Regards,
Ben
Oct 29 '07 #8

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

Similar topics

1
1251
by: ibm | last post by:
I am running a simple test /*******CODE************/ class A { int _x; public: A(int i) { _x = i;
9
2016
by: Alvin Bruney [MVP] | last post by:
with the using construct, exit from scope calls dispose on said object. But what happens in a connection pooling scenario? Is the run-time smart enough to realize that Object Pooling is being used...
4
1895
by: pachanga | last post by:
After you destroy a Object and its send to the garbage collections, can you retrieve the object back? Also, if you can, can you destroy an object permantly with no trace of it?
2
1326
by: jaymtz78 | last post by:
Hi, I have a huge windows application that I'm working on and I'm completely baffled. Sometimes when I try to close the application, it won't let me! I have an Exit button in my menu bar that...
16
4529
by: LP | last post by:
Hello, I am trying to use .NET with Excel. I installed Office 2003 and selected ..NET programming suport option, so it installed all those PIA, as MS sugests. But I can not find a way to destroy...
6
1299
by: pinorama123 | last post by:
I have an ASP.NET application that contains a few classes that I have built. One of my objects is a user object. I have a pretty basic question about how this would work. If I have multiple...
4
2685
by: Olumide | last post by:
Hello - I have two classes A and B as follows: class B{ public: ~B(){ cout << "destroying B" << endl; } }; class A{
3
2264
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
41
1837
by: =?Utf-8?B?VGltIE1hcnNkZW4=?= | last post by:
Hi, I am after suggestions on the best practice declaring and destroying objects. some example code: Private Sub MySub Dim frmMyForm As MyForm Try
0
7205
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,...
0
7093
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
7287
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
7348
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...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4685
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...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.