Subject: | Memory leaks when failed to open db |
Hello,
I have attached a patch which prevents memory leakage when tie(..., $DB_HASH) fails.
Test code:
#!/usr/bin/perl
use strict;
use warnings;
use DB_File;
use Fcntl qw(:DEFAULT :mode);
sub DEFAULT_MODE {
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
}
my $count = 500_000; # set as you wish
my %hash;
my $file = '/etc/sasl2/sasldb_not_exists'; # set invalid destination
while ($count > 0) {
tie (%hash, 'DB_File', $file,
O_RDWR | O_CREAT, DEFAULT_MODE, $DB_HASH);
$count--;
$count % 1000 or sleep(1); # slowmo :)
}
Thanks.
Subject: | DB_File_01.diff |
--- DB_File.xs.orig 2013-10-17 16:44:45.508895581 +0400
+++ DB_File.xs 2013-10-17 16:46:03.538588908 +0400
@@ -1494,10 +1494,10 @@
status = (RETVAL->dbp->cursor)(RETVAL->dbp, NULL, &RETVAL->cursor,
0) ;
/* printf("cursor returned %d %s\n", status, db_strerror(status)) ; */
- }
-
- if (status)
- RETVAL->dbp = NULL ;
+ } else {
+ db_close(RETVAL); // close **dbp handle to prevent mem.leak
+ RETVAL->dbp = NULL ;
+ }
}