stack vs heap destruction problem

J

Jamie Burns

Hello,

I have a function which uses an object I made. The object simply parses a
text file and provides access to some of the data.

I am trying to use it in a method of a different object.

The problem is, when I declare it like this:

{
Properties properties("/tmp/data.properties");
// use object...
}

It causes a SEGFAULT when its destructor is called every time.

But if I do:

{
auto_ptr<Properties> properties(new Properties("/tmp/data.properties"));
// use object...
}

Then it destructs perfectly every time.

Is there an issue declaring things on the stack vs heap?

Why does this happen?

Jamie.
 
S

Shezan Baig

Jamie said:
Hello,

I have a function which uses an object I made. The object simply parses a
text file and provides access to some of the data.

I am trying to use it in a method of a different object.

The problem is, when I declare it like this:

{
Properties properties("/tmp/data.properties");
// use object...
}

It causes a SEGFAULT when its destructor is called every time.

But if I do:

{
auto_ptr<Properties> properties(new Properties("/tmp/data.properties"));
// use object...
}

Then it destructs perfectly every time.

Is there an issue declaring things on the stack vs heap?

Why does this happen?

Jamie.


What does your constructor & destructor do? Pls post code.
 
D

David White

Jamie Burns said:
Hello,

I have a function which uses an object I made. The object simply parses a
text file and provides access to some of the data.

I am trying to use it in a method of a different object.

The problem is, when I declare it like this:

{
Properties properties("/tmp/data.properties");
// use object...

Have you tried _not_ using the object to see what happens?
}

It causes a SEGFAULT when its destructor is called every time.

But if I do:

{
auto_ptr<Properties> properties(new Properties("/tmp/data.properties"));
// use object...
}

Then it destructs perfectly every time.

Is there an issue declaring things on the stack vs heap?

No, unless you don't have enough stack or you are doing something somewhere
that depends on which you choose.
Why does this happen?

My guess is that you have a bug somewhere that, purely by accident,
manifests itself within the destructor when you use the automatic (stack)
version. Without seeing all the code being executed, that's the best I can
do.

DW
 

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,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top