fix segfault

This patch provides a memory-safe alternative to the changes introduced in commit 418adfde670b8d911a6a313e11a2ee4fc98feeee, which causes a segfault when the "batch mode" flag is required (i.e., for ascii/graphic export).

Note: the program name (i.e., argv[0]) has been added the list arguments passed to ROOT's TApplication. This ensures that the TApplication name matches that of the program (see 542b98b2cc/core/base/src/TApplication.cxx (L179-L180)).
This commit is contained in:
Ryan M. L. McFadden 2024-06-17 10:51:44 -03:00
parent b71dce9291
commit 61749b91c4
2 changed files with 15 additions and 15 deletions

View File

@ -1437,19 +1437,18 @@ Int_t main(Int_t argc, Char_t *argv[])
musrFT_dumpData(startupParam.dumpFln, fourier, startupParam.fourierRange[0], startupParam.fourierRange[1]); musrFT_dumpData(startupParam.dumpFln, fourier, startupParam.fourierRange[0], startupParam.fourierRange[1]);
} else { // do Canvas } else { // do Canvas
// if Fourier graphical export is whished, switch to batch mode // if Fourier graphical export is wished, switch to batch mode
Bool_t batch = false; Bool_t batch = false;
int cc=0; // create list of essential arguments to pass to the ROOT application
char **arg; std::vector<char*> args;
args.push_back(argv[0]); // program name
if (startupParam.graphicFormat.Length() != 0) { if (startupParam.graphicFormat.Length() != 0) {
batch = true; batch = true;
arg[cc] = (Char_t*)malloc(16*sizeof(Char_t)); args.push_back((char*)"-b"); // batch mode flag
strcpy(arg[cc], "-b");
cc++;
} }
int cc = args.size();
// plot the Fourier transform // plot the Fourier transform
TApplication app("App", &cc, arg); TApplication app("App", &cc, args.data());
if (startupHandler) { if (startupHandler) {
fourierCanvas = std::unique_ptr<PFourierCanvas>(new PFourierCanvas(fourier, dataSetTag, startupParam.title.Data(), fourierCanvas = std::unique_ptr<PFourierCanvas>(new PFourierCanvas(fourier, dataSetTag, startupParam.title.Data(),

View File

@ -37,6 +37,7 @@
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <vector>
#include <TApplication.h> #include <TApplication.h>
#include <TSAXParser.h> #include <TSAXParser.h>
@ -301,15 +302,15 @@ int main(int argc, char *argv[])
} }
if (result == PMUSR_SUCCESS) { if (result == PMUSR_SUCCESS) {
// generate Root application needed for PMusrCanvas // create the ROOT application needed for PMusrCanvas
int cc=0; // and pass it only essential arguments
char **arg; std::vector<char*> args;
args.push_back(argv[0]); // program name
if (graphicsOutput || asciiOutput) { if (graphicsOutput || asciiOutput) {
arg[cc] = (char*)malloc(16*sizeof(char)); args.push_back((char*)"-b"); // batch mode flag
strcpy(arg[cc], "-b");
cc++;
} }
TApplication app("App", &cc, arg); int cc = args.size();
TApplication app("App", &cc, args.data());
std::vector<PMusrCanvas*> canvasVector; std::vector<PMusrCanvas*> canvasVector;
PMusrCanvas *musrCanvas; PMusrCanvas *musrCanvas;