Question

I am running the following piece of code under the Marmalade SDK. I need to know if there's a "bug" in my code or in Marmalade:

template <class Return = void, class Param = void*>
class IFunction {

private:

    static unsigned int counterId;

protected:

    unsigned int id;

public:

    //

    static unsigned int getNewId() { return counterId++; }

    template <class FunctionPointer>
    static unsigned int discoverId(FunctionPointer funcPtr) {

        typedef std::pair<FunctionPointer, unsigned int> FP_ID;
        typedef std::vector<FP_ID> FPIDArray;
        static FPIDArray siblingFunctions; // <- NOTE THIS

        typename FPIDArray::iterator it = siblingFunctions.begin();
        while (it != siblingFunctions.end()) {
            if (funcPtr == it->first) return it->second; /// found
            ++it;
        }

        /// not found
        unsigned int newId = getNewId();
        siblingFunctions.push_back( FP_ID(funcPtr, newId) ); // <- NOTE THIS

        return newId;
    }

    //

    virtual ~IFunction() {}

    bool operator<(const IFunction* _other) const {
        if (this->id < _other->id) return true;
        return false;
    }

    virtual Return call(Param) = 0;

};

Note that every time template class discoverId is called for the 1st time, a static local array is created.

At program exit, the Marmalade memory manager complains that the memory reserved at this line :

siblingFunctions.push_back( FP_ID(funcPtr, newId) );

hasn't been freed. (The truth is that I don't empty the array, but how could I, I don't have access to it outside that function!).

Here is the catch : Marmalade complains only for the memory reserved at the very first call of this function! This function is called several times and with several different template parameters, but the complaining always occurs only for the memory reserved at the 1st call. This is the case even if I mix up the order of the various calls to this function. Memory reserved for every call after the 1st one is automatically freed - I have checked this out.

So, who's to blame now?

Was it helpful?

Solution

I don't know what "Marmalade" is (and a quick search for this word expectedly found a lot of irrelevant references) but your code doesn't have a resource leak with respect to the static FPIDArray siblingFunctions: this object is constructed the first time the function is called. It is destroyed at some point after main() is exited. I seem to recall that the order of destruction of objects with static linkage is the reverse of order in which objects are constructed but I'm not sure if this extends function local statics.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top