Skip to content

Commit fe52ad2

Browse files
committed
address some more memory management problems, see #37 (comment)
1 parent 109e9d5 commit fe52ad2

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

c/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ to the screen:
7373
printf("Error reading XDI file '%s':\n %s\t(error code = %ld)\n",
7474
argv[1], xdifile->error_message, ret);
7575
XDI_cleanup(xdifile, ret);
76+
free(xdifile);
7677
return 1;
7778
}
7879

@@ -168,6 +169,7 @@ To deallocate the memory from the XDIFile struct, do this:
168169

169170
```C
170171
XDI_cleanup(xdifile, ret);
172+
free(xdifile);
171173
```
172174
173175
Here, the second argument is the return code from the call to

c/xdi_reader.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ int main(int argc, char **argv) {
3939
printf("Error reading XDI file '%s':\n %s\t(error code = %ld)\n",
4040
argv[1], xdifile->error_message, ret);
4141
XDI_cleanup(xdifile, ret);
42+
free(xdifile);
4243
return 1;
4344
}
4445

@@ -106,5 +107,6 @@ int main(int argc, char **argv) {
106107
/* free memory before leaving */
107108
free(tdat);
108109
XDI_cleanup(xdifile, 0);
110+
free(xdifile);
109111
return 0;
110112
}

c/xdifile.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ XDI_cleanup(XDIFile *xdifile, long err) {
972972
(err != ERR_META_KEYNAME) &&
973973
(err != ERR_META_FORMAT)) {
974974
free(xdifile->filename);
975-
};
975+
}
976976

977977
if ((err < -1) || (err == 0)) {
978978

@@ -985,7 +985,7 @@ XDI_cleanup(XDIFile *xdifile, long err) {
985985
free(xdifile->array);
986986
free(xdifile->array_labels);
987987
free(xdifile->array_units);
988-
};
988+
}
989989

990990
for (j = 0; j < xdifile->nmetadata; j++) {
991991
free(xdifile->meta_families[j]);
@@ -1003,8 +1003,6 @@ XDI_cleanup(XDIFile *xdifile, long err) {
10031003
if (err == 0) {
10041004
free(xdifile->outer_array);
10051005
free(xdifile->outer_breakpts);
1006-
};
1006+
}
10071007
}
1008-
1009-
free(xdifile);
10101008
}

perl/lib/Xray/XDI.pm

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ has 'file' => (is => 'rw', isa => 'Str', traits => [qw(Clone)], default => q{},
1919
has 'xdifile' => (
2020
is => 'ro',
2121
traits => [qw(NoClone)],
22-
isa => 'Xray::XDIFile',
22+
#isa => 'Xray::XDIFile'|'Undef',
2323
init_arg => undef,
2424
lazy => 1,
2525
builder => '_build_object',
@@ -72,22 +72,32 @@ has 'data' => (
7272
},
7373
);
7474

75+
sub DEMOLISH {
76+
my ($self) = @_;
77+
return if $self->errorcode; # is this right? or does the errorcode need to be passed to _cleanup?
78+
return if not defined($self->xdifile);
79+
$self->xdifile->_cleanup(0);
80+
};
81+
7582
sub _build_object {
7683
my ($self) = @_;
7784
$self->error(q{});
7885
$self->ok(1);
7986
$self->warning(0);
8087
if (not -e $self->file) {
88+
$self->errorcode(1);
8189
$self->error('The file '.$self->file.' does not exist as XDI');
8290
$self->ok(0);
8391
return undef;
8492
};
8593
if (not -r $self->file) {
94+
$self->errorcode(1);
8695
$self->error('The file '.$self->file.' cannot be read as XDI');
8796
$self->ok(0);
8897
return undef;
8998
};
9099
if (-d $self->file) {
100+
$self->errorcode(1);
91101
$self->error($self->file.' is a folder (i.e. not an XDI file)');
92102
$self->ok(0);
93103
return undef;

0 commit comments

Comments
 (0)