seg fault problem

G

Gary Wessle

Hi

I am getting a seg fault, only if I replace the "(*m_os[*i])" with
"cout" it prints. how can I fix this?
the example below given as a representation of the part of a larger
code which exebits the problem.

many thanks

class A
{
std::map<std::string, std::eek:fstream*> m_os;
std::map<std::string, myType> t4s;
std::vector<string> file_names;
public:
A(){}
void A_Mthd();
};

A::A()
{
void populate();
}

void A::populate()
{
/* some code to fill file_names vector here */

/* Populate the ostream pointers map. */
for( vector<string>::iterator i = file_names.begin(); i != file_names.end(); i++ )
{
string f = "mydirectory/" + (*i);
ofstream* p_of = new ofstream( f.c_str(), ios::app);
m_os[*i] = p_of;
}


void A::A_Mthd(){
for( vector<string>::iterator i = file_names.begin(); i != file_names.end(); i++ )
(*m_os[*i]) << t4s[*i].B_Mthd() << " " << endl;
}
 
V

Victor Bazarov

Gary said:
Hi

I am getting a seg fault, only if I replace the "(*m_os[*i])" with
"cout" it prints. how can I fix this?
the example below given as a representation of the part of a larger
code which exebits the problem.

many thanks

class A
{
std::map<std::string, std::eek:fstream*> m_os;
std::map<std::string, myType> t4s;
std::vector<string> file_names;
public:
A(){}
void A_Mthd();
};

A::A()
{
void populate();

Instead of declaring a 'populate' function here you might want to
actually call it:

populate(); // drop the 'void'

And next time put a trace statements everywhere you need to verify
passing control through, or just use a debugger like everybody else
to confirm that the functions you need called actually get called.
}

void A::populate()
{
/* some code to fill file_names vector here */

/* Populate the ostream pointers map. */
for( vector<string>::iterator i = file_names.begin(); i !=
file_names.end(); i++ ) {
string f = "mydirectory/" + (*i);
ofstream* p_of = new ofstream( f.c_str(), ios::app);
m_os[*i] = p_of;
}


void A::A_Mthd(){
for( vector<string>::iterator i = file_names.begin(); i !=
file_names.end(); i++ ) (*m_os[*i]) << t4s[*i].B_Mthd() << " " <<
endl; }
 
G

Gary Wessle

Victor Bazarov said:
Gary said:
Hi

I am getting a seg fault, only if I replace the "(*m_os[*i])" with
"cout" it prints. how can I fix this?
the example below given as a representation of the part of a larger
code which exebits the problem.

many thanks

class A
{
std::map<std::string, std::eek:fstream*> m_os;
std::map<std::string, myType> t4s;
std::vector<string> file_names;
public:
A(){}
void A_Mthd();
};

A::A()
{
void populate();

Instead of declaring a 'populate' function here you might want to
actually call it:

populate(); // drop the 'void'

And next time put a trace statements everywhere you need to verify
passing control through, or just use a debugger like everybody else
to confirm that the functions you need called actually get called.

actually, the function populate is being called from within the
ctor. I miss typed this part of the example.

trace statements? what is that, I put "cout <<" every where and that
is way I was able to narrow it down to the area mentioned at the start
of my original post.

I am not able to use the gdb debugger because I am not able to get it
working with this multi-threaded code.

I which I can do more, but I don't know what or how.

thanks
 
K

Kai-Uwe Bux

Gary said:
Victor Bazarov said:
Gary said:
Hi

I am getting a seg fault, only if I replace the "(*m_os[*i])" with
"cout" it prints. how can I fix this?
the example below given as a representation of the part of a larger
code which exebits the problem.

many thanks

class A
{
std::map<std::string, std::eek:fstream*> m_os;
std::map<std::string, myType> t4s;
std::vector<string> file_names;
public:
A(){}
void A_Mthd();
};

A::A()
{
void populate();

Instead of declaring a 'populate' function here you might want to
actually call it:

populate(); // drop the 'void'

And next time put a trace statements everywhere you need to verify
passing control through, or just use a debugger like everybody else
to confirm that the functions you need called actually get called.

actually, the function populate is being called from within the
ctor. I miss typed this part of the example.
[snip]

I which I can do more, but I don't know what or how.

One thing you could do is to provide a minimal complete piece of code that
demonstrates _the_ problem (not some other problem as you OP). And make it
so that one can compile and run it without adding stuff like main().


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C99 Seg fault on while(), why ? 0
Map Seg Fault 17
input/output trouble cause segmentation fault 3
Strange seg fault 4
reading and writing a container of std::pair type 6
TF-IDF 1
possible seg fault 11
A seg fault 11

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top