Why crash when double click on Pad?

Aha. OK see it now. Thanks.

I have one more question. Is it possible to add a new TH1D or TGraph object to be drawn once the Worker::act() method had been called?

I understand I can create a TGraph object in Worker::act() but I should not call Draw() on it in Worker::act() as its not thread safe. Is there a way to make the call to Draw() (which adds the object pointer to the list of TPad primatives) safely?

Thanks,
Sanjeev

[quote=“supandey”]…
I understand I can create a TGraph object in Worker::act() but I should not call Draw() on it in Worker::act() as its not thread safe. . . . [/quote]Since the last version of the code requries no thread at all it doesn’t matter whether the code is thread safe or not. So yes, you should be able to add the new TGraph object. Have you met any problem?

I tried the following which creates a new TGraph each time SingleShot is called and its is much more stable than the threads version I had before. When I run it from cint it works fine. When I run it standalone after about 200 double clicks I get a crash. I understand I cant keep creating a graph with more and more points but it crashed around 200 points which is small enough I think.
Thanks,
Sanjeev

//---Worker.cxx 
#include "Worker.h" 
#include "TApplication.h"
#include "TTimer.h" 
#include "Riostream.h" 

#include "TGraph.h" 
#include "TMath.h" 
#include "TCanvas.h" 
#include "TRandom.h" 
#include "TH1.h" 

namespace 
{ 
    TCanvas *c1; 
    TPad *pad1; 
    TPad *pad2; 
} 

Worker::~Worker()
{
    delete m_h1;
    delete m_graph;
}

void 
Worker::act() 
{ 
    double val1 = gRandom->Gaus(-1,1.5); 
    m_h1->Fill(val1); 

    m_X.push_back(++m_N);
    m_Y.push_back(val1);
    Double_t* x = static_cast<Double_t*>(&m_X[0]);
    Double_t* y = static_cast<Double_t*>(&m_Y[0]);
    m_graph = new TGraph(m_N, x, y);
    pad2->cd(); 
    m_graph->Draw("ALP");

    pad1->Modified(); 
    pad2->Modified(); 
    c1->Update(); 
    TTimer::SingleShot(300,"Worker", this, "act()"); 
} 

void 
Worker::init() 
{ 
    double val = gRandom->Gaus(-1,1.5); 
    m_h1 = new TH1D("h1","h1",100,-4,4);
    m_h1->Fill(val); 

    m_X.push_back(m_N);
    m_Y.push_back(val);

    Double_t* x = static_cast<Double_t*>(&m_X[0]);
    Double_t* y = static_cast<Double_t*>(&m_Y[0]);
    m_graph = new TGraph(m_N, x, y);

    pad1->cd(); 
    m_h1->Draw("e1p"); 

    pad2->cd(); 
    m_graph->Draw("ALP");
} 

void Worker::RunWorker() 
{ 
    c1 = new TCanvas("c1","The example",800,400); 
    pad1 = new TPad("pad1","This is pad1",0.02,0.02,0.48,0.98,33); 
    pad2 = new TPad("pad2","This is pad2",0.52,0.02,0.98,0.98,33); 

    c1->cd(); 
    c1->SetGrid(); 
    pad1->Draw(); 

    c1->cd(); 
    pad2->Draw(); 

    Worker *wrk = new Worker(); 
    wrk->init(); 

    wrk->act(); 

}

int main(int argc, char **argv) 
{ 
    TApplication theApp("App", &argc, argv); 

    Worker::RunWorker();

    theApp.Run(); 

    return 0; 
} 
//---Worker.h 
#ifndef SAW_WORKER 
#define SAW_WORKER 

class TH1D; 
class TGraph;

#include <vector>

class Worker 
{ 
public: 
    // Construction 
    Worker(): m_N(1),m_h1(0),m_graph(0) {;} 
    virtual ~Worker(); 

    // Operations 
    void act(); 
    void init(); 
    static void RunWorker(); 

private: 
    //Data 
    int      m_N;
    TH1D*    m_h1; 
    TGraph*  m_graph;

    std::vector<double> m_X;
    std::vector<double> m_Y;

}; 

#endif 

With root 5.17.04 I get

Unhandled exception at 0x7c812a5b in testPlot.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012fb34..


 	kernel32.dll!7c812a5b() 	
 	[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]	
 	kernel32.dll!7c812a5b() 	
 	ntdll.dll!7c9106eb() 	
>	msvcr71.dll!_heap_alloc(unsigned int size=2084378196)  Line 212	C
 	msvcr71.dll!$$$00001()  Line 56	Asm
 	0012fb34()	
 	msvcp71.dll!std::_Nomemory()  Line 9 + 0x1e bytes	C++
 	libCore.dll!10292c57() 	
 	libCore.dll!1002471d() 	
 	libCore.dll!102953dc() 	
 	libCore.dll!100259df() 	
 	libCore.dll!102954e3() 	
 	libCore.dll!10025bb7() 	
 	libCore.dll!10028e75() 	
 	libCore.dll!102957e1() 	
 	libCore.dll!1002924c() 	
 	libCore.dll!10045ad0() 	
 	libCore.dll!102957e1() 	
 	libCore.dll!10045cf3() 	
 	libCore.dll!10296f2b() 	
 	testPlot.exe!Worker::act()  Line 43 + 0x15 bytes	C++
 	testPlot.exe!G__WorkerDict_218_0_2(G__value * result7=0x01ed5000, const char * funcname=0x00000000, G__param * libp=0x01ed5050, int hash=0)  Line 155	C++
 	libCint.dll!00427322() 	
 	libCore.dll!10024bbe() 	
 	msvcr71.dll!malloc(unsigned int size=20)  Line 54 + 0xf bytes	C
 	libCore.dll!10292c5d() 	
 	libCore.dll!10057cba() 	
 	libCore.dll!1002601f() 	
 	libCore.dll!10027a89() 	
 	libCore.dll!100064fd() 	
 	libCore.dll!10045c3b() 	
 	libCore.dll!1004578e() 	
 	libCore.dll!1007efa9() 	
 	kernel32.dll!7c80a017() 	
 	kernel32.dll!7c802532() 	
 	libGui.dll!021e4a5e() 	
 	libCore.dll!10081833() 	
 	libWin32gdk.dll!01f1b62b() 	
 	libCore.dll!1003c794() 	
 	libCore.dll!1003c72a() 	
 	libCore.dll!1003c700() 	
 	testPlot.exe!__tmainCRTStartup()  Line 597 + 0x17 bytes	C
 	libCore.dll!10005d6e() 	
 	testPlot.exe!main(int argc=1, char * * argv=0x01ac3cb8)  Line 95	C++
 	testPlot.exe!__tmainCRTStartup()  Line 597 + 0x17 bytes	C
 	kernel32.dll!7c816fd7() 	
 	libGpad.dll!00340031() 	

