Unused method is acting up

M

Martin Magnusson

Hi,

I have defined a method of a base class which writes to a C-style array
without initializing it. (No, I don't like C arrays either, but I have
to use it here because of another library I'm using.) Calling this
without first allocating memory would of course give a run-time error,
but what puzzles me is that I get a segmentation fault when this method
is defined, even if it is never called at all! Is that normal?

The function looks like below, and the seg fault seems to originate from
a line where I call TS::Clone().


class S
{
//...
virtual void Skew( int* result ) const
{
vector<Real> v = this->AsReals();
for (unsigned i = 0; i < v.size(); ++i)
result = (int)rint(v);
}

virtual S* Clone() const = 0;
};


class TS : public S
{
//...
// does not override Skew()

TS* TS::Clone() const
{
return new TS(*this);
}
};
 
H

Howard

Martin Magnusson said:
Hi,

The function looks like below, and the seg fault seems to originate from
a line where I call TS::Clone().
TS* TS::Clone() const
{
return new TS(*this);
}

What does your copy-constructor look like? More importantly probably: is
"this" non-null at the time you call the Clone function? (i.e., are you
calling Clone on a null pointer? We can't see the calling code, so we don't
know.)

If you get a run-time error just by defining a function that is never
called, it is NOT that function causing the problem (unless it really IS
getting called, and you just didn't know it...a debugger will tell you
that!). It is a problem elsewhere that simply fails to crash in some
limited testing when that function is not defined. Often this is caused by
referencing memory you haven't allocated or have already deallocated. (I
look first for null pointers, deleted pointers, and writing past array
bounds.)

Try debugging your app to see what is going on more clearly.

-Howard
 
V

Victor Bazarov

Martin Magnusson said:
I have defined a method of a base class which writes to a C-style array
without initializing it. (No, I don't like C arrays either, but I have
to use it here because of another library I'm using.) Calling this
without first allocating memory would of course give a run-time error,
but what puzzles me is that I get a segmentation fault when this method
is defined, even if it is never called at all! Is that normal?

No, it's not normal, but apparently it has nothing to do with that.
The segmentation fault is likely due to some other error, and if
you define your member function, you just help _uncover_ that other
error.
The function looks like below, and the seg fault seems to originate from
a line where I call TS::Clone().

"Seems to"? Do you have a debugger? Do you know how to use one?
If not, follow the recommendations in FAQ 5.8, then we will try
to help. Please try to avoid posting code that "looks like the
non-working variation". You either post the real non-working code
or you likely all by yourself.

Memory problems like the one you describe are not easy to catch,
I'll give you that. However, they are not impossible to find and
just require some elbow grease.

Look for uninitialised pointers (not just arrays). Look for memory
_overruns_ (they are the most destructive little pests). Look for
failed memory allocations. And, do post your _real_ code after
distilling it to the minimum that still exhibits the error. We can
help, I am sure of it.
class S
{
//...
virtual void Skew( int* result ) const
{
vector<Real> v = this->AsReals();
for (unsigned i = 0; i < v.size(); ++i)
result = (int)rint(v);
}

virtual S* Clone() const = 0;
};


class TS : public S
{
//...
// does not override Skew()

TS* TS::Clone() const
{
return new TS(*this);
}
};
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top