# Petsc's xxxSetxxx methods: Own Pointer or Copy Values?

In PETSC, there are many xxxSetyyy methods, e.g. MatSetLocalToGlobalMapping(A,rmap,cmap). I wonder whether contents of rmap and cmap (or generally yyy, that set to xxx) pointing to are copied to somewhere in A (or xxx). In other words, shall I destroy 'yyy' after set to 'xxx' or I should not before finish using 'yyy' inside 'xxx'?

Other examples include KSPSetPC, PCCompositeAddPC(PC pc,PCType type), ISCreateGeneral, PCASMSetLocalSubdomains. I found that for PCASMSetLocalSubdomains the IS defining the subdomains are copied so we can destroy it afterwards. On the other hand, ISCreateGeneral forward a parameter to let user specify PetscCopyMode (Own_Pointer or Copy_Values).

Is there any general rule for these things? Thanks!

In general, the Set methods (and PETSc) pass-by-reference, so it is not safe to destroy the object that you have passed to the method. Methods such as PCASMSetLocalSubdomains are the exceptions, not the rules.