TFile declaration causing crash

Hi…
I am writting a code for reading a binary data coming from DAQ of telescope.
The data is being read and write using predefined C-structures. We are trying to
make ROOT Trees from this binary data and save them to .root files. However
My TFile declaration itself seems to be causing memory crash with output like following

./example_root_l2_write write_test.bin write_test1.root
-------------------------PRINT -------------------------------
-------------------------PRINT -------------------------------
*** glibc detected *** ./example_root_l2_write: double free or corruption (!prev): 0x00000000024f97e0 ***
======= Backtrace: =========
/lib64/libc.so.6[0x3625a7cb3e]
/home/sagar/root/lib/libCore.so(_ZN8TStorage13ObjectDeallocEPv+0x78)[0x7f724f55ba58]
/home/sagar/root/lib/libCore.so(_ZN5TList6DeleteEPKc+0xb5)[0x7f724f5d5065]
/home/sagar/root/lib/libCore.so(_ZN5TROOTD1Ev+0xd7)[0x7f724f57b397]
/lib64/libc.so.6(__cxa_finalize+0x9a)[0x3625a3928a]
/home/sagar/root/lib/libCore.so(+0x186be3)[0x7f724f556be3]
======= Memory map: ========
00400000-00404000 r-xp 00000000 fd:04 37362305                           /home/sagar/MACE/Programs/test-programs/example_root_l2_write
00603000-00604000 r--p 00003000 fd:04 37362305                           /home/sagar/MACE/Programs/test-programs/example_root_l2_write
00604000-00605000 rw-p 00004000 fd:04 37362305                           /home/sagar/MACE/Programs/test-programs/example_root_l2_write
00605000-00607000 rw-p 00000000 00:00 0 
0219f000-0253d000 rw-p 00000000 00:00 0                                  [heap]
3625600000-3625620000 r-xp 00000000 fd:00 42991664                       /usr/lib64/ld-2.16.so
3625820000-3625821000 r--p 00020000 fd:00 42991664                       /usr/lib64/ld-2.16.so
3625821000-3625822000 rw-p 00021000 fd:00 42991664                       /usr/lib64/ld-2.16.so
3625822000-3625823000 rw-p 00000000 00:00 0 
3625a00000-3625bad000 r-xp 00000000 fd:00 42991878                       /usr/lib64/libc-2.16.so
3625bad000-3625dad000 ---p 001ad000 fd:00 42991878                       /usr/lib64/libc-2.16.so
3625dad000-3625db1000 r--p 001ad000 fd:00 42991878                       /usr/lib64/libc-2.16.so
3625db1000-3625db3000 rw-p 001b1000 fd:00 42991878                       /usr/lib64/libc-2.16.so
3625db3000-3625db8000 rw-p 00000000 00:00 0 
3626200000-3626216000 r-xp 00000000 fd:00 42992414                       /usr/lib64/libpthread-2.16.so
3626216000-3626416000 ---p 00016000 fd:00 42992414                       /usr/lib64/libpthread-2.16.so
3626416000-3626417000 r--p 00016000 fd:00 42992414                       /usr/lib64/libpthread-2.16.so
3626417000-3626418000 rw-p 00017000 fd:00 42992414                       /usr/lib64/libpthread-2.16.so
3626418000-362641c000 rw-p 00000000 00:00 0 
3626e00000-3626f00000 r-xp 00000000 fd:00 42994378                       /usr/lib64/libm-2.16.so
3626f00000-36270ff000 ---p 00100000 fd:00 42994378                       /usr/lib64/libm-2.16.so
36270ff000-3627100000 r--p 000ff000 fd:00 42994378                       /usr/lib64/libm-2.16.so
3627100000-3627101000 rw-p 00100000 fd:00 42994378                       /usr/lib64/libm-2.16.so
3627600000-3627615000 r-xp 00000000 fd:00 42994451                       /usr/lib64/libgcc_s-4.7.2-20121109.so.1
3627615000-3627814000 ---p 00015000 fd:00 42994451                       /usr/lib64/libgcc_s-4.7.2-20121109.so.1
3627814000-3627815000 r--p 00014000 fd:00 42994451                       /usr/lib64/libgcc_s-4.7.2-20121109.so.1
3627815000-3627816000 rw-p 00015000 fd:00 42994451                       /usr/lib64/libgcc_s-4.7.2-20121109.so.1
362a600000-362a6e5000 r-xp 00000000 fd:00 43001216                       /usr/lib64/libstdc++.so.6.0.17
362a6e5000-362a8e4000 ---p 000e5000 fd:00 43001216                       /usr/lib64/libstdc++.so.6.0.17
362a8e4000-362a8ec000 r--p 000e4000 fd:00 43001216                       /usr/lib64/libstdc++.so.6.0.17
362a8ec000-362a8ee000 rw-p 000ec000 fd:00 43001216                       /usr/lib64/libstdc++.so.6.0.17
362a8ee000-362a903000 rw-p 00000000 00:00 0 
362ae00000-362ae9b000 r-xp 00000000 fd:00 42996046                       /usr/lib64/libfreetype.so.6.9.0
362ae9b000-362b09a000 ---p 0009b000 fd:00 42996046                       /usr/lib64/libfreetype.so.6.9.0
362b09a000-362b0a0000 r--p 0009a000 fd:00 42996046                       /usr/lib64/libfreetype.so.6.9.0
362b0a0000-362b0a1000 rw-p 000a0000 fd:00 42996046                       /usr/lib64/libfreetype.so.6.9.0
363b600000-363b615000 r-xp 00000000 fd:00 42999275                       /usr/lib64/libz.so.1.2.7
363b615000-363b814000 ---p 00015000 fd:00 42999275                       /usr/lib64/libz.so.1.2.7
363b814000-363b815000 r--p 00014000 fd:00 42999275                       /usr/lib64/libz.so.1.2.7
363b815000-363b816000 rw-p 00015000 fd:00 42999275                       /usr/lib64/libz.so.1.2.7
396fc00000-396fc03000 r-xp 00000000 fd:00 42994581                       /usr/lib64/libdl-2.16.so
396fc03000-396fe02000 ---p 00003000 fd:00 42994581                       /usr/lib64/libdl-2.16.so
396fe02000-396fe03000 r--p 00002000 fd:00 42994581                       /usr/lib64/libdl-2.16.so
396fe03000-396fe04000 rw-p 00003000 fd:00 42994581                       /usr/lib64/libdl-2.16.so
7f724ae3e000-7f724ae4a000 r-xp 00000000 fd:00 42991663                   /usr/lib64/libnss_files-2.16.so
7f724ae4a000-7f724b049000 ---p 0000c000 fd:00 42991663                   /usr/lib64/libnss_files-2.16.so
7f724b049000-7f724b04a000 r--p 0000b000 fd:00 42991663                   /usr/lib64/libnss_files-2.16.so
7f724b04a000-7f724b04b000 rw-p 0000c000 fd:00 42991663                   /usr/lib64/libnss_files-2.16.so
7f724b04b000-7f724b053000 rw-p 00000000 00:00 0 
7f724b053000-7f724b78d000 r-xp 00000000 fd:04 36182591                   /home/sagar/root/lib/libGui.so
7f724b78d000-7f724b98c000 ---p 0073a000 fd:04 36182591                   /home/sagar/root/lib/libGui.so
7f724b98c000-7f724b9dc000 r--p 00739000 fd:04 36182591                   /home/sagar/root/lib/libGui.so
7f724b9dc000-7f724b9ea000 rw-p 00789000 fd:04 36182591                   /home/sagar/root/lib/libGui.so
7f724b9ea000-7f724b9fc000 rw-p 00000000 00:00 0 
7f724ba17000-7f724ba1e000 rw-p 00000000 00:00 0 
7f724ba1e000-7f724ba6d000 r-xp 00000000 fd:04 36181689                   /home/sagar/root/lib/libThread.so
7f724ba6d000-7f724bc6c000 ---p 0004f000 fd:04 36181689                   /home/sagar/root/lib/libThread.so
7f724bc6c000-7f724bc70000 r--p 0004e000 fd:04 36181689                   /home/sagar/root/lib/libThread.so
7f724bc70000-7f724bc72000 rw-p 00052000 fd:04 36181689                   /home/sagar/root/lib/libThread.so
7f724bc72000-7f724bc73000 rw-p 00000000 00:00 0 
7f724bc73000-7f724be9a000 r-xp 00000000 fd:04 36181758                   /home/sagar/root/lib/libMathCore.so
7f724be9a000-7f724c09a000 ---p 00227000 fd:04 36181758                   /home/sagar/root/lib/libMathCore.so
7f724c09a000-7f724c0a3000 r--p 00227000 fd:04 36181758                   /home/sagar/root/lib/libMathCore.so
7f724c0a3000-7f724c0a7000 rw-p 00230000 fd:04 36181758                   /home/sagar/root/lib/libMathCore.so
7f724c0a7000-7f724c0af000 rw-p 00000000 00:00 0 
7f724c0af000-7f724c14d000 r-xp 00000000 fd:04 36182817                   /home/sagar/root/lib/libPhysics.so
7f724c14d000-7f724c34c000 ---p 0009e000 fd:04 36182817                   /home/sagar/root/lib/libPhysics.so
7f724c34c000-7f724c34f000 r--p 0009d000 fd:04 36182817                   /home/sagar/root/lib/libPhysics.so
7f724c34f000-7f724c351000 rw-p 000a0000 fd:04 36182817                   /home/sagar/root/lib/libPhysics.so
7f724c351000-7f724c352000 rw-p 00000000 00:00 0 
7f724c352000-7f724c59f000 r-xp 00000000 fd:04 36181946                   /home/sagar/root/lib/libMatrix.so
7f724c59f000-7f724c79e000 ---p 0024d000 fd:04 36181946                   /home/sagar/root/lib/libMatrix.so
7f724c79e000-7f724c7a7000 r--p 0024c000 fd:04 36181946                   /home/sagar/root/lib/libMatrix.so
7f724c7a7000-7f724c7ad000 rw-p 00255000 fd:04 36181946                   /home/sagar/root/lib/libMatrix.so
7f724c7ad000-7f724c7b1000 rw-p 00000000 00:00 0 
7f724c7b1000-7f724c826000 r-xp 00000000 fd:04 36182824                   /home/sagar/root/lib/libPostscript.so
7f724c826000-7f724ca26000 ---p 00075000 fd:04 36182824                   /home/sagar/root/lib/libPostscript.so
7f724ca26000-7f724ca2d000 r--p 00075000 fd:04 36182824                   /home/sagar/root/lib/libPostscript.so
7f724ca2d000-7f724ca2e000 rw-p 0007c000 fd:04 36182824                   /home/sagar/root/lib/libPostscript.so
7f724ca2e000-7f724ca30000 rw-p 00000000 00:00 0 
7f724ca30000-7f724ca62000 r-xp 00000000 fd:04 36182881                   /home/sagar/root/lib/libRint.so
7f724ca62000-7f724cc61000 ---p 00032000 fd:04 36182881                   /home/sagar/root/lib/libRint.so
7f724cc61000-7f724cc63000 r--p 00031000 fd:04 36182881                   /home/sagar/root/lib/libRint.so
7f724cc63000-7f724cc64000 rw-p 00033000 fd:04 36182881                   /home/sagar/root/lib/libRint.so
7f724cc64000-7f724cc65000 rw-p 00000000 00:00 0 
7f724cc65000-7f724ce8b000 r-xp 00000000 fd:04 36182243                   /home/sagar/root/lib/libTree.so
7f724ce8b000-7f724d08a000 ---p 00226000 fd:04 36182243                   /home/sagar/root/lib/libTree.so
7f724d08a000-7f724d099000 r--p 00225000 fd:04 36182243                   /home/sagar/root/lib/libTree.so
7f724d099000-7f724d09e000 rw-p 00234000 fd:04 36182243                   /home/sagar/root/lib/libTree.so
7f724d09e000-7f724d0a2000 rw-p 00000000 00:00 0 
7f724d0a2000-7f724d18a000 r-xp 00000000 fd:04 36182406                   /home/sagar/root/lib/libGpad.so
7f724d18a000-7f724d38a000 ---p 000e8000 fd:04 36182406                   /home/sagar/root/lib/libGpad.so
7f724d38a000-7f724d393000 r--p 000e8000 fd:04 36182406                   /home/sagar/root/lib/libGpad.so
7f724d393000-7f724d396000 rw-p 000f1000 fd:04 36182406                   /home/sagar/root/lib/libGpad.so
7f724d396000-7f724d399000 rw-p 00000000 00:00 0 
7f724d399000-7f724d4b5000 r-xp 00000000 fd:04 36182456                   /home/sagar/root/lib/libGraf3d.so
7f724d4b5000-7f724d6b5000 ---p 0011c000 fd:04 36182456                   /home/sagar/root/lib/libGraf3d.so
7f724d6b5000-7f724d6bf000 r--p 0011c000 fd:04 36182456                   /home/sagar/root/lib/libGraf3d.so
7f724d6bf000-7f724d6c2000 rw-p 00126000 fd:04 36182456                   /home/sagar/root/lib/libGraf3d.so
7f724d6c2000-7f724d6c6000 rw-p 00000000 00:00 0 
7f724d6c6000-7f724d8a4000 r-xp 00000000 fd:04 36182349                   /home/sagar/root/lib/libGraf.so
7f724d8a4000-7f724daa3000 ---p 001de000 fd:04 36182349                   /home/sagar/root/lib/libGraf.so
7f724daa3000-7f724dab0000 r--p 001dd000 fd:04 36182349                   /home/sagar/root/lib/libGraf.so
7f724dab0000-7f724dab6000 rw-p 001ea000 fd:04 36182349                   /home/sagar/root/lib/libGraf.so
7f724dab6000-7f724dac4000 rw-p 00000000 00:00 0 
7f724dac4000-7f724dfb9000 r-xp 00000000 fd:04 36182069                   /home/sagar/root/lib/libHist.so
7f724dfb9000-7f724e1b8000 ---p 004f5000 fd:04 36182069                   /home/sagar/root/lib/libHist.so
7f724e1b8000-7f724e1e0000 r--p 004f4000 fd:04 36182069                   /home/sagar/root/lib/libHist.so
7f724e1e0000-7f724e1e7000 rw-p 0051c000 fd:04 36182069                   /home/sagar/root/lib/libHist.so
7f724e1e7000-7f724e1f1000 rw-p 00000000 00:00 0 
7f724e1f1000-7f724e338000 r-xp 00000000 fd:04 36181980                   /home/sagar/root/lib/libNet.so
7f724e338000-7f724e537000 ---p 00147000 fd:04 36181980                   /home/sagar/root/lib/libNet.so
7f724e537000-7f724e543000 r--p 00146000 fd:04 36181980                   /home/sagar/root/lib/libNet.so
7f724e543000-7f724e547000 rw-p 00152000 fd:04 36181980                   /home/sagar/root/lib/libNet.so
7f724e547000-7f724e54a000 rw-p 00000000 00:00 0 
7f724e54a000-7f724e83c000 r-xp 00000000 fd:04 36181754                   /home/sagar/root/lib/libRIO.so
7f724e83c000-7f724ea3c000 ---p 002f2000 fd:04 36181754                   /home/sagar/root/lib/libRIO.so
7f724ea3c000-7f724ea47000 r--p 002f2000 fd:04 36181754                   /home/sagar/root/lib/libRIO.so
7f724ea47000-7f724ea4b000 rw-p 002fd000 fd:04 36181754                   /home/sagar/root/lib/libRIO.so
7f724ea4b000-7f724ea4e000 rw-p 00000000 00:00 0 
7f724ea4e000-7f724ec8f000 r-xp 00000000 fd:04 36179809                   /home/sagar/root/lib/libCint.so
7f724ec8f000-7f724ee8f000 ---p 00241000 fd:04 36179809                   /home/sagar/root/lib/libCint.so
7f724ee8f000-7f724ee92000 r--p 00241000 fd:04 36179809                   /home/sagar/root/lib/libCint.so
7f724ee92000-7f724ee97000 rw-p 00244000 fd:04 36179809                   /home/sagar/root/lib/libCint.so
7f724ee97000-7f724f3d0000 rw-p 00000000 00:00 0 
7f724f3d0000-7f724faf5000 r-xp 00000000 fd:04 36181468                   /home/sagar/root/lib/libCore.so
7f724faf5000-7f724fcf4000 ---p 00725000 fd:04 36181468                   /home/sagar/root/lib/libCore.so
7f724fcf4000-7f724fd1e000 r--p 00724000 fd:04 36181468                   /home/sagar/root/lib/libCore.so
7f724fd1e000-7f724fd2c000 rw-p 0074e000 fd:04 36181468                   /home/sagar/root/lib/libCore.so
7f724fd2c000-7f724fdae000 rw-p 00000000 00:00 0 
7fff4ae59000-7fff4ae92000 rw-p 00000000 00:00 0                          [stack]
7fff4af58000-7fff4af5a000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)

