X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f Newsgroups: comp.os.msdos.djgpp Subject: Re: Destructor being called twice? References: <7b68d58f DOT 0411020754 DOT 2d857a9 AT posting DOT google DOT com> Message-ID: From: boohiss Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit User-Agent: Opera M2/7.54 (Win32, build 3869) Lines: 81 Date: Wed, 03 Nov 2004 22:49:49 GMT NNTP-Posting-Host: 24.95.40.189 X-Complaints-To: abuse AT rr DOT com X-Trace: fe2.columbus.rr.com 1099522189 24.95.40.189 (Wed, 03 Nov 2004 17:49:49 EST) NNTP-Posting-Date: Wed, 03 Nov 2004 17:49:49 EST Organization: Road Runner High Speed Online http://www.rr.com To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com On 2 Nov 2004 07:54:39 -0800, Tom wrote: > 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. I did not think that I needed a copy constructor, since I'm only instantiating this object once, and as a global object. Still, there may be some other place that it's used that I'm overlooking. Also, I'm not allocuting any dynamic memory. Code: #define RECEIPT_X 30 #define RECEIPT_Y 20 #include "globalsettings.h" GlobalSettings GS; int main() { // user edits the text that was read in by GS GS.edit_header(); return(0); } GlobalSettings::GlobalSettings() { // snip [some stuff that is working fine] // read header from a txt file int i=0; ifstream ifs; ifs.open("receipt.txt"); for(;!ifs.eof();) { getline(ifs,header[i]); for(;header[i].length() < RECEIPT_X;) { header[i]+=" "; } ++i; } ifs.close(); // snip [some more stuff that is working fine] } GlobalSettings::~GlobalSettings() { // snip [some stuff that is working fine] // save header to a file // do not save blank lines ofstream ofs; ofs.open("receipt.txt"); for(int i=0;i