It contains the address of the local variable which could not be available anymore, so the pointer isn't valid anymore?
Yes. It becomes a dangling pointer.
You could try vector::swap, as in:
class AA {
B mb; // not a pointer
f2(B* b){
mb.swap(*b); // swap the content with b, which is just a few pointer assignments.