tevent: add/use tevent_req_destructor
authorStefan Metzmacher <metze@samba.org>
Fri, 27 Sep 2013 01:41:29 +0000 (03:41 +0200)
committerStefan Metzmacher <metze@samba.org>
Fri, 17 Jan 2014 11:38:08 +0000 (12:38 +0100)
This makes sure we call tevent_req_received(req) on talloc_free()
and cleanup things in a defined order.

Note that some callers used their own destructor for their
tevent_req instance, they'll just overwrite this,
which is not intended, but works without problems.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
lib/tevent/tevent_req.c

index edb8550072934e1fa6ae34d5bbf5e530ef4b87ae..30e91e252691708a7765273f319d580bf97c4291 100644 (file)
@@ -51,6 +51,8 @@ char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req)
        return req->private_print(req, mem_ctx);
 }
 
+static int tevent_req_destructor(struct tevent_req *req);
+
 struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
                                    void *pdata,
                                    size_t data_size,
@@ -86,10 +88,18 @@ struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
 
        req->data = data;
 
+       talloc_set_destructor(req, tevent_req_destructor);
+
        *ppdata = data;
        return req;
 }
 
+static int tevent_req_destructor(struct tevent_req *req)
+{
+       tevent_req_received(req);
+       return 0;
+}
+
 void _tevent_req_notify_callback(struct tevent_req *req, const char *location)
 {
        req->internal.finish_location = location;
@@ -200,7 +210,8 @@ bool tevent_req_is_in_progress(struct tevent_req *req)
 
 void tevent_req_received(struct tevent_req *req)
 {
-       TALLOC_FREE(req->data);
+       talloc_set_destructor(req, NULL);
+
        req->private_print = NULL;
        req->private_cancel = NULL;
 
@@ -208,6 +219,8 @@ void tevent_req_received(struct tevent_req *req)
        TALLOC_FREE(req->internal.timer);
 
        req->internal.state = TEVENT_REQ_RECEIVED;
+
+       TALLOC_FREE(req->data);
 }
 
 bool tevent_req_poll(struct tevent_req *req,