C++ dynamic allocation in cocos2d-x -
i made pointer variable, ptr_view, in main cpp dynamic allocation create views & buttons in screen.
and sch, common pointer in class_a, class_b ... indicating main class access pointer ptr_view. when button class_a made pressed, function below running.
void class_a::changeview_b() { cc_safe_delete(sch->ptr_view); sch->ptr_view = new class_b; sch->ptr_view->rcreation(main_view); }
but creates error, got know why, since memory of class_a terminated when cc_safe_delete running 'sch' no more exist when trying access sch->ptr_view. still don't know how fix problem. can give me little clue through situation?
your logic easier reason if used smart pointer type managing memory. cocos2d-x provides own smart pointer type, cocos2d::refptr<t>
, reference-counted pointer (think std::shared_ptr<t>
or objective-c arc).
that said, given information in question, there no reason code posted shouldn't work. cc_safe_delete(sch->ptr_view)
(which delete ptr; ptr = nullptr;
) shouldn't delete sch
. there may other memory bugs in program. again: smart pointers friend.
Comments
Post a Comment