By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

Memory Leak Error

P: n/a
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[i];
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

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

Jul 22 '05 #2

P: n/a
"Brad Moore" <mo****@cis.ohio-state.edu> wrote in message
news:3F**************@cis.ohio-state.edu...
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[i];
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

Jul 22 '05 #3

P: n/a
> 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[i];
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?
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.