- This topic has 2 replies, 2 voices, and was last updated 5 years, 5 months ago by Anonymous.
April 4, 2018 at 14:58 #6133AnonymousInactive
Hi. While I’m doing some cleaning up and polishing for my code that uses fuzzylite, I now noticed something about the code, which is based on fuzzylite examples (and how pretty much everyone else is using that you can see from Google).
I do like lines like these. I think they are neat and clean to look at, even if they can get quite lengthy.
mSteer->addTerm(new Ramp(“left”, 1.000, 0.000));
mSteer->addTerm(new Ramp(“right”, 0.000, 1.000));
However, aren’t these unfriendly for memory leaks? And with using tons of these in my code, trying to rewrite every line to properly delete them seems like it will make things very, very messy.
ramp = new Ramp(“left”, 1.000, 0.000); mSteer->addTerm(ramp); delete ramp;
ramp = new Ramp(“right”, 0.000, 1.000); mSteer->addTerm(ramp); delete ramp;
// and of course this summarized example doesn’t even work since I still need them for addTerm, addInputVaraible
// probably need messier stuff in headers, constructors, destructors
What’s the cleanest, shortest, most efficient way to do this?April 5, 2018 at 04:16 #6141Juan Rada-Vilela (admin)Keymaster
thank you for your post and for using fuzzylite.
For C++ programmers, doing
mSteer->addTerm(new Ramp(“right”, 0.000, 1.000));may feel (very) unsafe and scary, and I understand that. However, when a term is added to the variable, the variable takes ownership of the term, and therefore deletes every term automatically upon deleting the variable. Likewise, the engine deletes the variables. All you need to do is make sure you delete the engine and the rest will be handled automatically for you. The nice structure and encapsulation of fuzzylite makes it easy to assign ownerships of objects.
You can take a look at:
Cheers.April 5, 2018 at 12:16 #6142AnonymousInactive
Wow, silly me. I did try to read the source code to investigate this myself. But I somehow managed to read only the destructors that didn’t have that kind of clean up code. This is cool, thanks.
Now, time to revert my super messy code that attempted manual deletion. No wonder I was getting segmentation faults upon deleting them. :p
- You must be logged in to reply to this topic.