Add ref-count check , avoiding crash.