开发者

Function is causing huge memory leak?

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 multip开发者_如何学Clier = 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;


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, '::'.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