I’ve got 3 files that relate to this problem. file.h, file.C and user.C.
file.h has a private member, fstream logs.
In file.C’s constructor it opens logs. It doesn’t do this in the constructor, but the constructor calls a function OpenLog().
file.h also has an inline close function:
CloseLog() {if (logs) logs.close();}
The file user.C has an exit function which creates an instance of file, then calls CloseLog. It seg faults at this point. I created some other dummy tests, and it appears as though logs is lost in the mix somewhere …
Going from file.C to user.C and then back to file.C causes this. If I have fstream logs as a global in file.C then it works – but I’d rather avoid a global.
Any thoughts on what I should do here? Please let me know if I should post more code about this, I can set up some dummy stuff to demo this better.
** Here’s more code, as requested – I can’t copy and paste, so forgive the lack of it please **
I will call the classes helpME.h, helpME.C and user.C
//helpME.h
#ifndef _helpME_H
#define _helpME_H#include < iostream>
#include < fstream>
//various includesclass helpME {
private:
fstream logs;public:
void CloseLog() {if (logs) logs.close();}
};
#endif//end helpME.h
//helpME.C
void helpME::helpME(int argc, char** argv)
{
//various code
OpenLog();
}void helpME::OpenLog()
{
//logname is set above, i had a print statement before that showed this is correct
logs.open(logname, ios::in | ios::out | ios::trunc);
}//end helpME.C
//user.C
void user::quitHelpME(item)
{
helpME* hME = (helpME*) item;
hME->CloseLog();
}//end user.C
Again – please forgive the lack of clarity, I’m thinking I may have just confused things more by adding this … this code is on another box and can’t be copied over.
This doesn’t create an instance, it’s using C style casting to cast from whatever item is to a pointer to
helpME.if item is NULL then calling a method on it will seq fault.
otherwise still not enough detail in your example to give you an answer, the code present seems sound.