472,373 Members | 1,552 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,373 software developers and data experts.

Memory Leak in this simple C++ program, a compiler bug ?

Hi,
I have this following simple c++ program, it will produce memory leak
( see what I did below ).
My observation also showed that:
There will be a mem leak when all the 3 conditions are true:
1. CallControlData does not provide a operator=
2. INUserHandle's operator= returns a object instead of a reference
3. Use CC -g main_leak.C instead of CC main_leak.C to compile.

You change any of the above condition, there will be NO mem leak.
The CC I used is Sun's /opt/SUNWspro/SC4.2/bin/CC.

If I use g++, and if all the above 3 conditions are true, there will be
memory leak. Besides that, even we
don't use g++ -g option, there will still be memory leak.

Can somebody explain this ?

Source of my main_leak.C is:
#include <iostream.h>
#define NULL 0
const int Size = 10;
short printlevel = 0;

class HandleBase
{
public:
int i;
};

class INUserHandle
{
public:
INUserHandle()
{
cout << "Entering INUserHandle()" << endl;
pchar = new char[Size];
impl = new HandleBase;
cout << "this = "<< this << endl;
cout << "Leaving INUserHandle()" << endl;
}
INUserHandle(const INUserHandle& handle)
:pchar ( NULL)
, impl( NULL )
{
cout << "Entering INUserHandle(constINUserHandle&)" << endl;
pchar = new char[Size];
impl = new HandleBase(*handle.impl);
for ( int i = 0; i < Size ; i++ )
{
pchar[i] = handle.pchar[i];
}
cout << "this = "<< this << endl;
cout << "Leaving INUserHandle(constINUserHandle&)" << endl;
}

// Note here, that one is returning obj, one is returning reference.
//INUserHandle& operator=(const INUserHandle& handle)
INUserHandle operator=(const INUserHandle& handle)
{
cout << "Entering INUserHandle::operator= "<< endl;
if( this != &handle ) {
delete [] pchar;
pchar = new char[Size];

delete impl;
impl = new HandleBase(*handle.impl);
for ( int i = 0; i < Size ; i++ )
{
pchar[i] = handle.pchar[i];
}
}
cout << "Leaving INUserHandle::operator= "<< endl;
return *this;
}

~INUserHandle()
{
cout << "Entering ~INUserHandle()." << endl;
delete [] pchar ;
delete impl;
cout << "this = "<< this << endl;
cout << "Leaving ~INUserHandle()." << endl;
}

public:
char *pchar;
HandleBase *impl;
};

class CallControlData {
public:
public:
INUserHandle handle;

};

class CallControlData_assignment {
public:

CallControlData_assignment& operator=(const
CallControlData_assignment& cdata_ass)
{
cout << "Entering CallControlData_assignment::opertor= " <<
endl;
handle = cdata_ass.handle;

cout << "Leaving CallControlData_assignment::opertor= " <<
endl;
return *this;
}
public:
INUserHandle handle;

};
void f()
{
CallControlData cdata1;
CallControlData cdata2;
cdata1 = cdata2;

//CallControlData_assignment cdata_ass1;
//CallControlData_assignment cdata_ass2;
//cdata_ass1 = cdata_ass2;
};

void g()
{
f();
};

int
main()
{
g();
//while ( 1 ) {int i;};
return 1;
}



What I did:
1. compile it.
2. run dbx a.out
3. in dbx prompt, type check -memuse
4. in dbx prompt, type run

$>/opt/SUNWspro/SC4.2/bin/CC -g main_leak.C
$>
$>dbx a.out
The major new features of this release relative to 3.2 are:

