On Wed, 20 Mar 2013 19:27:34 -0000, Jim Bosch <tallji...@gmail.com> wrote:

(btw, this reply seemed to only go to me rather than the list)

oh, sorry... Need to get into a habit of double-checking the reply address..


On 03/20/2013 02:16 PM, Alex Leach wrote:
void PyEnv::ResetPyList(const boost::python::list& envp)
{
     int envc = boost::python::len(envp);
     const char** pyenvp = new const char*[envc+1];
     boost::python::stl_input_iterator<std::string> begin(envp), end;
     int i=0;
     while (begin != end)
     {
         pyenvp[i++] = (*begin).c_str();
         begin++;
     }
     pyenvp[i] = '\0';
     const char* const* _envp = &pyenvp[0];
     Environment::Reset(_envp);
     delete[] pyenvp;
}

This actually looks like it could be your problem, and I think the dict version has the same issue.

When the stl_input_iterator makes a std::string from the Python object you've passed in, it's a temporary, and when you extract a c_str() from it, the pointer that's returned is invalidated as soon as the std::string is destroyed (which is probably as soon as the iterator is incremented).

This would probably explain an error I was getting yesterday, actually.. Thanks! Environment::Reset uses 'strchr' to search for the "=" character, and prints a diagnostic message if it's not found. I kept seeing that message, and it showed that the char* it found was garbage. However, the above method completes its call to Environment::Reset without issue now (later got segfaults because of no NULL pointer at the end of the array).


So you're passing a whole lot of invalid char* pointers into Environment::Reset. Note that they're invalid before you pass them in, so it doesn't matter if Environment::Reset does a deep copy of what's passed in.

This wouldn't explain why you're seeing the problem in the constructor instead of the destructor, but sometimes memory issues are like that.


err, isn't it the other way around? The constructor's complete successfully (verified with plenty of Python print and C++ printf statements), only the destructor seg-faults... Thanks for the help though; appreciated!

Kind regards,
Alex
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to