Changeset: 5ea645194dde for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=5ea645194dde
Modified Files:
        clients/Tests/exports.stable.out
        clients/mapilib/mapi.c
        common/stream/stream.c
        common/stream/stream.h
        monetdb5/modules/mal/mal_mapi.c
Branch: protocol
Log Message:

Fix memory leaks.


diffs (181 lines):

diff --git a/clients/Tests/exports.stable.out b/clients/Tests/exports.stable.out
--- a/clients/Tests/exports.stable.out
+++ b/clients/Tests/exports.stable.out
@@ -2551,6 +2551,7 @@ void bs2_resetbuf(stream *ss);
 int bs2_resizebuf(stream *ss, size_t bufsiz);
 void bs2_setpos(stream *ss, size_t pos);
 void *bs2_stealbuf(stream *ss);
+stream *bs_stealstream(stream *s);
 stream *bs_stream(stream *s);
 bstream *bstream_create(stream *rs, size_t chunk_size);
 void bstream_destroy(bstream *s);
diff --git a/clients/mapilib/mapi.c b/clients/mapilib/mapi.c
--- a/clients/mapilib/mapi.c
+++ b/clients/mapilib/mapi.c
@@ -2925,10 +2925,10 @@ mapi_reconnect(Mapi mid)
                //printf("Using protocol version %s.\n", prot_version == prot10 
 ? "PROT10" : "PROT10COMPR");
                assert(isa_block_stream(mid->to));
                assert(isa_block_stream(mid->from));
-               from = bs_stream(mid->from);
-               to = bs_stream(mid->to);
-               free(mid->from);
-               free(mid->to);
+               from = bs_stealstream(mid->from);
+               to = bs_stealstream(mid->to);
+               close_stream(mid->from);
+               close_stream(mid->to);
                mid->to = block_stream2(to, mid->blocksize, comp, mid->colcomp);
                mid->from = block_stream2(from, mid->blocksize, comp, 
mid->colcomp);
        }
diff --git a/common/stream/stream.c b/common/stream/stream.c
--- a/common/stream/stream.c
+++ b/common/stream/stream.c
@@ -3904,7 +3904,6 @@ bs_close(stream *ss)
        assert(s);
        if (s == NULL)
                return;
-       assert(s->s);
        if (s->s)
                s->s->close(s->s);
 }
@@ -3917,7 +3916,6 @@ bs_destroy(stream *ss)
        s = (bs *) ss->stream_data.p;
        assert(s);
        if (s) {
-               assert(s->s);
                if (s->s)
                        s->s->destroy(s->s);
                free(s);
@@ -3937,6 +3935,14 @@ stream* bs_stream(stream *s) {
        return ((bs*)s->stream_data.p)->s;
 }
 
+stream* bs_stealstream(stream *s) {
+       stream *res;
+       assert(isa_block_stream(s));
+       res = ((bs*)s->stream_data.p)->s;
+       ((bs*)s->stream_data.p)->s = NULL;
+       return res;
+}
+
 stream *
 block_stream(stream *s)
 {
@@ -4534,6 +4540,66 @@ isa_fixed_block_stream(stream *s) {
        return s && ((s->read == bs_read || s->write == bs_write));
 }
 
+static void
+bs2_close(stream *ss)
+{
+       bs2 *s;
+
+       s = (bs2 *) ss->stream_data.p;
+       assert(s);
+       if (s == NULL)
+               return;
+       assert(s->s);
+       if (s->s)
+               s->s->close(s->s);
+}
+
+static void
+bs2_destroy(stream *ss)
+{
+       bs2 *s;
+
+       s = (bs2 *) ss->stream_data.p;
+       assert(s);
+       if (s) {
+               assert(s->s);
+               if (s->s)
+                       s->s->destroy(s->s);
+               if (s->buf) 
+                       free(s->buf);
+               if (s->compbuf) 
+                       free(s->compbuf);
+               free(s);
+       }
+       destroy(ss);
+}
+
+static void
+bs2_update_timeout(stream *ss)
+{
+       bs2 *s;
+
+       if ((s = ss->stream_data.p) != NULL && s->s) {
+               s->s->timeout = ss->timeout;
+               s->s->timeout_func = ss->timeout_func;
+               if (s->s->update_timeout)
+                       (*s->s->update_timeout)(s->s);
+       }
+}
+
+static int
+bs2_isalive(stream *ss)
+{
+       struct bs2 *s;
+
+       if ((s = ss->stream_data.p) != NULL && s->s) {
+               if (s->s->isalive)
+                       return (*s->s->isalive)(s->s);
+               return 1;
+       }
+       return 0;
+}
+
 stream *
 block_stream2(stream *s, size_t bufsiz, compression_method comp, 
column_compression colcomp)
 {
@@ -4558,14 +4624,14 @@ block_stream2(stream *s, size_t bufsiz, 
 #endif
        ns->type = s->type;
        ns->access = s->access;
-       ns->close = bs_close;
+       ns->close = bs2_close;
        ns->clrerr = bs_clrerr;
-       ns->destroy = bs_destroy;
+       ns->destroy = bs2_destroy;
        ns->flush = bs2_flush;
        ns->read = bs2_read;
        ns->write = bs2_write;
-       ns->update_timeout = bs_update_timeout;
-       ns->isalive = bs_isalive;
+       ns->update_timeout = bs2_update_timeout;
+       ns->isalive = bs2_isalive;
        ns->stream_data.p = (void *) b;
 
        return ns;
diff --git a/common/stream/stream.h b/common/stream/stream.h
--- a/common/stream/stream.h
+++ b/common/stream/stream.h
@@ -234,6 +234,8 @@ stream_export stream *block_stream(strea
 stream_export int isa_block_stream(stream *s);
 stream_export int isa_fixed_block_stream(stream *s);
 stream_export stream* bs_stream(stream *s);
+stream_export stream* bs_stealstream(stream *s);
+
 
 typedef enum {
        PROTOCOL_AUTO = 0,
diff --git a/monetdb5/modules/mal/mal_mapi.c b/monetdb5/modules/mal/mal_mapi.c
--- a/monetdb5/modules/mal/mal_mapi.c
+++ b/monetdb5/modules/mal/mal_mapi.c
@@ -243,11 +243,12 @@ doChallenge(void *data)
                }
 
                {
+                       // convert the block_stream into a block_stream2
                        stream *from, *to;
-                       from = bs_stream(fdin);
-                       to = bs_stream(fdout);
-                       free(fdin);
-                       free(fdout);
+                       from = bs_stealstream(fdin);
+                       to = bs_stealstream(fdout);
+                       close_stream(fdin);
+                       close_stream(fdout);
                        fdin = block_stream2(from, buflen, comp, colcomp);
                        fdout = block_stream2(to, buflen, comp, colcomp);
                }
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to