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

Destructor not called

Hello all,

I am writing a kind of encapsulation of SQL statements in C++ as a
class. I am encountering problems that the destructor of my class
never gets called.

The class should be used as a kind of hierarchical representation of
the different SQL operations such as AND, OR and possible values.

I will post some abbreviated / shortened / simplified (partiyll
pseude) code here.

#include <stdio.h>
#include <stdlib.h>

class SQL
{
public:
SQL(int someOperator); // lets say 0=AND, 1=OR
SQL(char * someString); // lets say we only now AND, OR and
strings
~SQL();

SQL & operator & (SQL & param);

// public for testing purposes
int type; // lets say 0=Operator, 1=String
int value_operator;
char * value_string;
};
SQL::~SQL()
{
printf("I am the destructor\n");
};
SQL::SQL(int someOperator)
{
printf("I am the constructor for operators, my type is %i\n",
someOperator);

type = 0;
value_operator = someOperator;
};
SQL::SQL(char * someString)
{
printf("I am the constructor for string values, my value is
\"%s\"\n", someString);

type = 1;
value_string = someString;
};
SQL & SQL::operator & (SQL & param)
{
return * new SQL( 1 );
};
void showType( SQL & sql )
{
printf("TYPE: %i\n", sql.type);
};


int main()
{
SQL test1("Test 1");

showType( test1 );

return 0;
};
=============> outputs

I am the constructor for string values, my value is "Test 1"
TYPE: 1
I am the destructor

int main()
{
showType( * new SQL("Test 2") & * new SQL("Test 3") );

return 0;
};
=============> outputs

I am the constructor for string values, my value is "Test 3"
I am the constructor for string values, my value is "Test 2"
I am the constructor for operators, my type is 1
TYPE: 0



I cannot explain why there are constructor calls for the three
instances of SQL, but no destructor calls.

Any help would be appreciated.
Thanks
Manuel
Jul 22 '05 #1
5 1715

"manitu" <ma************@manitu.de> wrote in message
news:62**************************@posting.google.c om...
Hello all,

I am writing a kind of encapsulation of SQL statements in C++ as a
class. I am encountering problems that the destructor of my class
never gets called.

The class should be used as a kind of hierarchical representation of
the different SQL operations such as AND, OR and possible values.

I will post some abbreviated / shortened / simplified (partiyll
pseude) code here.

#include <stdio.h>
#include <stdlib.h>

class SQL
{
public:
SQL(int someOperator); // lets say 0=AND, 1=OR
SQL(char * someString); // lets say we only now AND, OR and
strings
~SQL();

SQL & operator & (SQL & param);

// public for testing purposes
int type; // lets say 0=Operator, 1=String
int value_operator;
char * value_string;
};
SQL::~SQL()
{
printf("I am the destructor\n");
};
SQL::SQL(int someOperator)
{
printf("I am the constructor for operators, my type is %i\n",
someOperator);

type = 0;
value_operator = someOperator;
};
SQL::SQL(char * someString)
{
printf("I am the constructor for string values, my value is
\"%s\"\n", someString);

type = 1;
value_string = someString;
};
SQL & SQL::operator & (SQL & param)
{
return * new SQL( 1 );
};
void showType( SQL & sql )
{
printf("TYPE: %i\n", sql.type);
};


int main()
{
SQL test1("Test 1");

showType( test1 );

return 0;
};
=============> outputs

I am the constructor for string values, my value is "Test 1"
TYPE: 1
I am the destructor

int main()
{
showType( * new SQL("Test 2") & * new SQL("Test 3") );

return 0;
};
=============> outputs

I am the constructor for string values, my value is "Test 3"
I am the constructor for string values, my value is "Test 2"
I am the constructor for operators, my type is 1
TYPE: 0



I cannot explain why there are constructor calls for the three
instances of SQL, but no destructor calls.


In your second 'main()' example, since you dynamically allocated
your objects but never deleted them, they were never 'officially'
destroyed. So the dtors were not invoked. (The ones in the first
'main()' were because the objects had automatic storage duration).

And the way you've written your second 'main()', the objects cannot
be deleted, since you've left no way to determine their addresses
(which are needed as the argument to delete). Your code has a 'memory
leak'. The absence of the dtor calls serves as a warning of this in
this case.
-Mike
Jul 22 '05 #2
On 29 Oct 2004 09:02:59 -0700 in comp.lang.c++, ma************@manitu.de
(manitu) wrote,
I cannot explain why there are constructor calls for the three
instances of SQL, but no destructor calls.


You allocated them with new, but then you did not delete them.
For every new, you should write a corresponding delete.
Jul 22 '05 #3
"manitu" <ma************@manitu.de> wrote in message news:62**************************@posting.google.c om...
Hello all,

