diff options
author | Roger Hu <rhu@hearsaycorp.com> | 2014-06-18 07:25:29 +0000 |
---|---|---|
committer | Roger Hu <rhu@hearsaycorp.com> | 2014-06-18 08:00:44 +0000 |
commit | 73d66a9f5ea9b9f72e497015be3c765f7cebd10f (patch) | |
tree | 83d035816cded1dc4d00b64560e935d41fb02edc | |
parent | 7d58aba8bb404f1a886c415bd417f5fb9d987a3f (diff) | |
download | librabbitmq-73d66a9f5ea9b9f72e497015be3c765f7cebd10f.tar.gz |
We store references to hostname, userid, virtual_host, and password inside a
RabbitMQ state object. However, once those arguments leave scope inside the
init() function, Python deallocates the memory.
If we want to keep references to them, we need to do custom malloc operations
to store them and deallocate them when we're no longer using the object.
I noticed this problem when putting a breakpoint on the
kombu/transport/librabbitmq.py establish_connection() and attempted to print
the Connection object. On multiple publishing, I noticed the Connection string
object corrupted, particularly the hostname.
-rw-r--r-- | Modules/_librabbitmq/connection.c | 40 |
1 files changed, 32 insertions, 8 deletions
diff --git a/Modules/_librabbitmq/connection.c b/Modules/_librabbitmq/connection.c index dd7a3f8..394cd2d 100644 --- a/Modules/_librabbitmq/connection.c +++ b/Modules/_librabbitmq/connection.c @@ -908,6 +908,19 @@ PyRabbitMQ_ConnectionType_dealloc(PyRabbitMQ_Connection *self) { if (self->weakreflist != NULL) PyObject_ClearWeakRefs((PyObject*)self); + + if (self->hostname != NULL) + PyMem_Free(self->hostname); + + if (self->userid != NULL) + PyMem_Free(self->userid); + + if (self->password != NULL) + PyMem_Free(self->password); + + if (self->virtual_host != NULL) + PyMem_Free(self->virtual_host); + Py_XDECREF(self->callbacks); Py_XDECREF(self->server_properties); self->ob_type->tp_free(self); @@ -932,10 +945,11 @@ PyRabbitMQ_ConnectionType_init(PyRabbitMQ_Connection *self, "heartbeat", NULL }; - char *hostname = "localhost"; - char *userid = "guest"; - char *password = "guest"; - char *virtual_host = "/"; + char *hostname; + char *userid; + char *password; + char *virtual_host; + int channel_max = 0xffff; int frame_max = 131072; int heartbeat = 0; @@ -947,10 +961,20 @@ PyRabbitMQ_ConnectionType_init(PyRabbitMQ_Connection *self, return -1; } - self->hostname = hostname; - self->userid = userid; - self->password = password; - self->virtual_host = virtual_host; + self->hostname = PyMem_Malloc(strlen(hostname) + 1); + self->userid = PyMem_Malloc(strlen(userid) + 1); + self->password = PyMem_Malloc(strlen(password) + 1); + self->virtual_host = PyMem_Malloc(strlen(virtual_host) + 1); + + if (self->hostname == NULL || self->userid == NULL || self->password == NULL || self->virtual_host == NULL) { + return PyErr_NoMemory(); + } + + strcpy(self->hostname, hostname); + strcpy(self->userid, userid); + strcpy(self->password, password); + strcpy(self->virtual_host, virtual_host); + self->port = port; self->channel_max = channel_max; self->frame_max = frame_max; |