473,785 Members | 2,829 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

After calling constructor, the destructor is called immediately - why?

Hello,

I want to use an object (LowCut) within another object (SampleRateConv erter)
like it is written as follows:

class SampleRateConve rter
{
public:
SampleRateConve rter( int iSourceSampleRa te, int iTargetSampleRa te )
{
LowCut = LowPassFilter(d Cutoff, 512, BLACKMAN); // here
debugger calls first constructor and then destructor ~LowPassFilter
}

private:
LowPassFilter LowCut;

void doSomething();
};

I used the debugger and realized:
After calling the constructor for LowPassFilter within SampleRateConve rter,
the destructor of LowPassFilter is called automatically.

That means, the Filter becomes destroyed immediately. So, I cannot use it in
the method doSomething. The Filter allocates a buffer in memory for storing
information.

But using LowPassFilter *LowCut; (pointer) (instead of the version written
above) and creating the Filter with new-operator (new LowPassFilter(. .)) it
works?
Then I have to delete this Filter manually by calling delete LowCut.

But why I cannot use the first version? Why does the runtime environment
destroys the Filter object immediately after generating it?
Anything wrong due to OOP design issues?

Kind regards, Michael


Jan 5 '06 #1
4 4972

Michael wrote:
Hello,

I want to use an object (LowCut) within another object (SampleRateConv erter)
like it is written as follows:

class SampleRateConve rter
{
public:
SampleRateConve rter( int iSourceSampleRa te, int iTargetSampleRa te )
{
LowCut = LowPassFilter(d Cutoff, 512, BLACKMAN); // here
debugger calls first constructor and then destructor ~LowPassFilter
}

private:
LowPassFilter LowCut;

void doSomething();
};

I used the debugger and realized:
After calling the constructor for LowPassFilter within SampleRateConve rter,
the destructor of LowPassFilter is called automatically.

That means, the Filter becomes destroyed immediately. So, I cannot use it in
the method doSomething. The Filter allocates a buffer in memory for storing
information.

But using LowPassFilter *LowCut; (pointer) (instead of the version written
above) and creating the Filter with new-operator (new LowPassFilter(. .)) it
works?
Then I have to delete this Filter manually by calling delete LowCut.

But why I cannot use the first version? Why does the runtime environment
destroys the Filter object immediately after generating it?
Anything wrong due to OOP design issues?

Kind regards, Michael


Try using an initialization list instead:

class SampleRateConve rter
{
public:
SampleRateConve rter( int iSourceSampleRa te, int iTargetSampleRa te
)
: LowCut( LowPassFilter(d Cutoff, 512, BLACKMAN) )
{}
// ...
};

See the FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-10.6

Cheers! --M

Jan 5 '06 #2
On 2006-01-05 14:21:58 -0500, "Michael" <fe**@hrz.tu-chemnitz.de> said:
Hello,

I want to use an object (LowCut) within another object
(SampleRateConv erter) like it is written as follows:

class SampleRateConve rter
{
public:
SampleRateConve rter( int iSourceSampleRa te, int iTargetSampleRa te )
{
LowCut = LowPassFilter(d Cutoff, 512, BLACKMAN); // here
debugger calls first constructor and then destructor
That is because you are creating a temporary object with the
expression (LowPassFilter( dCutoff, 512, BLACKMAN)). This temporary is
then copied via assignment to the instance variable LowCut, and is then
destroyed (hence the call to the destructor).
}

private:
LowPassFilter LowCut;

void doSomething();
};
Try this (this avoids the creation of a temporary object, and
initializes the member variable directly):

class SampleRateConve rter
{
public:
SampleRateConve rter( int iSourceSampleRa te, int iTargetSampleRa te )
: LowCut(dCutoff, 512, BLACKMAN)
{}

private:
LowPassFilter LowCut;

void doSomething();
};

I used the debugger and realized:
After calling the constructor for LowPassFilter within
SampleRateConve rter, the destructor of LowPassFilter is called
automatically.

That means, the Filter becomes destroyed immediately. So, I cannot use
it in the method doSomething. The Filter allocates a buffer in memory
for storing information.


this suggests to me that you haven't properly implemented an assignment
operator (or, odds are, copy constructor) for the class LowPassFilter.

--
Clark S. Cox, III
cl*******@gmail .com

