reepblue Posted March 2, 2019 Share Posted March 2, 2019 Hello, I've noticed when using the C++ actor system that the actor class doesn't automatically get released with the entity. I've tried releasing it before the entity, but it's still in memory after the entity has been released. Below is example code to demonstrate this behavior. Is this a bug or am I not creating the actor class correctly and it's not working with the engine's reference system? I expect that after the entity is released, it would take the actor with it making the actor pointer null. Please provide insight on how to clear actors properly. #include "Leadwerks.h" using namespace Leadwerks; class MyActor : public Actor { public: virtual void UpdateWorld() { entity->Turn(0, Time::GetSpeed(), 0); } void Test() { System::Print("Actor still in memory..."); } }; int main() { Leadwerks::Window* window = Window::Create(); Leadwerks::Context* context = Context::Create(window); World* world = World::Create(); Camera* camera = Camera::Create(); camera->SetRotation(35, 0, 0); camera->Move(0, 0, -6); Light* light = DirectionalLight::Create(); light->SetRotation(35, 35, 0); light->SetShadowMode(0); Model* model = Model::Box(); model->SetColor(1.0, 0.0, 0.0); model->SetPosition(0, 0, 0); model->SetShadowMode(0); MyActor* actor = new MyActor(); model->SetActor(actor); while (window->Closed() == false) { if (window->KeyHit(Key::T)) { if (actor != nullptr) { actor->Test(); } } if (window->KeyHit(Key::Space)) { if (model != nullptr) { model->GetActor()->Release(); model->Release(); //delete actor; <- This line crashes because Actor is off of Object. model = nullptr; } } Leadwerks::Time::Update(); world->Update(); world->Render(); context->DrawStats(0, 0, false); context->Sync(true); } return 0; } Quote Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon! Link to comment Share on other sites More sharing options...
SpiderPig Posted March 2, 2019 Share Posted March 2, 2019 I also assumed the actors were being released with entity->Release() but I've never checked. Maybe the entity only stores a reference to the memory and we need to delete the actor like this? MyActor* actor = new MyActor(); model->SetActor(actor); //End of game delete actor; Quote Link to comment Share on other sites More sharing options...
reepblue Posted March 2, 2019 Author Share Posted March 2, 2019 Deleting the actor will cause a crash because it's derived off of the engine's Object class. Quote Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon! Link to comment Share on other sites More sharing options...
Josh Posted March 2, 2019 Share Posted March 2, 2019 The correct way to do this is to set the entity actor to a new actor, or to NULL. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
reepblue Posted March 2, 2019 Author Share Posted March 2, 2019 Ok, I think I got it down now. Create the actor, set the entity to it, and then null the pointer. You probably don't need to null the actor pointer after asignment, the memory address carries over and gets assigned to entity->actor. #include "Leadwerks.h" using namespace Leadwerks; class MyActor : public Actor { public: virtual void UpdateWorld() { if (Window::GetCurrent()->KeyHit(Key::T)) { System::Print("Actor still in memory..."); } entity->Turn(0, Time::GetSpeed(), 0); } }; void SetActor(Entity* p) { MyActor* actor = new MyActor(); p->SetActor(actor); actor = nullptr; } int main() { Leadwerks::Window* window = Window::Create(); Leadwerks::Context* context = Context::Create(window); World* world = World::Create(); Camera* camera = Camera::Create(); camera->SetRotation(35, 0, 0); camera->Move(0, 0, -6); Light* light = DirectionalLight::Create(); light->SetRotation(35, 35, 0); light->SetShadowMode(0); Model* model = Model::Box(); model->SetColor(1.0, 0.0, 0.0); model->SetPosition(0, 0, 0); model->SetShadowMode(0); SetActor(model); while (window->Closed() == false) { if (window->KeyHit(Key::Space)) { if (model != nullptr) { model->Release(); model = nullptr; } } Leadwerks::Time::Update(); world->Update(); world->Render(); context->DrawStats(0, 0, false); context->Sync(true); } return 0; } 1 Quote Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon! Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.