Crash at LeaveCriticalSection function (Windows)

Dear Rooters,
I am running root “Pro, version 5.34/04” on WIN7x64.
The function read entries from TTreeSQL 40x4e6 table (size equivalent to 0.5Gb text file) and Fill their to another TTree to different file. When I copy small amount of entries every thing works well. When the amount greater than ~1e6 the program crash when trying to write tree “fNewTree->Write();”. The debugger breaks on function “LeaveCriticalSection”:

void __cdecl _unlock (
        int locknum
        )
{
        /*
         * leave the critical section.
         */
        LeaveCriticalSection( _locktable[locknum].lock );
}

It looks that I still have a lot of resources to use? Is this a bug or I am doing some thing wrong? Any ideas?
My code as follow:

	sql=new TMySQLServer("mysql://127.0.0.1", "igor", "mypc");
	fCacheTree=new TTreeSQL(sql->db,DB,table);
	fNentry=fCacheTree->GetEntries();
	Init(fCacheTree);

	TFile *fNewFile=new TFile(fname,"recreate");
	TTree *fNewTree = Clone(fNewFile); // create new tree and branches

        fNevent=fNentry;
	while((fCacheTree->GetEntry(fNentry-fNevent))<fNentry)
	{
            Init(fNewTree);
	    fNewTree->Fill();
            fNevent--;
	}

	fNewTree->FlushBaskets();
	fNewTree->Write(); <= CRASH IS HERE!!! 
	fNewFile->Write();
	delete fNewFile;
      Many thanks IGor.

Seems some formats I am not reading correctly.

The code is OK, the process just reaching a memory limit.