views:

169

answers:

3

I have the following function:

void CGlEngineFunctions::GetBezierOpposite( const POINTFLOAT &a,const POINTFLOAT &center, POINTFLOAT &b, float blength )
{


    POINTFLOAT v;
    v.x = a.x - center.x;
    v.y = a.y - center.y;

    float alength = GetDistance(a,center);

    if(blength == 0)
    {
        blength = alength;
    }

    float multiplier = blength / alength;

    b.x = center.x - multiplier * v.x;
    b.y = center.y - multiplier * v.y;

}

I have narrowed the problem down to the least 2 lines:

b.x = center.x - multiplier * v.x;
b.y = center.y - multiplier * v.y;

Every time I call this repeatedly, memory shots up until it crashes.

I use it like this:

glEngine.functions.GetBezierOpposite(usrpt[0].LeftHandle,
            usrpt[0].UserPoint,usrpt[0].RightHandle,0);

I really do not see how this could cause any problems. To test, I changed it to this:

void CGlEngineFunctions::GetBezierOpposite( const POINTFLOAT &a,const POINTFLOAT &center, POINTFLOAT &b, float blength )
{


    POINTFLOAT v;
    v.x = a.x - center.x;
    v.y = a.y - center.y;

    float alength = GetDistance(a,center);

    if(blength == 0)
    {
        blength = alength;
    }

    float multiplier = blength / alength;

    b.x = 5;
    b.y = 5;

}

When I do this it has absolutely no issues. I do not see how doing arithmetic can cause the memory usage to shoot up.

Thanks

could it be cause if alength and blength = 0?

POINTFLOAT:
float x;
float y;
A: 

I can't see anything in that code to cause a memory leak - what uses usrpt[0].RightHandle once it has been calculated by this function?

Chris Card
Well, I added an if to check if alength was 0 because I guess dividing by 0 was causing the leak, but why though?
Milo
@Jex: Divide by zero cannot cause a leak. Divide by zero *can* crash your program, depending on your platform, compiler settings, etc. If your program crashed inside this function, however, some memory leak detecting tools might point here as the cause of the crash, but not as a cause of a memory leak.
Billy ONeal
@Billy: Divide by zero can do anything. :) (okay, okay, in the real world it won't leak unless you've got some OS-specific hook to handle exceptions, are allocating in there and never releasing, etc.)
GMan
@GMan: But a divide by zero itself is not a memory leak. It *can* cause other interesting undefined things to happen though, yes. Sometimes I wonder why C++ doesn't mandate IEEE 754.
Billy ONeal
@Billy: Well, I didn't say it is necessarily a leak, I said it's a possible leak. You did say necessarily not a leak. :)
GMan
A: 

If POINTFLOAT is some complicated class (you have tagged your question C++) and you have overloaded the operators in the expression, how could we know?

Also you didn't tell us much why you think these expression are the culprit, neither about your compiler, platform, OS...

Easiest way to find all this out is valgrind (for unixen) or some similar tool. They will tell you exactly where the allocation takes place that ends up being leaked.

Jens Gustedt
+1  A: 

If the GetDistance calls this method, there may be a Stack Overflow.

If other threads call this method, there may be a Stack Overflow.

Check the POINTFLOAT definition. IMHO, it should be modified to provide subtraction operations. You should not need to reference any of the structure's members. But then this comment would be about C++.

You should remove the 'C' language tag, since the C language does not provide a scope resolution operator, '::'.

Thomas Matthews
Actually, I think the C tag is okay here. You don't need to understand C++ here -- the problem is applicable to both C and C++.
Billy ONeal