TH3::FitSlicesZ behaving differently in macro (.C) as in ROOT console

Dear Root users,
I need to use FitSlicesZ(), and created an example macro to see if it is working:

TF3 *gaus3d = new TF3("gaus3d", "[0]*TMath::Gaus(x, [1], [2])*TMath::Gaus(y, [3], [4])*TMath::Gaus(z, [5], [6])", -5, 5, -5, 5, -5, 5);
gaus3d->SetParameters(1, 0, 1, 0, 1, 0, 1);
TH3 *h3 = new TH3D("h3", "title", 50, -5, 5, 50, -5, 5, 50, -5, 5);
h3->FillRandom("gaus3d", 10000);
TFile rootfile("hisTest.root", "RECREATE");;

This runs as expected in the Root console. Creates hisTest.root file, with the TH2D histograms h3_0, h3_1, h3_2, h3_chi2, and also the original TH3D h3 in it.

However, running the exact same code (even after deleting the created root file) as a macro (named testHist.C, for example), i.e. inside a void function as follows:

void testHist(){
//previous code here


.x testHist.C

results in a .root file that has h3 in it, nothing else.
How can I achieve the same result with the macro file (creation of a .root file with the TH2 plots also in it)?
I thought the function’s source code might help, but I have the suspicion I won’t find the reason for this behaviour in it.