Casper7526
Casper7526

Reputation: 341

C++ Memory Leak Issues

Ok, so here's the easy way to explain it, the following code is from an SDK for a game of Unreal Engine 3, so most of the code shown was generated by a seperate program and not myself, however I've run into an issue with it and I can't seem to get it solved.

Basically, this is the line I'm calling

if (Entity->GetHumanReadableName().Data)

The function is causing a very small leak (adds up over a few hours until program crashes) Now I'm using the GetHumanReadableName function just for this purpose, but any function that returns an "FSTRING" will cause a leak, which to me atleast means my FSTRING is leaking, but I can't figure out where.

So I'm hoping to break down each layer and hopefully you guys can figure this out with me?

struct FString AActor::GetHumanReadableName ( )
{
    static UFunction* pFnGetHumanReadableName = NULL;

    if ( ! pFnGetHumanReadableName )
        pFnGetHumanReadableName = (UFunction*) UObject::GObjObjects()->Data[ 3859 ];

    AActor_execGetHumanReadableName_Parms GetHumanReadableName_Parms;

    this->ProcessEvent ( pFnGetHumanReadableName, &GetHumanReadableName_Parms, NULL );

    return GetHumanReadableName_Parms.ReturnValue;
};

FSTRING Structure

struct FString : public TArray< wchar_t > 
{ 
    FString() {}; 

    FString ( wchar_t* Other ) 
    { 
        this->Max = this->Count = *Other ? ( wcslen ( Other ) + 1 ) : 0; 

        if ( this->Count ) 
            this->Data = Other; 
    }; 

    ~FString() {}; 

    FString operator = ( wchar_t* Other ) 
    { 
        if ( this->Data != Other ) 
        { 
            this->Max = this->Count = *Other ? ( wcslen ( Other ) + 1 ) : 0; 

            if ( this->Count ) 
                this->Data = Other; 
        } 

        return *this; 
    }; 
}; 

TARRAY Structure

template< class T > struct TArray 
{ 
public: 
    T* Data; 
    int Count; 
    int Max; 

public: 
    TArray() 
    { 
        Data = NULL; 
        Count = Max = 0; 
    }; 

public: 
    int Num() 
    { 
        return this->Count; 
    }; 

    T& operator() ( int i ) 
    { 
        return this->Data[ i ]; 
    }; 

    const T& operator() ( int i ) const 
    { 
        return this->Data[ i ]; 
    }; 

    void Add ( T InputData ) 
    { 
        Data = (T*) realloc ( Data, sizeof ( T ) * ( Count + 1 ) ); 
        Data[ Count++ ] = InputData; 
        Max = Count; 
    }; 

    void Clear() 
    { 
        free ( Data ); 
        Count = Max = 0; 
    }; 
}; 

I mean, I'm sure I have to mess with the deconstructor of TARRAY or FSTRING, but I'm just not sure what I need to do...If you need any more code exerpts please let me know.

Upvotes: 1

Views: 189

Answers (1)

par
par

Reputation: 17724

The call to free( Data ) needs to be made when the object is destroyed.

Create a destructor in your TArray struct that calls Clear():

~TArray() { Clear(); }

Also, you're going to want to test Data to make sure it isn't NULL before you free() it (and set it to NULL afterwards per Rafael's comment). So in the Clear() method change the free() line to:

if ( Data != NULL ) {
    free( Data );
    Data = NULL;   // edit per Rafael Baptista
}

Upvotes: 3

Related Questions