[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
line):

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
line.

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
{
public:
    std::string name;
    void getString(const std::string& str)
    {
        std::cout << "String Parameter: " << str << std::endl;
    }
};
using namespace boost::python;
BOOST_PYTHON_MODULE( TestPython )
{
    class_<DataPack>("DataPack")
        .def_readwrite("name", &DataPack::name)
        .def("getString",&DataPack::getString);
}


TestApplication:

#include <boost/python.hpp>

int main(int argc, char **argv)

{
    Py_Initialize();

    try {
           boost::python::import("Test");
    }
    catch(boost::python::error_already_set const &)   {
           PyErr_Print();
    }

    Py_Finalize();

    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"
d.getString("abc")

print "Instance function call with long string"
d.getString("123456789012345678901234567890")

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