Memory Leak Error

B

Brad Moore

Hey All,

I need help with debugging some code, if this isn't the correct
newsgroup to ask in, disregard this message.

I'm having difficulty pinpointing a memory leak in my code, I believe it
has something to do with an inherited class I have, but I fail to see
anything wrong. Here is the problematic code. I've hunted the bug down
to this function using gdb and commenting out parts of my code. I
originally used CIntersect as a base class and inherited from it, but
even without inheriting it still causes a bus error.

SbColor shadeRay(OSUInventorScene * scene, SbLine &ray, int recur,
int maxRecur)
{
int numObjects = scene->Objects.getLength();

SbColor color(0,0,0);
OSUObjectData * obj;
CIntersect * intersect = 0;
float distance = -1.0;

if (recur >= maxRecur)
return color;

// calculate intersection point
for (int i = 0; i < numObjects; i++) {
OSUObjectData * tempObj = (OSUObjectData *)scene->Objects;
CIntersect * tempIntersect = 0;

tempIntersect = new CIntersect();

float tempDis = tempIntersect->getDistance();

if ((tempDis > 0) && ( tempDis < distance || distance == -1.0)) {
distance = tempDis;
obj = tempObj;

if (intersect != 0)
delete intersect;

intersect = tempIntersect;
}
}

if (intersect != 0)
delete intersect;

return color;
}

I've commented out objects, replaced them with dummy's, and it seems
that is it the CIntersect class that is causing the memory leak.

class CIntersect {
public:
CIntersect() { }
virtual ~CIntersect() { }
CIntersect(OSUObjectData * m_obj, SbLine& ray)
{
this->m_obj = m_obj;
}
SbVec3f getPoint() { return m_point; }
SbVec3f getNormal() { return m_normal; }
SbVec3f getIncoming() { return m_incoming; }
float getDistance() { return m_distance; }
OSUObjectData * getObject() { return m_obj; }
bool isFrontFace() { return m_isFrontFace; }

protected:
SbVec3f m_normal;
SbVec3f m_point;
SbVec3f m_incoming;
float m_distance;
bool m_isFrontFace;
OSUObjectData * m_obj;
};

Honestly, this isn't my original code, but this is the exact copy of the
mininum set of code I can come to and still cause the bus error. I
consider this a memory leak because the bus error occurs only when I
call shadeRay() a large amount of times. All the other non-standard
types are from tried and true libraries, so once again, I believe the
code at fault is CIntersect.

If you don't see anything wrong with this code, it'll still be a great
help because I'll then know to look elsewhere.

Thanks for any help,
-Brad
 
B

Brad Moore

Figured it out after several debugging. It wasn't a error with my
understanding of inheritance after all, it was a logic error. Thanks to
all that spent the time to look at my code.

-Brad
 
D

David White

Brad Moore said:
Hey All,

I need help with debugging some code, if this isn't the correct
newsgroup to ask in, disregard this message.

I'm having difficulty pinpointing a memory leak in my code, I believe it
has something to do with an inherited class I have, but I fail to see
anything wrong. Here is the problematic code. I've hunted the bug down
to this function using gdb and commenting out parts of my code. I
originally used CIntersect as a base class and inherited from it, but
even without inheriting it still causes a bus error.

SbColor shadeRay(OSUInventorScene * scene, SbLine &ray, int recur,
int maxRecur)
{
int numObjects = scene->Objects.getLength();

SbColor color(0,0,0);
OSUObjectData * obj;
CIntersect * intersect = 0;
float distance = -1.0;

if (recur >= maxRecur)
return color;

// calculate intersection point
for (int i = 0; i < numObjects; i++) {
OSUObjectData * tempObj = (OSUObjectData *)scene->Objects;
CIntersect * tempIntersect = 0;

tempIntersect = new CIntersect();


Wouldn't this be better?
CIntersect * tempIntersect = new CIntersect();

Note that you create a new CIntersect every iteration.
float tempDis = tempIntersect->getDistance();

if ((tempDis > 0) && ( tempDis < distance || distance == -1.0)) {
distance = tempDis;
obj = tempObj;

if (intersect != 0)

You don't need to test for null. Just delete it.
delete intersect;

You only delete the object if the distance conditions above are satisfied.
If they are not satisfied more than once for all iterations then you will
get a leak.
intersect = tempIntersect;
}
}

if (intersect != 0)

Forget the test again. Just delete it.
delete intersect;

This will delete one of the CIntersect objects not deleted in the loop, so
you will get a leak if there was more than one.
return color;
}

I've commented out objects, replaced them with dummy's, and it seems
that is it the CIntersect class that is causing the memory leak.

class CIntersect {
public:
CIntersect() { }
virtual ~CIntersect() { }
CIntersect(OSUObjectData * m_obj, SbLine& ray)
{
this->m_obj = m_obj;
}
SbVec3f getPoint() { return m_point; }
SbVec3f getNormal() { return m_normal; }
SbVec3f getIncoming() { return m_incoming; }
float getDistance() { return m_distance; }
OSUObjectData * getObject() { return m_obj; }
bool isFrontFace() { return m_isFrontFace; }

protected:
SbVec3f m_normal;
SbVec3f m_point;
SbVec3f m_incoming;
float m_distance;
bool m_isFrontFace;
OSUObjectData * m_obj;
};

This class is not doing any memory allocations, so I don't see how it can be
the culprit.
Honestly, this isn't my original code, but this is the exact copy of the
mininum set of code I can come to and still cause the bus error. I
consider this a memory leak because the bus error occurs only when I
call shadeRay() a large amount of times. All the other non-standard
types are from tried and true libraries, so once again, I believe the
code at fault is CIntersect.

If you don't see anything wrong with this code, it'll still be a great
help because I'll then know to look elsewhere.

DW
 
D

Duane Hebert

SbColor shadeRay(OSUInventorScene * scene, SbLine &ray, int recur,
int maxRecur)
{
int numObjects = scene->Objects.getLength();

SbColor color(0,0,0);
OSUObjectData * obj;
CIntersect * intersect = 0;
float distance = -1.0;

if (recur >= maxRecur)
return color;

// calculate intersection point
for (int i = 0; i < numObjects; i++) {
OSUObjectData * tempObj = (OSUObjectData *)scene->Objects;
CIntersect * tempIntersect = 0;

tempIntersect = new CIntersect();

float tempDis = tempIntersect->getDistance();

if ((tempDis > 0) && ( tempDis < distance || distance == -1.0)) {
distance = tempDis;
obj = tempObj;

if (intersect != 0)
delete intersect;

intersect = tempIntersect;
}
}

if (intersect != 0)
delete intersect;

return color;
}

For numObject times, you create a new tempintersect. Aside from the
dereferencing
that occurs conditionally in the for loop, I don't see it being deleted
here. Outside of the
for loop intersect is being deleted once. If numObjects > 1, you would have
that many leaks here.
For every instance of new, there should be an instance of delete no?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top