ROOT code on Windows behaves very differently if interpreted by CINT or compiled in VS

I am working on Windows with VS 2013 and the latest debug version of root 5.34.34, I have some code that runs perfectly as an interpreted macro but I can’t make it work in a compiled program in VS. The code builds correctly, but it doesn’t do what it should.

In the attached example I’m trying to read a root file containing an NTuple (called “pjmca”) and copying its data as TH1F in a new root file. Notice that at the beginning I have commented

//TApplication *a = new TApplication("a", 0, 0);

If you leave that as it is, you’ll notice that the program finds that pjmca has 0 entries and therefore doesn’t copy anything in the new TH1Fs. If instead you uncomment that line, GetEntries() works fine, but after 4-5 iterations of the for loop all my variables change their values at random as if some pointer had invaded them and changed them.

The fun part is that if you take that read function in a new file (without TApplication) and interpret it as a macro it all works perfectly.

On other operating systems this code seems to work both as a compiled program and as a macro, but is there anything I can do to make it work on Windows with VS?

EDIT: posted a cleaner version of main.cpp with less commented out lines that I used to switch quickly between problematic commands
Main.cpp (1.23 KB)
CampioneIgnotoBanana900.root (53.4 KB)

read.cxx (1.91 KB)

Thanks for the help, it works. But without TApplication, GetEntries() keeps returning 0. Do you know how TApplication affects that?