SmartPetscObj<PC> pC;
CHKERR KSPGetPC(ksp, pC)
Karol,
When you look here
https://petsc.org/release/docs/manualpages/KSP/KSPGetPC.html
according to PETSc documentation KSPGetPC returns a pointer, not an object, which you have to destroy when is no longer needed. So, you do not need to manage it with a smart pointer, and if you do, that will create a segmentation fault, once you go out of the scope, and smart petsc pointer will try to destroy the object. This is a typical case.
If you like to see how to convert a pointer to a smart petsc pointer, for pets object which you have to destroy, look here
http://mofem.eng.gla.ac.uk/mofem/html/petsc_smart_ptr_objects_8cpp-example.html
For example,
Mat m; CHKERR MatCreate(PETSC_COMM_SELF, &m); auto m_ptr = SmartPetscObj<Mat>(m, false);
however, in such a case, os better to use
auto m_ptr = createSmartVectorMPI(PETSC_COMM_WORLD, nb_loc, nb_glob);
`
see here doc for createSmartVectorMPI
http://mofem.eng.gla.ac.uk/mofem/html/namespace_mo_f_e_m.html#a80dca487877a37c15c740c9c8258ce40
You could anyway write a code such that it prevents the destruction of PC when KSP is destroyed, and bum use reference of it. In such case you would do:
PC pc_raw; CHKERR KSPGetPC(ksp, pc_raw)
auto pc_smart = SmartPetscObj<PC>(pc_raw, true);
`
Note the second argument in reference, i.e. "true", it bumps reference of the object. This will guarantee that the object exists until pc_smart gets out of the scope.
Regards,
Lukasz