Hi Sven,
What you do is illegal in C++ (and anyhow very inefficient).
When leaving your makegraph function, the object is deleted
when leaving the scope !
You can do as shown below
Rene Brun
#include "TGraph.h"
#include <iostream>
TGraph *makeGraph(void){
TGraph *rgraph = new TGraph(10);
rgraph->SetName("test");
return rgraph;
}
void sven() {
cout << "Creating main's TGraph" << endl;
TGraph *tgraph;
cout << "Getting TGraph from makeGraph." << endl;
tgraph = makeGraph();
cout << "Exiting test-code." << endl;
}
On Wed, 12
Mar 2003, Sven Schagen wrote:
> Dear ROOTers,
>
> I found a problem with functions returning TGraph objects. Returning a
> TGraph in a compiled piece of code triggers a segmentation violation. An
> simple example is attached below.
>
> My working version of ROOT is 3.04/00. I compiled the code with
> egcs-2.91.66 on a Redhat 6.2 machine. A minimal example of this problem is
> given in the code below.
>
> Getting an stack trace from the debugger after the crash shows that the
> problem is triggered by the TGraph-destructor.
>
> After looking at the TGraph source code, I think the problem lies in the
> fact that there is no TGraph copy constructor, to copy the arrays that
> contain the coordinates and the other 'pointer' data.
>
> So my question is: could a (safe) TGraph copy-constructor be implemented,
> or is there some (fundamental) reason why there is none implemented yet ?
>
> Cheers,
> Sven Schagen
>
> ---- Example code ----
>
> #include "TGraph.h"
> #include <iostream>
>
> TGraph makeGraph(void){
> TGraph rgraph(10);
> rgraph.SetName("test");
> return rgraph;
> }
>
> void main(int argc,char** argv){
> cout << "Creating main's TGraph" << endl;
> TGraph tgraph;
> cout << "Getting TGraph from makeGraph." << endl;
> tgraph = makeGraph();
> cout << "Exiting test-code." << endl;
>
> }
>
>
>
This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:10 MET