Mail Archives: djgpp/2004/11/03/18:01:38
On 2 Nov 2004 07:54:39 -0800, Tom <Thomas8675309 AT yahoo DOT com> 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<RECEIPT_Y;++i)
{
if(header[i] != " ") {
ofs << header[i] << "\n";
}
}
ofs.close();
}
Thanks,
Matt
--
http://www.mgroves.com
- Raw text -