Commit 2097dd82f7b3544e23bd5819da8aa34320ef0845

Authored by David Mayerich
1 parent 629c754e

fixed agilent_binary memory leak

Showing 1 changed file with 24 additions and 17 deletions   Show diff stats
stim/envi/agilent_binary.h
@@ -38,6 +38,7 @@ public: @@ -38,6 +38,7 @@ public:
38 } 38 }
39 void alloc(){ 39 void alloc(){
40 if (ptr != NULL) free(ptr); 40 if (ptr != NULL) free(ptr);
  41 + ptr = NULL;
41 ptr = (T*) malloc(bytes()); 42 ptr = (T*) malloc(bytes());
42 } 43 }
43 void alloc(size_t x, size_t y, size_t z){ 44 void alloc(size_t x, size_t y, size_t z){
@@ -64,13 +65,14 @@ public: @@ -64,13 +65,14 @@ public:
64 65
65 /// Default constructor, sets the resolution to zero and the data pointer to NULL 66 /// Default constructor, sets the resolution to zero and the data pointer to NULL
66 agilent_binary(){ 67 agilent_binary(){
67 - memset(R, 0, sizeof(size_t) * 3); //set the resolution to zero  
68 - memset(Z, 0, sizeof(double) * 2);  
69 ptr = NULL; 68 ptr = NULL;
  69 + memset(R, 0, sizeof(size_t) * 3); //set the resolution to zero
  70 + memset(Z, 0, sizeof(double) * 2);
70 } 71 }
71 72
72 /// Constructor with resolution 73 /// Constructor with resolution
73 agilent_binary(size_t x, size_t y, size_t z){ 74 agilent_binary(size_t x, size_t y, size_t z){
  75 + ptr = NULL;
74 alloc(x, y, z); 76 alloc(x, y, z);
75 memset(Z, 0, sizeof(double) * 2); 77 memset(Z, 0, sizeof(double) * 2);
76 } 78 }
@@ -84,6 +86,7 @@ public: @@ -84,6 +86,7 @@ public:
84 86
85 /// Copy constructor 87 /// Copy constructor
86 agilent_binary(const agilent_binary<T> &obj){ 88 agilent_binary(const agilent_binary<T> &obj){
  89 + ptr = NULL;
87 deep_copy(this, &obj); 90 deep_copy(this, &obj);
88 } 91 }
89 92
@@ -100,33 +103,37 @@ public: @@ -100,33 +103,37 @@ public:
100 } 103 }
101 104
102 ~agilent_binary(){ 105 ~agilent_binary(){
103 - free(ptr); 106 + if(ptr != NULL)
  107 + free(ptr);
104 } 108 }
105 109
106 void load(std::string filename){ 110 void load(std::string filename){
107 - if(ptr != NULL) free(ptr); //if memory has been allocated, free it 111 + if(ptr != NULL) free(ptr); //if memory has been allocated, free it
  112 + ptr = NULL;
108 113
109 - fname = filename; //save the filename 114 + fname = filename; //save the filename
110 115
111 short x, y, z; 116 short x, y, z;
112 117
113 std::ifstream infile(fname, std::ios::binary); //open the input file 118 std::ifstream infile(fname, std::ios::binary); //open the input file
114 - infile.seekg(9, std::ios::beg); //seek past 9 bytes from the beginning of the file 119 + if (infile) {
  120 + infile.seekg(9, std::ios::beg); //seek past 9 bytes from the beginning of the file
115 121
116 - infile.read((char*)(&z), 2); //read two bytes of data (the number of samples is stored as a 16-bit integer) 122 + infile.read((char*)(&z), 2); //read two bytes of data (the number of samples is stored as a 16-bit integer)
117 123
118 - infile.seekg(13, std::ios::cur); //skip another 13 bytes  
119 - infile.read((char*)(&x), 2); //read the X and Y dimensions  
120 - infile.read((char*)(&y), 2); 124 + infile.seekg(13, std::ios::cur); //skip another 13 bytes
  125 + infile.read((char*)(&x), 2); //read the X and Y dimensions
  126 + infile.read((char*)(&y), 2);
121 127
122 - infile.seekg(header, std::ios::beg); //seek to the start of the data 128 + infile.seekg(header, std::ios::beg); //seek to the start of the data
123 129
124 - alloc(x, y, z);  
125 - ptr = (T*) malloc(bytes()); //allocate space for the data  
126 - infile.read((char*)ptr, bytes()); //read the data  
127 - infile.close();  
128 - Z[0] = 1;  
129 - Z[1] = (double)R[2]; 130 + alloc(x, y, z);
  131 + //ptr = (T*)malloc(bytes()); //allocate space for the data
  132 + infile.read((char*)ptr, bytes()); //read the data
  133 + infile.close();
  134 + Z[0] = 1;
  135 + Z[1] = (double)R[2];
  136 + }
130 } 137 }
131 138
132 void save(std::string filename){ 139 void save(std::string filename){