Without your source code, it is rather impossible to help.

I am attaching the source code and header file.
It crashes even after exit(1) statement just after TFile declaration.
for testing run
./example_l2_root_write write_test.txt write_test.root
write_test.txt (714 KB)
telemetry_modified.h (5.36 KB)
example_root_l2_write.cxx (2.76 KB)

Sorry. I didn’t mention. I am using g++ to compile above code to make executable of name
example_root_l2_write

In your “example_root_l2_write.cxx” file, move the line:
#include "telemetry_modified.h"
to the place directly before you define the “routine” (i.e. to a place after all another #include lines).

BTW. I get a whole bunch of warnings when I try to compile your code: [...]$ `root-config --cxx --cflags` -O3 -W -Wall -o example_root_l2_write example_root_l2_write.cxx `root-config --libs` In file included from example_root_l2_write.cxx:40:0: telemetry_modified.h:36:1: warning: ‘typedef’ was ignored in this declaration [enabled by default] }; ^ telemetry_modified.h:60:1: warning: ‘typedef’ was ignored in this declaration [enabled by default] }; ^ telemetry_modified.h:71:1: warning: ‘typedef’ was ignored in this declaration [enabled by default] }; ^ telemetry_modified.h:83:1: warning: ‘typedef’ was ignored in this declaration [enabled by default] }; ^ telemetry_modified.h:91:1: warning: ‘typedef’ was ignored in this declaration [enabled by default] }; ^ example_root_l2_write.cxx: In function ‘int main(int, char**)’: example_root_l2_write.cxx:49:8: warning: unused variable ‘Nmodules’ [-Wunused-variable] Int_t Nmodules = 4 ; ^ example_root_l2_write.cxx:50:8: warning: unused variable ‘Nchannels’ [-Wunused-variable] Int_t Nchannels = 64 ; ^ example_root_l2_write.cxx:56:9: warning: unused variable ‘l2f’ [-Wunused-variable] TFile *l2f = new TFile(argv[2], "recreate"); ^ example_root_l2_write.cxx: At global scope: example_root_l2_write.cxx:43:5: warning: unused parameter ‘argc’ [-Wunused-parameter] int main(int argc, char *argv[]) ^ example_root_l2_write.cxx: In function ‘int main(int, char**)’: example_root_l2_write.cxx:68:40: warning: ignoring return value of ‘size_t fread(void*, size_t, size_t, FILE*)’, declared with attribute warn_unused_result [-Wunused-result] fread(&head,sizeof(head),1,ifp); ^ example_root_l2_write.cxx:73:46: warning: ignoring return value of ‘size_t fread(void*, size_t, size_t, FILE*)’, declared with attribute warn_unused_result [-Wunused-result] fread(&routine,sizeof(routine),1,ifp); ^ [...]$

Thanks. That works. I also removed warnings by properly ‘typedef’-ing structures.
I don’t understand though why sequence of #include statements should matter. What
is the reason ? I have long way to go in this code and answer may be important for
me.

You should take into account repercussions, if you’re globally modifying structure packing (note that, with gcc, you should be able to attach the attribute((packed, aligned(x))) to individual structure declarations, instead).

Okk… Thank you. Now I will try to keep structure packing local to file.