TGFileDialog not working

Hi,

I’m trying to use the TGFileDialog to get a filename, but when I click the “open” button the dialog window freezes and never returns:

TGFileInfo file_info_; const char *filetypes[] = {"RAW images", "*.raw", 0, 0}; file_info_.fFileTypes = filetypes; file_info_.fIniDir = StrDup("."); new TGFileDialog(this, this, kFDOpen, &file_info_);

If don’t allocate it with new, it crashes altogether.

Hi,

Could you give more info how you run your code?
Which version of ROOT? On which OS? When running a macro? From a compiled application?
For example, this code:

void open_file() { TGFileInfo file_info_; const char *filetypes[] = {"RAW images", "*.raw", 0, 0}; file_info_.fFileTypes = filetypes; file_info_.fIniDir = StrDup("."); new TGFileDialog(gClient->GetDefaultRoot(), gClient->GetDefaultRoot(), kFDOpen, &file_info_); if( file_info_.fFilename ) cout << "'" << file_info_.fFilename << "' selected." << endl; }
Works just fine from the ROOT prompt on Windows with 5.34.05. It gives:

C:\Users\bellenot\rootdev>root -l root [0] .x open_file.C 'C:/Users/bellenot/rootdev/graph_with_law.raw' selected. root [1]
Or provide a running macro showing the problem.

Cheers, Bertrand.

Hi Bertrand,

I am running a compiled application with ROOT 5.34/03 on the latest Ubuntu.
The following is a minimal example that crashes:

[code]#include

#include “TApplication.h”
#include “TGFileDialog.h”

using namespace std;

int main(int argc, char **argv) {
TApplication app(“app”, &argc, argv);
TGFileInfo file_info_;
const char filetypes[] = {“RAW images”, ".raw", 0, 0};
file_info_.fFileTypes = filetypes;
file_info_.fIniDir = StrDup(".");
TGFileDialog dialog(gClient->GetDefaultRoot(), gClient->GetDefaultRoot(),
kFDOpen, &file_info_);
if( file_info_.fFilename )
cout << “’” << file_info_.fFilename << “’ selected.” << endl;
app.Run();
return 0;
} [/code]

Compiled with:

g++ -Wall `root-config --cflags` -o trying_file_dialog trying_file_dialog.cpp `root-config --glibs`

Hi,

OK, sorry, I overlooked at your first post. You have to use the operator new, and cannot delete the dialog.
For example, this would crash too:

TGFileDialog *dlg = new dialog(gClient->GetDefaultRoot(), gClient->GetDefaultRoot(), kFDOpen, &file_info_); delete dlg;Since creating it on the stack has the same effect than calling delete when going out of scope.
This is because the dialog is deleted when closing the window, so then trying to delete it again lead to a crash.

Cheers, Bertrand.

Alright, I didn’t know that. Wouldn’t it be a good idea to give, as usual, the caller of new the responsibility to delete the object?

Moreover, inside a TGMainFrame, it has to be initialized with gClient->GetRoot() as first argument: