[C++-sig] [c++-sig] python std::string assignment

Furkan Kuru furkankuru at gmail.com
Sun Oct 26 10:27:57 CET 2008

Sorry for long delay,

The problem was:
I export a class with a std::string field and assinging its string field
longer than 15chars crashed my application on one client computer using
vista as os.

I wrote small test case and used previous project configurations to build
and it worked  (The codes are below the message)

Then I changed my code as small as the test code but still it was not
working (what I mean by not working is that application crashed at same code

The only difference with my pyd and test pyd was that test pyd was near the
main executable but my pyd was under a directory (module)

I put test pyd file in a directory and an __init__.py file and trying to
import it and It also crashed just on the long (>15chars) string assignment

Putting all the pyd s and dlls from the module directory to the main
applications directory, and arranging python import statements accordingly
solved my problem in a dirty way.

Now the main folder contains too many dlls which is annoying. I still do not
know if it is boost-related or just python related.
Is space allocation done automatically  during std::string assignment?

When I put pyd file and an __init__.py to a folder and import it from its
parent folder it crashes the application. Does the way of memory allocation
change if it is in a different folder or next to the main file?

Thanks for your help,

Here is the test part:

a main application embed python
a boost python dll module loaded by python script
a script called by main application

for  TestPython.pyd file:

#include <iostream>
#include <string>
#include <boost/python.hpp>
class DataPack
    std::string name;
    void getString(const std::string& str)
        std::cout << "String Parameter: " << str << std::endl;
using namespace boost::python;
        .def_readwrite("name", &DataPack::name)


#include <boost/python.hpp>

int main(int argc, char **argv)


    try {
    catch(boost::python::error_already_set const &)   {


    return 0;

Test.py file:

from TestPython import DataPack

print "Instance"
d = DataPack()

print "Instance string field assignment SHORT"
d.name = "abc"

print "Instance string field assignment LONG"
d.name = "123456789012345678901234567890"

print "Instance function call with short string"

print "Instance function call with long string"

On Fri, Oct 17, 2008 at 7:17 PM, David Abrahams <dave at boostpro.com> wrote:

> on Fri Oct 17 2008, "Furkan Kuru" <furkankuru-AT-gmail.com> wrote:
> >     Yes, that seems very likely; this looks a bit like the small string
> >     optimization gone awry.  Perhaps you have mixed the MS runtime lib
> >     headers with a different version of the binary library.
> >
> > Is there any way to remove this string optimization. Or How can I trace
> and find
> > these mixed headers to fix the problem?
> I don't know.  There are many people in the world much better-qualified
> to help you with this problem than I am.  I suggest take your question
> to a MSVC-specific group.  As Stefan said, it's unlikely that it has
> anything to do with boost or python.
> --
> Dave Abrahams
> BoostPro Computing
> http://www.boostpro.com
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig

Furkan Kuru
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20081026/afd3c559/attachment.htm>

More information about the Cplusplus-sig mailing list