Trouble with passing TCanvas* to function

Hi all.

This is maybe a very basic question - which fits, since I am rather a ROOT/C++ beginner. I wanna do the following. I have a class which shall contain a canvas as a member variable. This canvas I wanna define/initialize via a function. So I have to pass this canvas to the function and I get troubles with that. I store the initialization of the canvas in the function because then I can set default widths and heights of the canvas.

The function:

#include <TCanvas.h>

void SetCanvas(TCanvas * canvas, std::string name, int width, int height){

    canvas = new TCanvas(name.c_str(), "C", width, height);  


Header file of the class:

Header file:

#include <TCanvas.h>

Class myClass{

    void DoSomeStuff(); 
    TCanvas * kCanvas;


Source file:

#include "myClass.hh"

void myClass::DoSomeStuff(){

    kCanvas -> ls(); // this is the marked line


Gcc compiler does not complain but when I wanna execute the code, I get the following seg fault in the marked line from above:

#0  0x00000037458ac5de in waitpid () from /lib64/
#1  0x000000374583e619 in do_system () from /lib64/
#2  0x00007f18a37a7c72 in TUnixSystem::StackTrace() () from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/
#3  0x00007f18a37a4bba in TUnixSystem::DispatchSignals(ESignals) () from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/
#4  <signal handler called>
#5  0x0000000000434fbe in myClass::DoSomeStuff (this=0x12f30f0, module_id=11, sample_names=Traceback (most recent call last):
    itype0  = self.val.type.template_argument(0)
    self.item = self.item + 1
    itype0  = self.val.type.template_argument(0)
    self.item = self.item + 1

So I figure, the initialization is not correct. Somehow I do not give the canvas properly to the function or such. But I cannot see the error. Do you?

Thank you for your help.



this is a C++ issue. In your SetCanvas function, you are passing a TCanvas pointer type by value and changing its value from within the function with the allocation with new.
Your signature should be:

void SetCanvas(TCanvas *& canvas, std::string name, int width, int height)