Previous 15994 Revisions Next

r2569 Wednesday 7th May, 2008 at 11:21:27 UTC by Nathan Woods
Fixed memory leak in hard drive code
[src/mess/devices]harddriv.c

src/mess/devices/harddriv.c
r2568r2569
7676
7777struct mess_hd
7878{
79   chd_file *chd;
7980   hard_disk_file *hard_disk_handle;
8081};
8182
r2568r2569
116117{
117118   chd_error err = 0;
118119   struct mess_hd *hd;
119   chd_file *chd;
120120   int is_writeable;
121121   int id = image_index_in_device(image);
122122
r2568r2569
126126   do
127127   {
128128      is_writeable = image_is_writable(image);
129      chd = NULL;
130      err = chd_open_file(image_core_file(image), is_writeable ? CHD_OPEN_READWRITE : CHD_OPEN_READ, NULL, &chd);
129      hd->chd = NULL;
130      err = chd_open_file(image_core_file(image), is_writeable ? CHD_OPEN_READWRITE : CHD_OPEN_READ, NULL, &hd->chd);
131131
132132      /* special case; if we get CHDERR_FILE_NOT_WRITEABLE, make the
133133       * image read only and repeat */
134134      if (err == CHDERR_FILE_NOT_WRITEABLE)
135135         image_make_readonly(image);
136136   }
137   while(!chd && is_writeable && (err == CHDERR_FILE_NOT_WRITEABLE));
138   if (!chd)
137   while(!hd->chd && is_writeable && (err == CHDERR_FILE_NOT_WRITEABLE));
138   if (!hd->chd)
139139      goto done;
140140
141141   /* if we created the image and hence, have metadata to set, set the metadata */
142142   if (metadata)
143143   {
144      err = chd_set_metadata(chd, HARD_DISK_METADATA_TAG, 0, metadata, strlen(metadata) + 1);
144      err = chd_set_metadata(hd->chd, HARD_DISK_METADATA_TAG, 0, metadata, strlen(metadata) + 1);
145145      if (err != CHDERR_NONE)
146146         goto done;
147147   }
148148
149149   /* open the hard disk file */
150   hd->hard_disk_handle = hard_disk_open(chd);
150   hd->hard_disk_handle = hard_disk_open(hd->chd);
151151   if (!hd->hard_disk_handle)
152152      goto done;
153153
r2568r2569
156156done:
157157   if (err)
158158   {
159      if (chd)
160         chd_close(chd);
159      /* if we had an error, close out the CHD */
160      if (hd->chd != NULL)
161      {
162         chd_close(hd->chd);
163         hd->chd = NULL;
164      }
161165
162166      image_seterror(image, IMAGE_ERROR_UNSPECIFIED, chd_get_error_string(err));
163167   }
r2568r2569
212216DEVICE_IMAGE_UNLOAD( mess_hd )
213217{
214218   struct mess_hd *hd = get_drive(image);
215   assert(hd->hard_disk_handle);
216   hard_disk_close(hd->hard_disk_handle);
217   hd->hard_disk_handle = NULL;
219
220   if (hd->hard_disk_handle != NULL)
221   {
222      hard_disk_close(hd->hard_disk_handle);
223      hd->hard_disk_handle = NULL;
224   }
225
226   if (hd->chd != NULL)
227   {
228      chd_close(hd->chd);
229      hd->chd = NULL;
230   }
231
218232   drive_handles[image_index_in_device(image)] = NULL;
219233}
220234

Previous 15994 Revisions Next


© 1998-2018 The MESS Team