Opened 14 years ago

Closed 13 years ago

#1952 closed Bugs (worksforme)

[saywhat] The operation static_cast<std::string>(boost::python::extract<std::string>(...)) crashes Python. Seems to create reference counting problem.

Reported by: Konstantin Aslanidi <konstantin.aslanidi@…> Owned by: Dave Abrahams
Milestone: Boost 1.36.0 Component: python USE GITHUB
Version: Boost 1.35.0 Severity: Showstopper
Keywords: python extract Cc: troy@…

Description

I submit boost::python based python extension code that crashes Python interpreter when used as described below. I attach Visual Studio .NET project files.

C:\KDocuments\Variance Swap Validation 2\VarSw2\Debug>python ActivePython 2.5.2.2 (ActiveState Software Inc.) based on Python 2.5.2 (r252:60911, Mar 27 2008, 17:57:18) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.

import vrsw2 p=vrsw2.VarSwapTest() p.set(((1,2),(3,4)))

<crash here>

The C++ extension code: #include <string> #include <fstream> #include <boost/python.hpp> #include <boost/shared_ptr.hpp> using namespace boost::python;

class VarSwapTest { private:

class Impl { private:

std::ofstream theFile;

public:

Impl()

: theFile("VarSwapTestEcho.txt") {}

void set( object& input ) {

object o=input.attr("str")(); extract<std::string> ex(o); if( ex.check() )

theFile<<(static_cast<std::string>(ex()))<<std::endl;

else

theFile<<"failed to extract string"<<std::endl;

}

};

private:

boost::shared_ptr<Impl> theImpl;

public:

VarSwapTest() : theImpl(new Impl()) {} void set( object input ) {

theImpl->set(input);

}

};

BOOST_PYTHON_MODULE(vrsw2) {

class_<VarSwapTest>("VarSwapTest")

.def("set",&VarSwapTest::set)

;

}

Attachments (1)

VarSw2.zip (971 bytes ) - added by Konstantin Aslanidi <konstantin.aslanidi@…> 14 years ago.

Download all attachments as: .zip

Change History (7)

by Konstantin Aslanidi <konstantin.aslanidi@…>, 14 years ago

Attachment: VarSw2.zip added

comment:1 by Konstantin Aslanidi <konstantin.aslanidi@…>, 14 years ago

Less complicated inputs do not create immediate problem: p.set((1,2)) p.set('d')

are fine.

comment:2 by Dave Abrahams, 14 years ago

Status: newassigned

suppose you replace

static_cast<std::string>(boost::python::extract<std::string>(...))

with

boost::python::extract<std::string>(...)() // note extra parens

If the static_cast fails but my replacement works, I think you need to file a bug report with your compiler vendor.

comment:3 by Konstantin Aslanidi <konstantin.aslanidi@…>, 14 years ago

Dave,

I tried all combinations, I do use extra perens in the submitted code. The code compiles, links, runs. It succeds for list,dicts,strings etc. It fails for embedded structures such as tuple of tuples.

For example: import vrsw2 p=vrsw2.VarSwapTest() p.set((1,2)) p.set('d')

are successful.

However, p.set(((1,2),(3,4))) crashes. I did some debugging, it appears that the crash occurs during heap deallocation. I was unable to find any version of the submitted code that would pass the tuple of tuples.

K.

in reply to:  2 comment:4 by Konstantin Aslanidi <konstantin.aslanidi@…>, 14 years ago

Please, see the newly submitted correction.

Replying to dave:

suppose you replace

static_cast<std::string>(boost::python::extract<std::string>(...))

with

boost::python::extract<std::string>(...)() // note extra parens

If the static_cast fails but my replacement works, I think you need to file a bug report with your compiler vendor.

in reply to:  2 comment:5 by Konstantin Aslanidi <konstantin.aslanidi@…>, 14 years ago

... in addition to previous comments, I verified the below suggestion. It does not work.

Replying to dave:

suppose you replace

static_cast<std::string>(boost::python::extract<std::string>(...))

with

boost::python::extract<std::string>(...)() // note extra parens

If the static_cast fails but my replacement works, I think you need to file a bug report with your compiler vendor.

comment:6 by troy d. straszheim, 13 years ago

Cc: troy@… added
Resolution: worksforme
Status: assignedclosed
Summary: The operation static_cast<std::string>(boost::python::extract<std::string>(...)) crashes Python. Seems to create reference counting problem.[saywhat] The operation static_cast<std::string>(boost::python::extract<std::string>(...)) crashes Python. Seems to create reference counting problem.

unable to reproduce with recent gcc. Test case simplified and added to my branch in git, will test on vc9, if it doesn't pass there i'll reopen.

the test:

http://gitorious.org/~straszheim/boost/straszheim/commit/43829f0e3290b74aa88907a25698e30c573b1012

Note: See TracTickets for help on using tickets.