[NeXus-code-tickets] [NeXusCode] #262: Segmentation fault NeXus C-API ver 4.2.1

NeXus Data Format Library and Applications noreply at nexusformat.org
Wed Nov 17 09:24:42 GMT 2010


#262: Segmentation fault NeXus C-API ver 4.2.1
-------------------------+--------------------------------------------------
 Reporter:  Jiro Suzuki  |       Owner:  Unassigned
     Type:  defect       |      Status:  new       
 Priority:  major        |   Milestone:            
Component:  napi         |     Version:  4.2.0     
 Keywords:               |  
-------------------------+--------------------------------------------------
 Hi,
 I'm Jiro Suzuki (KEK/J-PARC).

 We have used NeXus C-API in the C++ class library, Manyo-Lib.
 In this month, we updated NeXus C-API in our computing environment
 from ver. 4.2.0 to 4.2.1.

 Unfortunately we receive segmentation fault from our software
 which includes NeXus C-API ver. 4.2.1.
 The software is working well with ver. 4.2.0.
 The NeXus files written with the API ver. 4.2.1
 can read with the API ver 4.2.0.

 To try to find the cause of the error,
 I wrote test code in C++ attached this e-mail.

 If many number of arrays (about 50 or more) are written in a directory
 of NeXus files, the file cannot read with the C-API with 4.2.1.
 The type of array in the test code is double, and its size is 50.

 The number of arrays in the directory is N,
 and the size of each array is M in the test code.


 The following is our software environment.
 Ubuntu Linux 10.04 LTS 64-bit
 gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
 HDF5 ver.1.8.4 (Ubuntu-package) (not changed)

 Thanks,
   Jiro SUZUKI

 ///////////////////////////////////////////////
 #include <stdlib.h>
 #include <iostream>
 #include <cstdlib>
 #include <vector>
 #include <list>
 #include <string>
 #include <math.h>
 #include <fstream>
 #include <stdio.h>
 #include <stdarg.h>
 #include <ctype.h>
 #include <string.h>

 using namespace std;
 typedef char               Char;
 typedef unsigned char      UChar;
 typedef short              Int2;
 typedef unsigned short     UInt2;
 typedef int                Int4;
 typedef unsigned int       UInt4;
 typedef long long          Int8;
 typedef unsigned long long UInt8;
 typedef double             Double;
 typedef float              Float;
 typedef bool               Bool;

 #include "napi.h"

 string UInt4ToString( UInt4 i );
 void MakeOpenGroup( NXhandle handle, string Name, string Class );
 vector<string> GetNextEntry( NXhandle handle );
 void OpenGroup( NXhandle handle, string GroupName, string GroupClass );
 NXstatus OpenData( NXhandle handle, string DataSetName );
 vector<Int4> GetInfo( NXhandle handle );
 UInt4 GroupItemNum( NXhandle handle, string Name, string ClassName );
 ////////////////////////////////////////
 int main(){

   UInt4 N = 100;
   // the number of arrays in a directory

   UInt4 M = 50;
   // the size of each array

   ////////////////////////////////////////////////

   string OutPutFileName = "nx.nx";
   string UserName = "suzuki";
   NXhandle handle;

   NXopen( OutPutFileName.c_str(), NXACC_CREATE5, &handle );
   NXputattr( handle, "user_name", (void*)UserName.c_str(),
              UserName.size(), NX_CHAR );

   MakeOpenGroup( handle, "Entry1", "NXentry" );
   MakeOpenGroup( handle, "Data1", "NXdata" );

   for( UInt4 n=0; n<N; n++ ){
     string VectorName = "x" + UInt4ToString( n );
     vector<Double> vd = vector<Double>( M );
     for( UInt4 m=0; m<M; m++ ){ vd[ m ] = (Double)m; }

     Int4 rank = 1;
     Int4 ArrayDemDef = vd.size();
     Double *VecPointer = &( vd[0] );
     NXmakedata( handle, VectorName.c_str(),
                 NX_FLOAT64, rank, &ArrayDemDef );
     NXopendata( handle, VectorName.c_str() );
     NXputdata( handle, (void*)( VecPointer ) );
     NXclosedata( handle );
   }

   NXclosegroup( handle );
   NXclosegroup( handle );
   NXclose( &handle );

   ////////////////////////////////////////////////////

   vector<string> vs;

   NXhandle Rhandle;
   NXopen( OutPutFileName.c_str(), NXACC_RDWR, &Rhandle );
   vs = GetNextEntry( Rhandle );
   OpenGroup( Rhandle, vs[0], vs[1] );
   vs = GetNextEntry( Rhandle );
   OpenGroup( Rhandle, vs[0], vs[1] );

   UInt4 ItemNum = GroupItemNum( Rhandle, vs[0], vs[1] );

   for( UInt4 i=0; i<ItemNum; i++ ){

     vs = GetNextEntry( Rhandle );
     OpenData( Rhandle, vs[0] );
     vector<Int4> v = GetInfo( Rhandle );
     UInt4 size = (UInt4)( v[0] );
     Double *array = new Double [ size ];
     NXgetdata( Rhandle, array );

     cout << vs[0] << "\t" <<  size << endl;
     delete [] array;
   }


 } // End of MAIN function
 //////////////////////////////////////
 void MakeOpenGroup( NXhandle handle, string Name, string Class )
 {
   NXmakegroup( handle, Name.c_str(), Class.c_str() );
   NXopengroup( handle, Name.c_str(), Class.c_str() );
 }
 //////////////////////////////////////
 UInt4 GroupItemNum( NXhandle handle, string Name, string ClassName ){
   Int4 ItemNum;
   Char *CName = new Char [256];
   Char *CClassName = new Char [256];
   strcpy( CName, Name.c_str() );
   strcpy( CClassName, ClassName.c_str() );

   const char *ConCName = CName;
   const char *ConCClassName = CClassName;

   NXgetgroupinfo( handle, &ItemNum, CName, CClassName );
   delete [] CName;
   delete [] CClassName;
   return ItemNum;
 }
 //////////////////////////////////////
 vector<string> GetNextEntry( NXhandle handle )
 {
   vector<string> vs;
   vs.clear();
   Int4 Number;
   Char GroupName[256];
   Char GroupClass[256];
   NXgetnextentry( handle, GroupName, GroupClass, &Number );
   vs.push_back( string( GroupName  ) );
   vs.push_back( string( GroupClass ) );
   return vs;
 }
 //////////////////////////////////////
 void OpenGroup( NXhandle handle, string GroupName, string GroupClass ){
   NXopengroup( handle, GroupName.c_str(), GroupClass.c_str() );
 }
 //////////////////////////////////////
 NXstatus OpenData( NXhandle handle, string DataSetName ){
   return NXopendata( handle, DataSetName.c_str() );
 }
 //////////////////////////////////////
 vector<Int4> GetInfo( NXhandle handle )
 {
   Int4 Rank;
   Int4 *Dimension = new Int4 [ 256 ];
   Int4 DataType;

   NXgetinfo( handle, &Rank, Dimension, &DataType );
   vector<Int4> v;
   v.clear();
   v.push_back( Dimension[0] );
   v.push_back( DataType );
   delete [] Dimension;
   return v;
 }
 //////////////////////////////////////
 string UInt4ToString( UInt4 i )
 {
   Char *c = new Char [16];
   sprintf( c, "%d", i );
   string s = string( c );
   delete [] c;
   return s;
 }
 ///////////////////////////////////////

-- 
Ticket URL: <http://trac.nexusformat.org/code/ticket/262>
NeXus Data Format Library and Applications <http://www.nexusformat.org/>
NeXus Data Format Library and Applications



More information about the NeXus-code-tickets mailing list