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: | 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)
Change History (7)
by , 14 years ago
Attachment: | VarSw2.zip added |
---|
comment:1 by , 14 years ago
follow-ups: 4 5 comment:2 by , 14 years ago
Status: | new → assigned |
---|
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 , 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.
comment:4 by , 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 parensIf the static_cast fails but my replacement works, I think you need to file a bug report with your compiler vendor.
comment:5 by , 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 parensIf the static_cast fails but my replacement works, I think you need to file a bug report with your compiler vendor.
comment:6 by , 13 years ago
Cc: | added |
---|---|
Resolution: | → worksforme |
Status: | assigned → closed |
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
Less complicated inputs do not create immediate problem: p.set((1,2)) p.set('d')
are fine.