Reputation: 11
In my project, I made a class,shown below,
class MyQt3D: public Qt3DExtras::Qt3DWindow
{
public:
MyQt3D()
{
// Root entity
m_rootEntity = new Qt3DCore::QEntity();
setRootEntity(m_rootEntity);
// Camera
Qt3DRender::QCamera* cameraEntity = camera();
cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
cameraEntity->setPosition(QVector3D(0, 0, 40.0f));
cameraEntity->setUpVector(QVector3D(0, 1, 0));
cameraEntity->setViewCenter(QVector3D(0, 0, 0));
// For camera controls
Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(m_rootEntity);
camController->setCamera(cameraEntity);
auto m_coneEntity = new Qt3DCore::QEntity(m_rootEntity);
// Cone shape data
Qt3DExtras::QConeMesh *cone = new Qt3DExtras::QConeMesh(m_coneEntity);
cone->setTopRadius(0.5);
cone->setBottomRadius(1);
cone->setLength(3);
cone->setRings(50);
cone->setSlices(20);
// ConeMesh Transform
Qt3DCore::QTransform *coneTransform = new Qt3DCore::QTransform(m_coneEntity);
coneTransform->setScale(1.5f);
coneTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1.0f, 0.0f, 0.0f), 45.0f));
coneTransform->setTranslation(QVector3D(0.0f, 4.0f, -1.5));
Qt3DExtras::QPhongMaterial *coneMaterial = new Qt3DExtras::QPhongMaterial(m_coneEntity);
coneMaterial->setDiffuse(QColor(QRgb(0x928327)));
// Cone
m_coneEntity->addComponent(cone);
m_coneEntity->addComponent(coneMaterial);
m_coneEntity->addComponent(coneTransform);
}
~MyQt3D()
{
delete m_rootEntity;
}
protected:
Qt3DCore::QEntity *m_rootEntity;
};
As I need to dynamically create and destory the object of the class "MyQt3D", I use the following for loop to show the memory leak,
for(int i=0;i<20;i++)
{
MyQt3D* pView = new MyQt3D();
delete pView;
}
At the beginning, the memory usage is 20 MB. After the for loop, the memory usage is 80 MB.
The source code project files can be found in, https://drive.google.com/drive/folders/1r8ZPaJVBOlYKywm7K-Se0J0ylQjbJILY?usp=sharing
How to sovle the memory leak problem? Thank you.
Upvotes: 1
Views: 588
Reputation: 11
Thank you all. With your help, the memory leak problem is solved.
I did a little experiment by using the example code from Qt3D, "BasicShape", which will display a window, as shown below, The running window of the project
I did a little modification of the project. That is, I add the following code,
for(int i=0;i<1000;i++)
{
Qt3DCore::QEntity *TempRootEntity = new Qt3DCore::QEntity();
SceneModifier *modifier = new SceneModifier(TempRootEntity);
delete modifier;
delete TempRootEntity;
qDebug()<<"delet!!!"<<i<< "\n";
}
before
SceneModifier *modifier = new SceneModifier(rootEntity);
Then, run this progam. You can find that there is now memory leak, even though we have done the for loop for 1000 times. The key point is
delete TempRootEntity;
because all the entities in SceneModifier is the children of TempRootEntity. Only delete modifier is not enough!
Upvotes: 0