0

Hi Iam working on a cocos2dx framework with cpp. I am facing memleak on a function where I use new keyword to initialize a class. Here I have a base class for all the popup in my game. I would like to make a popup from the base popup class inheriting from it. And adding the new popup as a child of a scene. The problem is when I initialize the new popup class on the stack, I couldnot call other functions which is in the new popup class. So i initialized my new popup class in heap using new keyword. Iam also deleting the instance on a function called close popup. I also saw that when clearing Iam calling the delete on the correct pointer. But my precommit says thats a memeleak. Can someone sugeest ideas? Here's my code

# Newpopup.cpp extends BasePopup

views::BasePopup* NewPopup::createPopup() {
  popup = BasePopup::createPopup(Size(400, 500));
  return popup;
}

void NewPopup::cleanPopup() {
  popup->removeFromParent();
  delete this;
}

Adding the new popup to a scene in another Scene class

# Adding to scene
void MainScene::getNewPopup() {
  NewPopup* NewPopup = new NewPopup();
  this->addChild(NewPopup);
}

Here am I doing the correct delete?

Ganesh
  • 33
  • 1
  • 3
  • `"the problem is when I initialize the new popup class on the stack, I could not call other functions which is in the new popup class"` - that does't make much sense. Can you elaborate on what error you are getting? Does NewPopup inherit from `: public BasePopup`. If not, that would be my guess. In any case, this questions needs more details - specifically a [mcve] – selbie Mar 02 '20 at 06:05
  • @ganesh does your base class have a virtual destructor ? https://stackoverflow.com/questions/8752111/possible-memory-leak-without-a-virtual-destructor – itsOkToAskStupidQus Mar 02 '20 at 09:31

0 Answers0