   Class Member & std::ofstream . Plz Help!!
  Ok having a huge problem with classes and ofstream...  I've used this basicfunction a million times in normal functions.  Now when I call this functionor class member outside of the class it works perfect!!  but when i callthis function / class member from another class member, it dies right on..std::ofstream oStream;  Inbetween the TEST 1 and TEST 2.. Thats how i tracked it down.  I'm pulling my hair out trying to figure why it works when i call it outside, but not from another class function..  can anyone shed some light on this for me?  I'm going so nuts!! // top of source file#include <fstream>#include <string>using namespace std;// Creates a New Menu//----------------------------------------------------------------------------void menus::create(char *menuname) {    std::string name = MENUPATH;    name += menuname;   logntp("TEST 1");   std::ofstream oStream;   logntp("TEST 2");   oStream.open( name.c_str(), ios::out | ios::trunc );    if (!oStream.is_open()) {   // unable to create menu file   logntp("Unable to Create Menu File");   return;    }    oStream << "New Vampyric Menu" << endl;   // menur->MenuName    oStream << endl; // menur->Directive    oStream << endl; // menur->Tutorial    oStream << endl; // menur->mPromptF    oStream << endl; // menur->mPromptF    oStream << endl; // menur->MenuPrompt    oStream << (0) << endl;    // menur->ForceInput    oStream << (0) << endl;    // menur->HiLite    oStream << (0) << endl;    // menur->LoLite    oStream << endl; // menur->Acs    oStream << endl; // menur->Password    oStream << endl; // menur->Fallback    oStream << (0) << endl;    // menur->ForceHelpLevel    oStream << (1) << endl;    // menur->GenCols    oStream << (0) << endl;    // menur->GCol[0]    oStream << (0) << endl;    // menur->GCol[1]    oStream << (0) << endl;    // menur->GCol[2]    oStream << (false) << endl;// menur->MFlags.ClrScrAfter    oStream << (false) << endl;// menur->MFlags.ClrScrBefore    oStream << (false) << endl;// menur->MFlags.DontCenter    oStream << (false) << endl;// menur->MFlags.ForcePause    oStream << (false) << endl;// menur->MFlags.NoMenuPrompt    oStream << (false) << endl;// menur->MFlags.UseGlobal    oStream << endl; // Put a Space Inbetween Menu and Commands!    oStream << "New Menu Command" << endl;    // cmdr[noc].LDesc    oStream << "'Q'uit" << endl;    // cmdr[noc].SDesc    oStream << endl; // cmdr[noc].CKeys    oStream << endl; // cmdr[noc].Acs    oStream << endl; // cmdr[noc].CmdKeys    oStream << endl; // cmdr[noc].MString    oStream << (true) << endl; // cmdr[noc].Visible    oStream.close();}[edited by - tonic151 on February 3, 2004 8:21:16 AM]
  Tried debugging it in a debugger? What the error code \msg?;  only thing i see is a mesasge saying:  Access Violation (Segmentation Fault) raised in your programbasically the only line i get in the debugger is...source C:\Documents.... /menus.cpp:202:5400:beg:0x407fd0then it highlights the ofstream oStream;it was also doing the same on std::string name;but i have no idea why it would do this.. --------------Hmm i have a feeling that maybe my threading is casuing the problem some how..  if i call the function member from main() it works.. but when i call it inside my new thread it seems to die..  but all the handles i pass below work just great in the thread.. //I'm using with the following..#include <process.h>HANDLE ahThread[zTelnetServer_MAX_SESSIONS];PASSING *pass;  // For Passing Socket and Session for Each Thread// Fill Structure for Passing to new Threadpass->handler = handler; // Command Handlerpass->session = &session[i];  // Session Handlerpass->z = z;   // Socket  HandlerahThread[i] = (HANDLE)_beginthread( StartSessionThread, 0, (void *)pass);everything seems to run ok in the thread until i try to run these class members.. or really the orignal one at the top there..  and it seems to die on the std:: members.. but i'm using other class members just fine though.. so it's very strange.. but non of them have std:: in them either..  It's really a shame that these c++ functions are crocking.. if i use stright c file i/o and char *arrays.. everything works perfect.. i might just have to write everyone in c.. and say fuck it and switch.. but i really love the std::string and fstream easability..  [edited by - tonic151 on February 3, 2004 10:44:11 AM];  You don''t really show enough code for someone to spot the problem right off because I think it lies in a different part of your program.  If you posted more of the code then someone could probably help you.  Did you check to make sure that the objects and data you''re passing to the threads are either global or created dynamically?  If it works in C and not in C++ then there''s still a problem, it''s just being masked.  These are the worse ones of all because they always creep back up later in your project and are extremely difficult to track down. ;  You mentioned threads.Is Std lib thread safe? Are you opening the file twice at the same time in different threads? ;  Well i don't know what else to really post, but here is the class in question, and the main function that calls to it..If you can find anything i'd be much apprecieative..But as you can probably see it's all pretty basic.. Main Class for Menu System #ifndef MENUS_H#define MENUS_H#include "ztelnetserver.h"#include "struct.h"using namespace std;// Menu Systemclass menus {    protected:   // This holds passed telnet session data    // So Menu System can Read/Write Sockets    PASSING *pass; char curmenu[80];  // Current Menu    char fallback[80]; // Previous Menu    MenuRec*menur;   // Handle to Menu Structure    CommandRec  *cmdr;    // Handle up to 50 Commands per Menu   // Menu System / Processing Control    void clear();    bool readin();    // Menu Editor / Creation    bool menuexists(char *menuname);  public:    menus::menus();    void start(PASSING *passing);    bool mcreate(char *menuname);    void select(); };#endifHand C++ files with Menu Class Functions // Creates a New Menubool menus::mcreate(char *menuname) {    char name[300];  // Changed from std::string    sprintf(name,"%s%s",MENUPATH, menuname);ofstream oStream;oStream.open( name, ofstream::out | ofstream::trunc );    if (!oStream.is_open()) {   // unable to create menu file   logntp("Unable to Create Menu File");   return false;    }    oStream << "New Vampyric Menu" << endl;   // menur->MenuName    oStream << endl; // menur->Directive    oStream << endl; // menur->Tutorial    oStream << endl; // menur->mPromptF    oStream << endl; // menur->mPromptF    oStream << endl; // menur->MenuPrompt    oStream << (0) << endl;    // menur->ForceInput    oStream << (0) << endl;    // menur->HiLite    oStream << (0) << endl;    // menur->LoLite    oStream << endl; // menur->Acs    oStream << endl; // menur->Password    oStream << endl; // menur->Fallback    oStream << (0) << endl;    // menur->ForceHelpLevel    oStream << (1) << endl;    // menur->GenCols    oStream << (0) << endl;    // menur->GCol[0]    oStream << (0) << endl;    // menur->GCol[1]    oStream << (0) << endl;    // menur->GCol[2]    oStream << (false) << endl;// menur->MFlags.ClrScrAfter    oStream << (false) << endl;// menur->MFlags.ClrScrBefore    oStream << (false) << endl;// menur->MFlags.DontCenter    oStream << (false) << endl;// menur->MFlags.ForcePause    oStream << (false) << endl;// menur->MFlags.NoMenuPrompt    oStream << (false) << endl;// menur->MFlags.UseGlobal    oStream << endl; // Put a Space Inbetween Menu and Commands!    oStream << "New Menu Command" << endl;    // cmdr[noc].LDesc    oStream << "'Q'uit" << endl;    // cmdr[noc].SDesc    oStream << endl; // cmdr[noc].CKeys    oStream << endl; // cmdr[noc].Acs    oStream << endl; // cmdr[noc].CmdKeys    oStream << endl; // cmdr[noc].MString    oStream << (true) << endl; // cmdr[noc].Visible    oStream.close();    return true;    }// Reads In and Sorts All Menu files on the flyvoid menus::select() {    // First Read in all Menu file for a full list    WIN32_FIND_DATA wfd;  // File Structure    HANDLEh;    // Handle to File    char tb[255]; // Changed from std::string    sprintf(tb,"%s*.mnu",MENUPATH);    // Display Header    pass->session->printf("\x1b[2J");    pass->session->printf(".----------------------------------------------------------------.\r\n");    pass->session->printf("| Menu Files Listing.. . |\r\n");    pass->session->printf("`----------------------------------------------------------------'\r\n");    pass->session->printf("  Size Filename\r\n");    pass->session->printf(".----------------------------------------------------------------.\r\n");    int row = 6;    h = FindFirstFile(tb, &wfd);    if(h != INVALID_HANDLE_VALUE) {   if (wfd.nFileSizeLow > 1000)  pass->session->printf("| %i kb \x1b[%i;13H%s \x1b[%i;66H|\r\n", (wfd.nFileSizeLow / 1000), row, wfd.cFileName, row);   else  pass->session->printf("| 1 kb \x1b[%i;13H%s \x1b[%i;66H|\r\n", row, wfd.cFileName, row);   ++row;   while(FindNextFile(h, &wfd)) {  if (wfd.nFileSizeLow > 1000) pass->session->printf("| %i kb \x1b[%i;13H%s \x1b[%i;66H|\r\n", (wfd.nFileSizeLow / 1000), row, wfd.cFileName, row);  else pass->session->printf("| 1 kb \x1b[%i;13H%s \x1b[%i;66H|\r\n", row, wfd.cFileName, row);  ++row;   }   FindClose(h);    }    else {   pass->session->printf("|  No Menu Files Found! \r\n");    }   pass->session->printf("`----------------------------------------------------------------'\r\n");    pass->session->printf("\r\n");    pass->session->printf("(c)reate, (e)dit, (m)odify, (d)elete, (q)uit : \x1b[1;44m %c",0x08);    return;    }Main Calling Function to Handle Menu Class // Menu Calling Functionvoid MenuEdit(PASSING *pass) {    menus *mnu = new menus;   // Initalize Menu Editing System    mnu->start(pass);   // Dispaly Menu and Retrive Selection    unsigned char c;    int result = 0;    char rBuffer[1024];   while (1) {   mnu->select();    // Incomming Socket/Message Loop   while (1) {  // Check conenction and Poll for messages  if( pass->session->isActive() ) { result = pass->session->pollc(c); if (result) break;  }   }  // Check for Backspace first!   if (toupper(c) == 'C') {  pass->session->puts("\x1b[0m\r\n\r\nNew Menu Name: \x1b[1;44m"); pass->session->printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08); if( pass->session->isActive() ) {while(1) {    result = pass->session->polls(rBuffer);    if (result) break;}// Create New Menuif (!mnu->mcreate(rBuffer)) {    pass->session->printf("\r\nError Creating New Menu: %s", rBuffer);}break; } else break;   }    }    delete mnu;} [edited by - tonic151 on February 3, 2004 11:29:00 AM];quote:Original post by Zidane    You mentioned threads.Is Std lib thread safe? Are you opening the file twice at the same time in different threads?   Na, i'm only running, or connect 1 at a time,, and that 1 is having the problems.. other then the new thread that gets kicked off on the main thread keeps going and that only polls for conenctions.. I also have another file transfer program i made and that doesn't seem to have any of these problems.. i guess the only way i'm gonig to figure this out is break out a couple other compilers and see if they catch any error that this one is missing..   [edited by - tonic151 on February 3, 2004 12:06:38 PM];  Id try that as well, because I really cant see much wrong with your code. ;  thank for you all takeing the time to check my shit over.. i finially fixed the problem.. omfg!!!    heheit was with the way i was passing the PASSING *pass struct to the new thread ... i guess gcc 2.95.3 didn't pick it up but gcc 3.2 picked it up in debug... if you look at the thread i posted earlier you will note the changes.. i allocated a new to the pointer before passing it.. // Fill Structure for Passing to new ThreadPASSING *pass = new PASSING;pass->handler = (ZTelnetServerCmdHandler *) handler; // Command Handlerpass->session = &session[i];   // Session Handlerpass->z = z;    // Socket  HandlerahThread[i] = (HANDLE)_beginthread( StartSessionThread, 0, pass);// Then the type case back from (void *) in the new thread..void StartSessionThread(void *p) { PASSING *pass = (PASSING*) p;hope this helps anyone with threading problems.. whewwwww after 2 days i can relax.. thank god!!  Thank again for everyone's help.. i almost forgot to check the debugger.. haha but gcc/g++ 2.95.3 didn't catch it, till i installed 3.2 ... a note for anyone who uses DevC++ it's good to have both versions for testing..  [edited by - tonic151 on February 3, 2004 1:00:01 PM];  Im happy you found the bug 