views:

74

answers:

2

Hi, 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 includes

class 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.

A: 

Because you've declared your variable in the .h file, you have two copies of it. The compiler doesn't 'see' .h files, it just copies/pastes what is in the file into the .C files, so, that's why you have two copies of the variable.

Declare the variable as extern in the .h file, and declare it again without the extern in only one .C file and don't use static in any declaration of that file.. It should fix your problem.

Gianni
thank you kindly - I get the message 'error: storage class specified for logs' when I do this. I looked that up and it looks like I may have a problem in my .h somewhere so I'm looking for that.Again, thank you.
glazedandconfused
http://cboard.cprogramming.com/c-programming/113962-my-error-storage-class-specified-parameter.htmlis the link I'm looking at for that, for future reference for anyone else
glazedandconfused
A: 
void user::quitHelpME(item)
{
helpME* hME = (helpME*) item;

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.

Greg Domjan
XtPointer item...looking into that now ... thank you!(to somewhat explain the lack of clarity - it's legacy code and the push is 'make it compile' ... there's not much push to clean it up or understand it. so i'm learning as I go. apologizies for my lack of knowledge.)
glazedandconfused