469,282 Members | 2,034 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Memory Management in Embedded Python

Hi there,

I have a piece of code like this:

void funct(PyObject* pyobj)
{
char str[128];
strncpy(str, "just a test string", sizeof(str));
PyObject* pydata = PyObject_CallMethod(pyobj, "method_x",
"s", str);
Py_DECREF(pydata);
}

After the function is exited, the str is not there anymore. Will this
affect python operation. How does python use the str? It's copied or it
just uses the pointer?

Thanks in advance.

Jan 18 '07 #1
1 978
Huayang Xia schrieb:
I have a piece of code like this:

void funct(PyObject* pyobj)
{
char str[128];
strncpy(str, "just a test string", sizeof(str));
PyObject* pydata = PyObject_CallMethod(pyobj, "method_x",
"s", str);
Py_DECREF(pydata);
}

After the function is exited, the str is not there anymore. Will this
affect python operation. How does python use the str? It's copied or it
just uses the pointer?
The interpreter creates a string object, and passes that to method_x.
Creating a string object does indeed create a copy. The string object
will be refcounted, so it exists as long as there is a reference to it.
Likely (unless the method_x implementation somehow stores the string),
it gets deallocated before PyObject_Call returns.

HTH,
Martin
Jan 18 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Marcelo A. Camelo | last post: by
4 posts views Thread by lebo | last post: by
18 posts views Thread by Tron Thomas | last post: by
7 posts views Thread by Dan Nilsen | last post: by
12 posts views Thread by ira2402 | last post: by
4 posts views Thread by quigstah | last post: by
4 posts views Thread by cesar.ortiz | last post: by
reply views Thread by kiran kumar | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.