🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++
Article #118

Re: Destructor called too soon, Destructor not called - G++1.35

#118
From: John Nagle
Date: Thu, 07 Sep 1989 23:19
40 lines
1926 bytes
In article <2381@csun.edu-> cbcscmrs@ma.csun.edu (Mike Stump) writes:
->[Insult deleted]
->The type of the argument to a printf %s should be a char*,
->not an argument of type String.  Therefore one should cast
->the String to a char * as in:
->
->       printf ("Function: %s (%x)\n", (char*)my_func(),
->	       (char*)my_func());
->
->That's about it, oh, just one other thing, in class String,
->one does need to tell c++ _how_ to convert the thing into
->a char* type object.
->
->class String {
->...
->     operator char* () {
->	 return _ptr;
->     }
->...
->}

     No good.  What you have done here may result in a pointer to
space that has already been released with "delete".  Think about
the sequence of events implied here.

	1.  my_func returns a string object into a temporary variable
	    allocated by the compiler. 
	2.  The "char *" conversion extracts the pointer to the array
	    of characters from the string object.  This pointer is to
	    space obtained with "new" when the string object was allocated.
	3.  The destructor for the temporary string object is called,
	    resulting in the release of the space holding the characters.
	4.  The pointer obtained in step 2, which now points to free
	    space, is passed to printf.

To make it clear that this approach is wrong, try putting code in the
destructor to clear the buffer to some garbage character before releasing
it with "delete".

					John Nagle
Technical Details
Message-ID: <13510@well.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!ucsd!usc!apple!well!nagle
References: <ANDREWT.89Aug3001106@watsnew.waterloo.edu> <8909040747.AA00164@teacake.sun.com> <2381@csun.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!ucsd!usc!apple!well!nagle
From: nagle@well.UUCP (John Nagle)
Newsgroups: gnu.g++
Subject: Re: Destructor called too soon, Destructor not called - G++1.35
Message-ID: <13510@well.UUCP>
Date: 7 Sep 89 23:19:37 GMT
References: <ANDREWT.89Aug3001106@watsnew.waterloo.edu> <8909040747.AA00164@teacake.sun.com> <2381@csun.edu>
Reply-To: nagle@well.UUCP (John Nagle)
Distribution: gnu
Lines: 40