wlav / cppyy

Other
384 stars 38 forks source link

Change of behavior of heuristic memory policy #221

Closed guitargeek closed 1 week ago

guitargeek commented 3 months ago

I noticed that the heuristic memory policy, which is not the default in cppyy, behaves different from it's incarnation in ROOT.

I thought a const pointer should never mean ownership transfer, but this doesn't seem to be respected anymore. Was there a deliberate change in the heuristic policy, or was this unintended?

This issue is a blocker for one of the three remaining test failures in the ROOT synchronization: https://github.com/root-project/root/pull/14507

Reproducer:

import cppyy

cppyy.cppdef("""
class MemTester {
public:
   static int counter;

public:
   MemTester() {
      ++counter;
   }

   MemTester( const MemTester& ) {
      ++counter;
   }

   virtual ~MemTester() {
      --counter;
   }

   void Dummy() {}

public:
   static void CallRef( MemTester& ) {}
   static void CallConstRef( const MemTester& ) {}
   static void CallPtr( MemTester* ) {}
   static void CallConstPtr( const MemTester* ) {}
};

int MemTester::counter = 0;
""")

MemTester = cppyy.gbl.MemTester
kMemoryStrict = cppyy._backend.kMemoryStrict
kMemoryHeuristics = cppyy._backend.kMemoryHeuristics

cppyy._backend.SetMemoryPolicy(kMemoryHeuristics)

# never give up ownership in case of non-const reference call
MemTester.CallConstRef( MemTester() )
print( MemTester.counter ) # should be 0

# never give up ownership in case of non-const pointer call
MemTester.CallConstPtr( MemTester() )
print( MemTester.counter ) # should be 0 too? But prints 1!