and with the BNL version (root.5.18.00.exe) I get

 	kernel32.dll!7c812a5b() 	
 	[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]	
 	kernel32.dll!7c812a5b() 	
 	msvcr80.dll!78132bc6() 	
 	msvcr80.dll!78138b51() 	
 	msvcr80.dll!781323ff() 	
 	msvcr80.dll!78131731() 	
 	msvcr80.dll!78158e89() 	
 	msvcr80.dll!78160e7c() 	
 	libHistPainter.dll!01e77e21() 	
 	ntdll.dll!7c9106eb() 	
 	msvcr80.dll!78134d83() 	
 	ntdll.dll!7c91056d() 	
 	msvcr80.dll!78134c39() 	
 	msvcr80.dll!78134c58() 	
 	qt-mt337.dll!39d1e995() 	
 	qt-mt337.dll!39d71cfc() 	
 	libGQt.dll!01739f8a() 	
 	libGQt.dll!0175ffdb() 	
 	libGQt.dll!0173c788() 	
 	libGpad.dll!00e13624() 	
 	libGraf.dll!00b863dd() 	
 	libCore.dll!10071a0a() 	
 	libCore.dll!10071a0a() 	
 	libCore.dll!10071a61() 	
 	libHist.dll!007e2abe() 	
 	libGpad.dll!00e0b03c() 	
 	libGpad.dll!00e0afa5() 	
 	libGpad.dll!00df5eaf() 	
>	testPlot.exe!Worker::act()  Line 42	C++
 	testPlot.exe!Worker::act()  Line 43	C++
 	testPlot.exe!G__WorkerDict_218_0_2(G__value * result7=0x02242d70, const char * funcname=0x00000000, G__param * libp=0x02242dc0, int hash=0)  Line 155	C++
 	libCint.dll!00427b54() 	
 	libCore.dll!1002570e() 	
 	msvcr80.dll!78160e30() 	
 	msvcr80.dll!78160e30() 	
 	libCore.dll!1005f5ca() 	
 	libCore.dll!10026a5f() 	
 	libCore.dll!10028631() 	
 	libCore.dll!1000529d() 	
 	libCore.dll!1004848d() 	
 	libCore.dll!10047f88() 	
 	libCore.dll!100895d2() 	
 	libGui.dll!019c48ce() 	
 	libCore.dll!1008c1ef() 	
 	libGpad.dll!00e448cb() 	
 	libCore.dll!1003e813() 	
 	libCore.dll!1003e6e7() 	
 	libCore.dll!1003e6b9() 	
 	libCore.dll!10067f4c() 	
 	libCore.dll!100055f3() 	
 	libCore.dll!10004ab0() 	
 	testPlot.exe!main(int argc=1, char * * argv=0x0158abf0)  Line 95	C++
 	testPlot.exe!__tmainCRTStartup()  Line 597 + 0x17 bytes	C
 	kernel32.dll!7c816fd7() 	

testPlot.zip (11.6 KB)

Hello
I was not able to reproduce your crash with BNL version of ROOT.
I made no change of your code and did:

del %ROOTSYS%\lib\*new* rootcint -f WorkerDict.cxx -c Worker.h Linkdef.h cl *.cxx -I%ROOTSYS%/include %ROOTSYS%/lib/*.lib
with Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved.
I have no time to install and investigate the version from CERN right now.
Worker.zip (39.6 KB)

Did I understand your current problem properly?
[ul] - If you compile your code with the ROOT assistance (aka via ACliC) it works.

  • If I compile your code it works too
  • If you compile your code “your way” it crashes.[/ul]I mean your original issue was resolved. Now we are speaking about the new one. May be you should close this thread and start the new one “How to compile ROOT application properly” or something like this.

Yes you understood the issue right. Thank you very much for your help on this. When I find out more about this issue (more reproducible) I will open a new thread.

Thanks,
Sanjeev

Please, see another topic: root.cern.ch/phpBB2/viewtopic.php?t=6753.
Especially call your attention to the clause:[quote=“Fine”] . . .
There are a few Win32/VC++ nasty features you should take in account and learn anyway planning your activities. Here you are, with VC++ all libraries your application is going to use must be compiled consistently. That means, for example, ALL of them must be compiled with the “debug” option “on” or “debug” option “off”. You cannot mix easily the DLLs compiled with the “debug” option (for example Qt) and DLL compiled with no debug
For you, this means you have to recompile everythting YOURSELF anyway. This is neither Qt nor QtRoot feature, it is Win32/VC++ “nature” that you have to deal with as soon as you create any application for Windows.
. . . [/quote]