BehaviorTree.CPP icon indicating copy to clipboard operation
BehaviorTree.CPP copied to clipboard

Is returning a local instance of BehaviorTreeFactory by value from a function disallowed ?

Open vincent-hui opened this issue 9 months ago • 1 comments

Describe the bug This commit https://github.com/BehaviorTree/BehaviorTree.CPP/commit/e7a3991233843ab4400c29ccef66fec6ea83e07f makes user code compilation fail if a function that returns a local instance of BehaviorTreeFactory by value is defined.

How to Reproduce* myheader.h

#include "behaviortree_cpp/bt_factory.h"

inline BT::BehaviorTreeFactory factoryOfBtFactory() {
  BT::BehaviorTreeFactory factory;
  return factory;
}

main.cpp

#include "myheader.h"

int main(){
return 0;
}

Compilation error

/usr/local/include/c++/15.0.1/bits/unique_ptr.h: In instantiation of ‘constexpr void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = BT::BehaviorTreeFactory::PImpl]’:
/usr/local/include/c++/15.0.1/bits/unique_ptr.h:399:17:   required from ‘constexpr std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = BT::BehaviorTreeFactory::PImpl; _Dp = std::default_delete<BT::BehaviorTreeFactory::PImpl>]’
  399 |           get_deleter()(std::move(__ptr));
      |           ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
/home/vincent/workspace/BehaviorTree.CPP/include/behaviortree_cpp/bt_factory.h:218:3:   required from here
  218 |   BehaviorTreeFactory(BehaviorTreeFactory&& other) noexcept = default;
      |   ^~~~~~~~~~~~~~~~~~~
/usr/local/include/c++/15.0.1/bits/unique_ptr.h:91:23: error: invalid application of ‘sizeof’ to incomplete type ‘BT::BehaviorTreeFactory::PImpl’
   91 |         static_assert(sizeof(_Tp)>0,
      |                       ^~~~~~~~~~~

vincent-hui avatar Feb 19 '25 03:02 vincent-hui

Hi @facontidavide , would you mind letting me know why you made https://github.com/BehaviorTree/BehaviorTree.CPP/commit/e7a3991233843ab4400c29ccef66fec6ea83e07f ? Did you run into any issues ? Would you mind reverting https://github.com/BehaviorTree/BehaviorTree.CPP/commit/e7a3991233843ab4400c29ccef66fec6ea83e07f ?

vincent-hui avatar Mar 04 '25 08:03 vincent-hui

I ran into this exact issue too. My work-around is to return a shared_ptr from my factory function.

kgreenek avatar Aug 19 '25 17:08 kgreenek