Jan 5 '06 #3
You are creating a temporary LowPassFilter object who's scope is only the
assignment operation. If LowPassFilter has a proper copy constructor than
your code should work but isn't optimal because you are creating two LowPassFilters
instead of one. In fact, you are creating three because one more is created
upon initialization of the object (by LowPassFilter's default constructor).
mlimber's advice to use the initialization list saves you the default instance
but still creates a temporary and copies it to your member variable. The
correct way to do this is like so:

class SampleRateConve rter
{
private:
LowPassFilter LowCut;

public:
SampleRateConve rter( int iSourceSampleRa te, int iTargetSampleRa te ) :
LowCut(dCutoff, 512, BLACKMAN)
{
// ...
}

void doSomething() { /* work with LowCut */ }
};

Ales

Hello,

I want to use an object (LowCut) within another object
(SampleRateConv erter) like it is written as follows:

class SampleRateConve rter
{
public:
SampleRateConve rter( int iSourceSampleRa te, int
iTargetSampleRa te )
{
LowCut = LowPassFilter(d Cutoff, 512, BLACKMAN); // here
debugger calls first constructor and then destructor ~LowPassFilter
}
private:
LowPassFilter LowCut;
void doSomething();
};
I used the debugger and realized:
After calling the constructor for LowPassFilter within
SampleRateConve rter,
the destructor of LowPassFilter is called automatically.
That means, the Filter becomes destroyed immediately. So, I cannot use
it in the method doSomething. The Filter allocates a buffer in memory
for storing information.

But using LowPassFilter *LowCut; (pointer) (instead of the version
written
above) and creating the Filter with new-operator (new
LowPassFilter(. .)) it
works?
Then I have to delete this Filter manually by calling delete LowCut.
But why I cannot use the first version? Why does the runtime
environment
destroys the Filter object immediately after generating it?
Anything wrong due to OOP design issues?
Kind regards, Michael

Jan 5 '06 #4
"Michael" <fe**@hrz.tu-chemnitz.de> wrote in
news:dp******** **@anderson.hrz .tu-chemnitz.de:
Hello,

I want to use an object (LowCut) within another object
(SampleRateConv erter) like it is written as follows:

class SampleRateConve rter
{
public:
SampleRateConve rter( int iSourceSampleRa te, int
iTargetSampleRa te ) {
LowCut = LowPassFilter(d Cutoff, 512, BLACKMAN); // here
debugger calls first constructor and then destructor ~LowPassFilter
}

private:
LowPassFilter LowCut;

void doSomething();
};

I used the debugger and realized:
After calling the constructor for LowPassFilter within
SampleRateConve rter, the destructor of LowPassFilter is called
automatically.
From a pessimistic compiler, you may have 2 constructors, an assignment,
and a destructor called:

- LowCut is default constructed (at the time that SampleRateConve rter
is...)
- A temporary LowPassFilter is constructed for the purposes of the
assignment
- As assignment between the temporary and LowCut is performed
- The temporary LowPassFilter is destroyed.
That means, the Filter becomes destroyed immediately. So, I cannot use
it in the method doSomething. The Filter allocates a buffer in memory
for storing information.
_A_ Filter (I presume you really mean LowPassFilter) is destroyed.
There's two of them in there.... since Filter is allocating a buffer, you
may not have correctly implemented your assignment operator.
But using LowPassFilter *LowCut; (pointer) (instead of the version
written above) and creating the Filter with new-operator (new
LowPassFilter(. .)) it works?
Then I have to delete this Filter manually by calling delete LowCut.
Sure, it would work. But as you point out, you'll have to manage the
lifetime of the pointer.
But why I cannot use the first version? Why does the runtime
environment destroys the Filter object immediately after generating
it? Anything wrong due to OOP design issues?


Likely because you've violated the "Rule of three": "If a class needs a
destructor, or a copy constructor, or an assignment operator, it needs
them all." What's probably happening is that during the assignment, it's
only copying the pointer that you allocated to in the LowPassFilter.
When the temporary goes out of scope, it probably deletes the memory that
it holds. As a result, you still have a dangling pointer stored inside
of LowCut. When you try to do something with it later on, you get
Undefined Behaviour (it may crash, it may not... luck of the draw...)
Jan 5 '06 #5

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

Similar topics

7
13229
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
6
3019
by: giles | last post by:
Hi, Does a compiler generated constructor initialise the data members? Giles
23
5184
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
3
1744
by: rahul8143 | last post by:
hello, I write a following program and have problem in understanding constructors and destructors. #include <iostream.h> class vector { public: double x; double y;
6
2702
by: Fred Zwarts | last post by:
Hello, I am trying to debug some complex debug code. In order to track the use of dynamically allocated memory, I replaced the standard global new and delete operators. (Not for changing the memory allocation algorithm, but for gathering some statistics and to find memory leaks.) This seems to work. However, I noticed that my replacing delete operator is not called
11
1964
by: Lloyd Dupont | last post by:
(not I use 2.0, so new return a "normal" pointer and gcnew return a managed one, my question below regarding new concern plain standart C++ allocator) - if I use the default new operator, are all the instance variable initialize to NULL / 0 ? - if there is not enough memory what happend with new ? does it return NULL or throw an exception? - if new throw a native C++ exception what happen in Managed C++ ?! - if there is an exception in...
8
2228
by: Kannan | last post by:
Some amount of memory is allocated inside the Base Constructor using new. During the construction of a derived object an exception occurred in the constructor of the derived class. Will the memory get de allocated which got allocated in Base class constructor? Will the Base class destructor get called? class Base { int *p;
1
8557
by: newbie | last post by:
This is a snippet from C++ FAQs, which I have never done--- when I do such a thing, I would declare function used by constructor ( in this example, init() ) as static. But I do understand that it would be great if the following practice is valid. But by OO concept, it seems not to very good because init() belongs to an object, which hasn't be constructed. Can you give any comment on my question?
4
2217
by: raj s | last post by:
Will the below code cause memory leak in c++ class base{ int a; int *pint; someclass objsomeclass; someclass* psomeclass; base(){ objsomeclass = someclass(); psomeclass = new someclass();
0
9645
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
10327
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...
0
10151
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8973
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6740
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
5381
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2879
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.