Squirrel

The programming language
Welcome to Squirrel Sign in | Join | Help
in Search

Memory Leaks when using overloadConstructor

Last post 06-12-2008, 9:09 PM by JamesTan. 3 replies.
Sort Posts: Previous Next
  •  05-16-2008, 9:16 AM 2516

    Memory Leaks when using overloadConstructor

    Using the latest snapshot of SqPlus, I seem to be getting memory leaks when I use the overloadConstructor function when defining my class's.

    I'm still researching into it, but at this stage, if I do not use the overloadConstructor then I do not see memory leaks. Using SquirrelVM to both initialize and shut down Squirrel, is there anything extra I have to do to ensure that overloadConstructor deallocates memory?

    Inserting the Tuple3 code into my own source code also generates memory leaks, where as inserting my code into the test examples, no memory leaks occur.

    struct NativeVector2
    {
        NativeVector2() : x(0.f), y(0.f) { };
        NativeVector2(SQFloat in_x, SQFloat in_y) : x(in_x), y(in_y) { };
        NativeVector2(SQInteger in_x, SQInteger in_y) : x(in_x), y(in_y) { };
        virtual ~NativeVector2() { wxLogMessage(wxT("NativeVector2 released")); };

        float x, y;
    };
    DECLARE_INSTANCE_TYPE_NAME(NativeVector2, Vector2);

    Engine_Script::Engine_Script()
    {
        SquirrelVM::Init();

        // Vector2 class
        SqPlus::SQClassDef<NativeVector2>(wxT("Vector2"))
            .overloadConstructor<NativeVector2(*)(void)>()
            .overloadConstructor<NativeVector2(*)(float, float)>()       
            .overloadConstructor<NativeVector2(*)(int, int)>()   
            .overloadConstructor<NativeVector2(*)(NativeVector2&)>();
    }

    Running any scripts, which do ' local v = Vector2() ', will show that ~NativeVector2 is properly being deleted, yet when exiting the application I am leaking about 12 bytes of data.
  •  05-17-2008, 7:33 AM 2518 in reply to 2516

    Re: Memory Leaks when using overloadConstructor

    Hi,

    In the current snapshot of sqplus, I'm using static variables
    for holding the overloaded functions and constructors.
    I suppose this should cause the memory leak you found.
    In addition, the static variables are not safe for switching
    multiple VMs, so I'm now rewriting it by storing them in squirrel
    register and attributes.  This will be included in the next
    snapshot.

    Regards,
    --
    K. Kawachi

  •  06-12-2008, 8:28 PM 2564 in reply to 2518

    Re: Memory Leaks when using overloadConstructor

    Hi,

    I got the same problem of Memory Leaks when using overloadConstructor
    and I found the problem could resulted from ClassType<T>::destruct()
    here is the current code in SqPlusOverload.h

        static inline int destruct(SQUserPointer up, SQInteger size) {
            if (up) {
                static_cast<T*>(up)->~T();
            }
            return 0;
        }

    the object in 'up' just be destructed, and the memory has not been freed
    so I modified the code into

        static inline int destruct(SQUserPointer up, SQInteger size) {
            if (up) {
                static_cast<T*>(up)->~T();
                sq_free(up, size); // paired with sq_malloc in Arg<T>::create()
            }
            return 0;
        }

    and the problem of Memory Leaks was disappeared in my application.

    Regards,

  •  06-12-2008, 9:09 PM 2566 in reply to 2564

    Re: Memory Leaks when using overloadConstructor

    Thanks for the information!
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems