Skip to content

Commit

Permalink
Fix particle tracks crashing egs_view in Windows
Browse files Browse the repository at this point in the history
Fix memory leaks that lead to crashes in Windows when loading tracks in
egs_view. These bugs were added in #427.
  • Loading branch information
rtownson authored and ftessier committed Jan 13, 2020
1 parent ebffbdb commit a3aa130
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 51 deletions.
79 changes: 35 additions & 44 deletions HEN_HOUSE/egs++/view/egs_track_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,36 +82,36 @@ EGS_TrackView::EGS_TrackView(const char *filename, vector<size_t> &ntracks) {
// Slurp file
char *tmp_buffer = 0;
const char *func_name = "EGS_TrackView()";
{
ifstream data(filename, ios::binary | ios::ate);
if (data.fail() || !data.good()) {
egsWarning("%s: Unable to open track space file '%s'! No tracks loaded\n",
func_name, filename);
return;
}
streamsize size = data.tellg();
data.seekg(0, ios::beg);

data.read((char *)&tot_tracks, sizeof(int));
// very conservative sanity check to avoid a huge allocation
if (tot_tracks * sizeof(Vert) > size - sizeof(int)) {
egsInformation("%s: No tracks loaded: %d tracks can't fit in %d-byte file '%s'\n",
func_name, tot_tracks, size, filename);
m_failed = true;
return;
}
egsInformation("%s: Reading %d tracks from '%s' ...\n", func_name, tot_tracks, filename);

tmp_buffer = new char[size-sizeof(int)];
// May want to look into memory mapping, but only if access patterns/OS sets matter
if (!data.read((char *)tmp_buffer, size-sizeof(int))) {
egsWarning("%s: Unable to read %d bytes into memory! No tracks loaded\n",
func_name, size);
m_failed = true;
return;
}
egsInformation("%s: Original size : %d\n", func_name, size-sizeof(int));

ifstream data(filename, ios::binary | ios::ate);
if (data.fail() || !data.good()) {
egsWarning("%s: Unable to open track space file '%s'! No tracks loaded\n",
func_name, filename);
return;
}
streamsize size = data.tellg();
data.seekg(0, ios::beg);

data.read((char *)&tot_tracks, sizeof(int));
// very conservative sanity check to avoid a huge allocation
if (tot_tracks * sizeof(Vert) > size - sizeof(int)) {
egsInformation("%s: No tracks loaded: %d tracks can't fit in %d-byte file '%s'\n",
func_name, tot_tracks, size, filename);
m_failed = true;
return;
}
egsInformation("%s: Reading %d tracks from '%s' ...\n", func_name, tot_tracks, filename);

tmp_buffer = new char[size-sizeof(int)];

// May want to look into memory mapping, but only if access patterns/OS sets matter
if (!data.read((char *)tmp_buffer, size-sizeof(int))) {
egsWarning("%s: Unable to read %d bytes into memory! No tracks loaded\n",
func_name, size);
m_failed = true;
return;
}
egsInformation("%s: Original size : %d\n", func_name, size-sizeof(int));


char **tmp_index = new char *[tot_tracks];
Expand Down Expand Up @@ -175,11 +175,11 @@ EGS_TrackView::EGS_TrackView(const char *filename, vector<size_t> &ntracks) {

// Compression routine!
for (int i=0; i<tot_tracks; i++) {
char *loc = (char *)tmp_index[i];
int nverts = *((int *)loc);
char *ind = (char *)tmp_index[i];
int nverts = *((int *)ind);
PInfo pInfo =
*(PInfo *)(loc+sizeof(int));
char *start = loc+sizeof(int)+sizeof(PInfo);
*(PInfo *)(ind+sizeof(int));
char *start = ind+sizeof(int)+sizeof(PInfo);

int type = particleType(pInfo);
int m_off = mem_rcnt[type];
Expand Down Expand Up @@ -213,6 +213,7 @@ EGS_TrackView::EGS_TrackView(const char *filename, vector<size_t> &ntracks) {
}
}
// Sentinel at the end; get lengths
ntracks.clear();
for (int i=0; i<3; i++) {
m_index[i][ind_rcnt[i]] = mem_rcnt[i];
m_tracks[i] = size_t(ind_rcnt[i]);
Expand Down Expand Up @@ -241,17 +242,7 @@ EGS_TrackView::EGS_TrackView(const char *filename, vector<size_t> &ntracks) {
m_failed = false;
}

EGS_TrackView::~EGS_TrackView() {
// cleanup allocations to avoid memory leaks
for (int i=0; i<3; i++) {
if (m_index[i]) {
delete[] m_index[i];
}
if (m_points[i]) {
delete[] m_points[i];
}
}
}
EGS_TrackView::~EGS_TrackView() {}

bool EGS_TrackView::renderTracks(int nx, int ny, EGS_Vector *image,
EGS_ClippingPlane **planes, const int ext_planes,
Expand Down
37 changes: 30 additions & 7 deletions HEN_HOUSE/egs++/view/viewcontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,6 @@ void GeometryViewControl::loadTracksDialog() {
#ifdef VIEW_DEBUG
egsWarning("In loadTracksDialog()\n");
#endif

QFileInfo inputFileInfo = QFileInfo(filename);
filename_tracks = QFileDialog::getOpenFileName(this, "Select particle tracks file", inputFileInfo.canonicalPath(), "*.ptracks");

Expand Down Expand Up @@ -2449,12 +2448,36 @@ void GeometryViewControl::updateView(bool transform) {
rp.show_regions = show_regions;
rp.doseTransparency = doseTransparency;

rp.trackIndices[0] = spin_tminp->value()-1;
rp.trackIndices[1] = spin_tmaxp->value()-1;
rp.trackIndices[2] = spin_tmine->value()-1;
rp.trackIndices[3] = spin_tmaxe->value()-1;
rp.trackIndices[4] = spin_tminpo->value()-1;
rp.trackIndices[5] = spin_tmaxpo->value()-1;
if(spin_tminp->value() > 0) {
rp.trackIndices[0] = spin_tminp->value()-1;
} else {
rp.trackIndices[0] = 1;
}
if(spin_tmaxp->value() > 0) {
rp.trackIndices[1] = spin_tmaxp->value()-1;
} else {
rp.trackIndices[1] = 1;
}
if(spin_tmine->value() > 0) {
rp.trackIndices[2] = spin_tmine->value()-1;
} else {
rp.trackIndices[2] = 1;
}
if(spin_tmaxe->value() > 0) {
rp.trackIndices[3] = spin_tmaxe->value()-1;
} else {
rp.trackIndices[3] = 1;
}
if(spin_tminpo->value() > 0) {
rp.trackIndices[4] = spin_tminpo->value()-1;
} else {
rp.trackIndices[4] = 1;
}
if(spin_tmaxpo->value() > 0) {
rp.trackIndices[5] = spin_tmaxpo->value()-1;
} else {
rp.trackIndices[5] = 1;
}

gview->render(g, transform);
}
Expand Down

0 comments on commit a3aa130

Please sign in to comment.