o The Collector now supports MT applications (see `help collectormt')
o Runtime checking (RTC) is supported with fork/exec/attach (see `help
rtc attach' for details)
o RTC has an API for allocators (see `help rtc api')
o New command `regs' to print current value of registers (see `help regs')
o New command `showblock' to give details about heap block (see `help
showblock')
o Enhanced `pathmap' command (see `help pathmap')
o New dbxenv variable 'language_mode' (see `help dbxenv' under
`language_mode')
o New dbxenv variable 'output_inherited_members' (ee `help dbxenv' under
`output_inherited_members')
o Two new dbx read-only variables: $helpfile and $helpfile_html (see `help
help')
o New -v (verbose) flag to the `module' and `modules' commands
(see `help module' and `help modules')
o New +r flag to print and display commands (see `help print')
o Default value of dbxenv variable `scope_look_aside' has been changed
to on (see `help dbxenv' under scope_look_aside and `help scope')
o New `inobject' event for support of `stop inobject <c++_obj_exp>'
(see `help c++ inobject' and `help event specification')
o `inclass' event now supports template classes (see `help c++ inclass' and
`help event specification')
o `stop at <lineno>' now supports C++ template definitions as well as
C++ inlined functions defined in header files (see `help event
specification'
under `at' event)
o Support for new C++ operators: `const_cast', `dynamic_cast',
`reinterpret_cast', and `static_cast'
o Support for new C++ `typeid' operator
o An HTML version of the help file is available (see `help help')

See also `help changes32'
The major new features of 3.2 (SPARCWorks 3.1) relative to 3.1 are:

o Objective C support (see `help ObjC')
o Fortran 90 support (see `help fortran')
o Runtime checking (RTC) gives information about memory usage
Reading symbolic information for a.out
Reading symbolic information for rtld /usr/lib/ld.so.1
Reading symbolic information for libm.so.1
Reading symbolic information for libC.so.5
Reading symbolic information for libw.so.1
Reading symbolic information for libc.so.1
Reading symbolic information for libdl.so.1
Reading symbolic information for libc_psr.so.1
(dbx)
(dbx)
(dbx) check -memuse
memuse checking - ON
(dbx) run
Running: a.out
(process id 21043)
Reading symbolic information for librtc.so
Skipping libm.so.1, already read
Skipping libC.so.5, already read
Skipping libw.so.1, already read
Skipping libc.so.1, already read
Skipping libdl.so.1, already read
Skipping libc_psr.so.1, already read
Enabling Error Checking... done
Entering INUserHandle()
this = 0xeffff314
Leaving INUserHandle()
Entering INUserHandle()
this = 0xeffff30c
Leaving INUserHandle()
Entering INUserHandle::operator=
Leaving INUserHandle::operator=
Entering INUserHandle(constINUserHandle&)
this = 0xeffff29c
Leaving INUserHandle(constINUserHandle&)
Entering ~INUserHandle().
this = 0xeffff30c
Leaving ~INUserHandle().
Entering ~INUserHandle().
this = 0xeffff314
Leaving ~INUserHandle().
Checking for memory leaks...

Actual leaks report (actual leaks: 2 total size: 14 bytes)

Total Num of Leaked Allocation call stack
Size Blocks Block
Address
====== ====== ========== =======================================
10 1 0x22848 operator new < INUserHandle::INUserHandle <
INUserHandle::operator = < CallControlData::operator = < f < g < main
4 1 0x23238 operator new < INUserHandle::INUserHandle <
INUserHandle::operator = < CallControlData::operator = < f < g < main
Possible leaks report (possible leaks: 0 total size: 0 bytes)
Checking for memory use...

Blocks in use report (blocks in use: 0 total size: 0 bytes)

execution completed, exit code is 1
(dbx) quit
$>
Jul 22 '05 #1
6 2469
Scott Niu wrote:
Hi,
I have this following simple c++ program, it will produce memory leak
( see what I did below ).
My observation also showed that:
There will be a mem leak when all the 3 conditions are true:
1. CallControlData does not provide a operator=
2. INUserHandle's operator= returns a object instead of a reference
3. Use CC -g main_leak.C instead of CC main_leak.C to compile.


Hi Scott,

I didn't get into the details of your program. But I can say that
operator = must return a reference. Concerning the different
compiler flags, the reason might be that without -g dbx just does
not detect the memory leak. But the details of this are offtopic
here as memory leak detection is certainly not defined in the
standard. You might want to refer to Sun's CC Forum concerning
this. I don't know which version of Sun's C++ compiler you are
using, but it seems to me to be an obsolete one. So upgrading might
be an option...
Cheers,

Tom
Jul 22 '05 #2
Scott Niu wrote:
Hi,
I have this following simple c++ program, it will produce memory leak
( see what I did below ).
My observation also showed that:
There will be a mem leak when all the 3 conditions are true:
1. CallControlData does not provide a operator=
2. INUserHandle's operator= returns a object instead of a reference
3. Use CC -g main_leak.C instead of CC main_leak.C to compile.


Hi Scott,

I didn't get into the details of your program. But I can say that
operator = must return a reference. Concerning the different
compiler flags, the reason might be that without -g dbx just does
not detect the memory leak. But the details of this are offtopic
here as memory leak detection is certainly not defined in the
standard. You might want to refer to Sun's CC Forum concerning
this. I don't know which version of Sun's C++ compiler you are
using, but it seems to me to be an obsolete one. So upgrading might
be an option...
Cheers,

Tom
Jul 22 '05 #3
"Scott Niu" <sc****@qd.lucent.com> wrote:
Hi,
I have this following simple c++ program, it will produce memory leak

#include <iostream.h>
Error, nonstandard header
#define NULL 0
Error, redefinition of NULL

Change those 2 lines to:
#include <iostream>
using namespace std;
void f()
{ [snip] };
Syntax error - extra semicolon
void g()
{
f();
};


Same again

Fix those problems and then see if you still get the problem.
Could it be that this 'dbx' program is just being paranoid?

NB. If you are more interested in solving your problem than
in finding out what happened, modify your code to use
automatic memory management (eg. use std::string for pchar
and std::auto_ptr for impl).

Also, your objects are not exception-safe.
Jul 22 '05 #4
On 8 Nov 2004 12:35:14 -0800, ol*****@inspire.net.nz (Old Wolf) wrote:
"Scott Niu" <sc****@qd.lucent.com> wrote:
void f()
{

[snip]
};


Syntax error - extra semicolon


That's not a syntax error. While it's technically against style for
putting in blank or no-op statements in C++, it compiles correctly.
Jul 22 '05 #5
"Scott Niu" <sc****@qd.lucent.com> wrote in message news:<cm********@netnews.proxy.lucent.com>...
I have this following simple c++ program, it will produce memory leak
( see what I did below ). [snip] Can somebody explain this ?

Source of my main_leak.C is: [snips] class CallControlData {
public:
public:
INUserHandle handle;

}; [snips] CallControlData cdata1;
CallControlData cdata2;
cdata1 = cdata2;

//CallControlData_assignment cdata_ass1;
//CallControlData_assignment cdata_ass2;
//cdata_ass1 = cdata_ass2;
};

[snipper]

Note that the class CallControlData does not have an explicit op=
in it. So it will do the default shallow copy. But class INUserHandle
has a call to new in it. So, yes, this is a mem leak.

The class CallControlData_assignment has an op= in it, but I never
checked if it does the correct thing.
Socks
Jul 22 '05 #6
bk***@ncf.ca (Raymond Martineau) wrote in message news:<9m********************************@4ax.com>. ..
On 8 Nov 2004 12:35:14 -0800, ol*****@inspire.net.nz (Old Wolf) wrote:
"Scott Niu" <sc****@qd.lucent.com> wrote:
void f()
{ [snip] };
Syntax error - extra semicolon


That's not a syntax error. While it's technically against
style for putting in blank or no-op statements in C++,


You can't have statements at file scope, only declarations
and definitions.

it compiles correctly.


// file: r.cc
void f() {};

$ g++ -o r r.cc -ansi -pedantic -W -Wall
r.cc:2: error: extra `;'
Jul 22 '05 #7

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

Similar topics

2
by: Elbert Lev | last post by:
#When I'm running this script on my windows NT4.0 box, #every time dialog box is reopened there is memory growth 384K. #Bellow is the text I sent to Stephen Ferg (author of easygui) # I have...
14
by: J. Campbell | last post by:
what happens to allocated memory when a program terminates before the memory is released. For example: int main(){ int* array; int a_size = 1000; array = new int; for(int i = 0; i < a_size;...
5
by: blugus | last post by:
Hi Guys, I've been try to use Dinkum STL library. It workes well first, but report memory leak in MFC Debug Mode. I use Dinkum Unabridged Library for VC++ V4.02, MSVC6, WIN2000 SERVER. I...
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...
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...
8
by: vidya.bhagwath | last post by:
Hello Experts, I am using std::string object as a member variable in one of the my class. The same class member function operates on the std::string object and it appends some string to that...
17
by: Mike | last post by:
Hello, I have following existing code. And there is memory leak. Anyone know how to get ride of it? function foo has been used in thousands places, the signature is not allowed to change. ...
20
by: gNash | last post by:
Hi all, void main() { char *fp; fp=malloc(26); strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ"); fp='\0'; free(fp); }
16
by: graham.keellings | last post by:
hi, I'm looking for an open source memory pool. It's for use on an embedded system, if that makes any difference. Something with garbage collection/defragmentation would be nice. It should have...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.