Tree in c++ program, segmentation violation

Hi,

I put a tree in my c++ program and use it to store myclass data. I can build my program. But when I run it, I got the following error message:

I don’t understand the message. One of my guess is my program use a class which implements its own threads. Root tree may have its own thread implementation. Do they have conflicts? I may be totally wrong about this. Thank you very much for your helps.

cheer,

gma

IMHO Before you start guess-work, you’d better:
1)write the smallest C++ code which is similar to yours and can reproduce your problem and
2) show this code here.

Hi,
I see your points. My program is as follows:

[code]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <OTClient.h>
#include “TTree.h”
#include “TFile.h”
#include “TBranch.h”
#include “OTHistData.h”

class MyClient : public OTClient
{
int i,i1;

public:
int id;
int id1;
int id2;
TTree *tree;
TFile f;
OTHistData
h;
int counter;

MyClient ()
{
  i = i1 = 0;
  
  f = new TFile("/home/gma/c++program/test.root","RECREATE");
  tree = new TTree("T","Test of tree");
  h = new OTHistData();
  tree->Branch("histdata","Transaction", &h,3200,99);
  counter =0;

}

virtual ~MyClient ()
{
 //tree->Write();
 delete tree; 
 delete f;
 delete h;

}

void onLogin () 
{
  printf ("login ok\n");
 //requestListExchanges ();

 // id = requestTickStream (&OTDataEntity ("Q", "CSCO"));
         
  struct tm begDate = { 0, 0, 9, 15, 11, 104 };
  struct tm endDate = { 0, 0, 9, 25, 11, 104 };
                                     
  requestHistData (&OTDataEntity ("Q", "MSFT"),
                   mktime(&begDate),
                   mktime(&endDate),
                   OT_HIST_RAW_TICKS,
                   1);
}

void onError (OTError * error) 
{
  printf ("-----> Error id=%d, code=%d, error=%s\n", 
  error->getRequestId(), error->getCode(), error->getDescription());      
}



void onHistTrade (OTTrade *trade)
{


  h->price = trade->getPrice();
  h->volume = trade->getVolume();
  h->timestamp = trade->getTimestamp();
  tree->Fill();
  tree->Write();

  printf ("HistTrade: id = %d, timestamp = %d, price = %f, size = %d, volume = %lld ",
           trade->getRequestId(), trade->getTimestamp(), trade->getPrice(), trade->getSize(), trade->getVolume()
         );
                              
  printf ("sequenceNumber = %d, indicator = %c, tickIndicator = %c, isOpen = %d, isHigh = %d, isLow = %d, isClose = %d, isUpdateLast = %d \
          isUpdateVolume = %d, isCancel = %d, isFromBook = %d \n", trade->getSequenceNumber(), trade->getIndicator(), trade->getTickIndicator(),
          trade->isOpen(), trade->isHigh(), trade->isLow(), trade->isClose(), 
          trade->isUpdateLast(), trade->isUpdateVolume(), trade->isCancel(), trade->isFromBook()
       );
}

};

int main()
{
MyClient *client = new MyClient();

client->addHost (“feed1.opentick.com”, 10010);

char login[255] = “”;
char passw[255] = “”;

printf (“Please, enter your login: “);
scanf (”%254s”, login);

printf (“Please, enter your password: “);
scanf (”%254s”, passw);

printf (“trying to log in…\n”);
getchar();
client->login (login, passw);

getchar();

delete client;
return 0;
}

[\code]

Attached please find the necessary header and library files which are needed to compile the problem. Thank you for your helps.

cheers,

gma[/code]
ot.zip (1.08 MB)

Please provide the shortest possible (but running) version of your code,
removing all the unnecessary stuff. We cannot use your shared libs.

Rene

Rene,
Thank for your reply. I will try to get the minimized code. However, the class I am using is developed by the vendor, which is a trunk of large code. It is not easy for me to break it up.

Anyhow, I hope the following detailed debug information can help you give me some clue where I cam wrong.

The problem I found came from the class constructor, Myclient(). In particular,

h = new OTHistData();
tree->Branch("histdata", "histdata", &h, 3200,99);

for h, the value is 0X747640. The problem is from tree-:Branch statement. Basically, the debugger took me to the template function of TBrance * Branch() in TTree.h. After I executed the function of BranchImp, it gives me the error message of

Thank you very much for your helps.

regards,

gma

btw, another piece of information is that I am using root 5.17.04. Thanks.

gma

You must create a dictionary for your class OTHistData (or/and any class that will be
stored in the Tree.
You must replace the line in your MyClient class

tree->Branch("histdata","Transaction", &h,3200,99); by

tree->Branch("histdata","OTHistData", &h,3200,99); or

tree->Branch("histdata", &h,3200,99);
To create a dictionary, use rootcint on all your header files in the Tree.
see examplse in $ROOTSYS/test/Makefile

Rene

Rene,
Thank for your helps. Your answer solved my problem. Now the program is running. The new problem is that I cann’t read the created root file.

When I do the following

TFile* f = new TFile("test.root")

ROOT basically was frozen. I checked the size of the created root file, It is unusually large, about 500M.

I began to suspect my way of using the tree. From my code, in the method onHistTrade (OTTrade *trade), you may find the following codes:

// Tree as a sink

 h->price = trade->getPrice();
      h->volume = trade->getVolume();
      h->timestamp = trade->getTimestamp();
      tree->Fill();
      tree->Write(); 

     // stdout as the sink
      printf ("HistTrade: id = %d, timestamp = %d, price = %f, size = %d, volume = %lld ",
               trade->getRequestId(), trade->getTimestamp(), trade->getPrice(), trade->getSize(), trade->getVolume()
             );

What I intend to do is to use tree as a sink instead of stdout. You can see that I capture the data, put them into the tree and write to the file. I am not sure whether this is the right way to use the tree in my case. Thank you very much if you can clarify this.

regards,

gma