Curious C++ segmentation fault
Can anyone suggest for me reasons why the first of these two fragments might produce a segmentation fault while the second works fine? ssc::RunSet has only one constructor, which takes no parameters. ++ #include"Executive.h" void ssc::Executive( int argc, const char* args[] ){ ... ssc::RunSet runset; /* FAILS HERE */ runset.run(); } ++ #include"Executive.h" void ssc::Executive( int argc, const char* args[] ){ ... ssc::RunSet* runset = new ssc::RunSet(); runset->run(); delete runset; } ++ -- JDL Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.
John Lamb
ssc::RunSet has only one constructor, which takes no parameters.
++
#include"Executive.h"
void ssc::Executive( int argc, const char* args[] ){ ... ssc::RunSet runset; /* FAILS HERE */ runset.run(); }
You should also provide the code of RunSet, at least the relevant parts, otherwise it's hard to see why the constructor fails. Philipp
Philipp Thomas wrote:
You should also provide the code of RunSet, at least the relevant parts, otherwise it's hard to see why the constructor fails.
alan@ibgames.com wrote:
I'd need to see the definition of runset, and the constructor to help...
OK, that itself is helpful. I'd forgotten to look in the most obvious
place (and forgotten to switch of optimisation before debugging ;-( )
When constructing RunSet on the stack, I get a segmentation fault during
dynamic_cast in setStream(). I don't understand that one at all. I can
work around this one, but I'd really like to know what's going on here
in case there's a more subtle bug lurking.
Does anyone know what "warning: can't find class named `ssc::RunSet', as
given by C++ RTTI" means? (See debug info below)
RunSet::RunSet(){
result = new Result( stream );
result->setShowIntroduction( true );
result->setShowOutput( true );
result->setShowSummary( true );
setStream( &std::cout, "output" );
algorithm = 0;
multipleRuns = 0;
upperBounds = 0;
lowerBounds = 0;
resultType = BEST;
}
void RunSet::setStream( std::ostream* stream,
const std::string stream_name ){
if( dynamic_caststd::ofstream*( this->stream ) )
delete this->stream;
this->stream = stream;
/* propagate to result and algorithm->result */
result->setStream( this->stream );
if( algorithm != 0 )
algorithm->setStream( this->stream );
this->stream_name = std::string( stream_name );
}
Program received signal SIGSEGV, Segmentation fault.
0x4029834f in __dynamic_cast () from /usr/lib/libstdc++.so.5
(gdb) #1 0x080768c4 in ssc::RunSet::setStream(std::ostream*, std::string) (
this=0xbffff3e0, stream=0x80ab0f8, stream_name=
{static npos = 4294967295, _M_dataplus = {
Never mind ... I've worked it out. And as usual the bug was the most obvious thing it could be: trying to delete an uninitialised pointer. I'd still like to know what warning: can't find class named `ssc::RunSet', as given by C++ RTTI means. JDL John Lamb wrote:
RunSet::RunSet(){ ... setStream( &std::cout, "output" ); ... }
void RunSet::setStream( std::ostream* stream, const std::string stream_name ){ if( dynamic_caststd::ofstream*( this->stream ) ) delete this->stream; ... }
-- JDL Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.
On 14 Sep 2003 at 20:40, John Lamb wrote:
Date sent: Sun, 14 Sep 2003 20:40:04 +0100
From: John Lamb
Can anyone suggest for me reasons why the first of these two fragments might produce a segmentation fault while the second works fine?
ssc::RunSet has only one constructor, which takes no parameters.
++
#include"Executive.h"
void ssc::Executive( int argc, const char* args[] ){ ... ssc::RunSet runset; /* FAILS HERE */ runset.run(); }
++
#include"Executive.h"
void ssc::Executive( int argc, const char* args[] ){ ... ssc::RunSet* runset = new ssc::RunSet(); runset->run(); delete runset; }
++
-- JDL
I'd need to see the definition of runset, and the constructor to help... alan -- http://www.ibgames.net/alan Registered Linux user #6822 http://counter.li.org Winding Down - Weekly Tech Newsletter - subscribe at http://www.ibgames.net/alan/winding/mailing.html
participants (3)
-
alan@ibgames.com
-
John Lamb
-
Philipp Thomas