I am writing a kind of encapsulation of SQL statements in C++ as a
class. I am encountering problems that the destructor of my class
never gets called.

The class should be used as a kind of hierarchical representation of
the different SQL operations such as AND, OR and possible values.

I will post some abbreviated / shortened / simplified (partiyll
pseude) code here.

#include <stdio.h>
#include <stdlib.h>

class SQL
{
public:
SQL(int someOperator); // lets say 0=AND, 1=OR
SQL(char * someString); // lets say we only now AND, OR and
strings
~SQL();

SQL & operator & (SQL & param);

// public for testing purposes
int type; // lets say 0=Operator, 1=String
int value_operator;
char * value_string;
};
SQL::~SQL()
{
printf("I am the destructor\n");
};
SQL::SQL(int someOperator)
{
printf("I am the constructor for operators, my type is %i\n",
someOperator);

type = 0;
value_operator = someOperator;
};
SQL::SQL(char * someString)
{
printf("I am the constructor for string values, my value is
\"%s\"\n", someString);

type = 1;
value_string = someString;
};
SQL & SQL::operator & (SQL & param)
{
return * new SQL( 1 );
};
void showType( SQL & sql )
{
printf("TYPE: %i\n", sql.type);
};


int main()
{
SQL test1("Test 1");

showType( test1 );

return 0;
};
=============> outputs

I am the constructor for string values, my value is "Test 1"
TYPE: 1
I am the destructor

int main()
{
showType( * new SQL("Test 2") & * new SQL("Test 3") );

return 0;
};
=============> outputs

I am the constructor for string values, my value is "Test 3"
I am the constructor for string values, my value is "Test 2"
I am the constructor for operators, my type is 1
TYPE: 0



I cannot explain why there are constructor calls for the three
instances of SQL, but no destructor calls.

Any help would be appreciated.
Thanks
Manuel


I would recommend a basic C++ reference.
You need to understand the difference between
dynamic allocation and other kinds before you
use C++ in any serious way. To put the present
puzzle simply, it is up to the programmer to get
new and delete operations to match up. You
have not done so.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Jul 22 '05 #4

"manitu" <ma************@manitu.de> wrote in message
news:62**************************@posting.google.c om...
Hello all,

I am writing a kind of encapsulation of SQL statements in C++ as a
class. I am encountering problems that the destructor of my class
never gets called.

The class should be used as a kind of hierarchical representation of
the different SQL operations such as AND, OR and possible values.

I will post some abbreviated / shortened / simplified (partiyll
pseude) code here.

int main()
{
showType( * new SQL("Test 2") & * new SQL("Test 3") );

return 0;
};
=============> outputs

I am the constructor for string values, my value is "Test 3"
I am the constructor for string values, my value is "Test 2"
I am the constructor for operators, my type is 1
TYPE: 0


Just rewrite like this

showType( SQL("Test 2") & SQL("Test 3") );

No calls to new. But this will only work if you rewrite operator& in the way
that you should have in the first place

SQL operator & (const SQL & param) const;

Not the addition of const and that operator& no longer returns a reference.

These changes will problably require some other changes that should have
been written in the first place, for instance a copy constructor and an
assignment operator and proper const correctness on other methods.




I cannot explain why there are constructor calls for the three
instances of SQL, but no destructor calls.


Because you use new but never called delete. But in the example above you
didn't need to use new at all.

You're not a Java programmer are you? In any case you need a book to teach
you how to program C++ properly, you are going down so many wrong paths
simultaneously that it not surprising you are in a mess. I would recommend
Effective C++ by Scott Meyers.

john
Jul 22 '05 #5
>
Just rewrite like this

showType( SQL("Test 2") & SQL("Test 3") );

No calls to new. But this will only work if you rewrite operator& in the way that you should have in the first place

SQL operator & (const SQL & param) const;

Not the addition of const and that operator& no longer returns a reference.


Oh and showType should be rewritten like this

void showType( const SQL & sql )

Again note the addition of const.

John
Jul 22 '05 #6

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

Similar topics

52
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object...
9
by: sahukar praveen | last post by:
Hello, This is the program that I am trying. The program executes but does not give me a desired output. ********************************************** #include <iostream.h> #include...
11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
16
by: Timothy Madden | last post by:
Hy I have destructors that do some functional work in the program flow. The problem is destructors should only be used for clean-up, because exceptions might rise at any time, and destructors...
6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
4
by: Joe | last post by:
I am looking for the quintessential blueprint for how a C++ like destructor should be implemented in C#. I see all kinds of articles in print and on the web, but I see lots of discrepencies. For...
35
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
14
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
5
by: junw2000 | last post by:
I use the code below to study delete and destructor. #include <iostream> using namespace std; struct A { virtual ~A() { cout << "~A()" << endl; }; //LINE1 void operator delete(void* p) {...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...

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.