home Forums # Technical Support Memory management

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #6133
    Anonymous
    Inactive

    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 *ramp;
    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;
    // yuck!
    // 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?

    #6141

    Hi temprt,

    thank you for your post and for using fuzzylite.

    Interesting question.

    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:

    https://github.com/fuzzylite/fuzzylite/blob/release/fuzzylite/src/Engine.cpp#L96
    https://github.com/fuzzylite/fuzzylite/blob/release/fuzzylite/src/variable/Variable.cpp#L63
    https://github.com/fuzzylite/fuzzylite/blob/release/fuzzylite/src/rule/RuleBlock.cpp#L67

    Cheers.

    #6142
    Anonymous
    Inactive

    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

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.