diff --git a/stim/envi/agilent_binary.h b/stim/envi/agilent_binary.h index b103e68..bf42c8a 100644 --- a/stim/envi/agilent_binary.h +++ b/stim/envi/agilent_binary.h @@ -38,6 +38,7 @@ public: } void alloc(){ if (ptr != NULL) free(ptr); + ptr = NULL; ptr = (T*) malloc(bytes()); } void alloc(size_t x, size_t y, size_t z){ @@ -64,13 +65,14 @@ public: /// Default constructor, sets the resolution to zero and the data pointer to NULL agilent_binary(){ - memset(R, 0, sizeof(size_t) * 3); //set the resolution to zero - memset(Z, 0, sizeof(double) * 2); ptr = NULL; + memset(R, 0, sizeof(size_t) * 3); //set the resolution to zero + memset(Z, 0, sizeof(double) * 2); } /// Constructor with resolution agilent_binary(size_t x, size_t y, size_t z){ + ptr = NULL; alloc(x, y, z); memset(Z, 0, sizeof(double) * 2); } @@ -84,6 +86,7 @@ public: /// Copy constructor agilent_binary(const agilent_binary &obj){ + ptr = NULL; deep_copy(this, &obj); } @@ -100,33 +103,37 @@ public: } ~agilent_binary(){ - free(ptr); + if(ptr != NULL) + free(ptr); } void load(std::string filename){ - if(ptr != NULL) free(ptr); //if memory has been allocated, free it + if(ptr != NULL) free(ptr); //if memory has been allocated, free it + ptr = NULL; - fname = filename; //save the filename + fname = filename; //save the filename short x, y, z; std::ifstream infile(fname, std::ios::binary); //open the input file - infile.seekg(9, std::ios::beg); //seek past 9 bytes from the beginning of the file + if (infile) { + infile.seekg(9, std::ios::beg); //seek past 9 bytes from the beginning of the file - infile.read((char*)(&z), 2); //read two bytes of data (the number of samples is stored as a 16-bit integer) + infile.read((char*)(&z), 2); //read two bytes of data (the number of samples is stored as a 16-bit integer) - infile.seekg(13, std::ios::cur); //skip another 13 bytes - infile.read((char*)(&x), 2); //read the X and Y dimensions - infile.read((char*)(&y), 2); + infile.seekg(13, std::ios::cur); //skip another 13 bytes + infile.read((char*)(&x), 2); //read the X and Y dimensions + infile.read((char*)(&y), 2); - infile.seekg(header, std::ios::beg); //seek to the start of the data + infile.seekg(header, std::ios::beg); //seek to the start of the data - alloc(x, y, z); - ptr = (T*) malloc(bytes()); //allocate space for the data - infile.read((char*)ptr, bytes()); //read the data - infile.close(); - Z[0] = 1; - Z[1] = (double)R[2]; + alloc(x, y, z); + //ptr = (T*)malloc(bytes()); //allocate space for the data + infile.read((char*)ptr, bytes()); //read the data + infile.close(); + Z[0] = 1; + Z[1] = (double)R[2]; + } } void save(std::string filename){ -- libgit2 0.21.4