delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2004/11/02/11:01:14

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
From: Thomas8675309 AT yahoo DOT com (Tom)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Destructor being called twice?
Date: 2 Nov 2004 07:54:39 -0800
Organization: http://groups.google.com
Lines: 37
Message-ID: <7b68d58f.0411020754.2d857a9@posting.google.com>
References: <opsgtcaqh37iosox AT dhcp9540189 DOT columbus DOT rr DOT com>
NNTP-Posting-Host: 63.72.148.162
X-Trace: posting.google.com 1099410879 16247 127.0.0.1 (2 Nov 2004 15:54:39 GMT)
X-Complaints-To: groups-abuse AT google DOT com
NNTP-Posting-Date: Tue, 2 Nov 2004 15:54:39 +0000 (UTC)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

boohiss <boohiss AT cubesource DOT net> wrote:

> I have written a class with my own constructor and destructor.  I declare  
> an object of this class globally.
> The class is constructed just fine, but when the program exits, the  
> destructor is being called twice for some
> reason unknown to me.  This is causing a seg fault, since I am trying to  
> write to a file from a string array
> in the destructor, and the 2nd time the destructor is called, it writes  
> anywhere from 80-100k of garbage to my
> file.
> 
> I am *not* calling this destructor anywhere in the program, it should only  
> be called once (implicitly) upon
> program end.
> 
> Any idea what I'm doing wrong here?

No, because you haven't posted your code.  Please post the smallest
self-contained compilable example that will duplicate the problem you
describe, so that we can copy and paste and try it out for ourselves. 
(No more than 50 lines, please.)

Still, if I had to guess ... Hmmm, my crystal ball tells me that your
constructor allocates memory dynamically, that you didn't bother to
define a proper copy constructor, and somewhere in your program, the
(compiler-generated default) copy constructor is called implicitly -
presto, your destructor is called twice, and the same dynamically
allocated memory is deleted twice, which leads to a seg fault. 
Solution:  Define a proper copy constructor.

Or maybe not.  Without your posting some code, how could we possibly
know?

Best regards,

Tom

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019