Re: stack vs heap destruction problem

From: David White (no_at_email.provided)
Date: 02/01/05


Date: Tue, 1 Feb 2005 11:23:43 +1100


"Jamie Burns" <reply_to_group@news.com> wrote in message
news:5XyLd.35041$v8.18137@fe2.news.blueyonder.co.uk...
> 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



Relevant Pages

  • Re: stack vs heap destruction problem
    ... Jamie Burns wrote: ... when I declare it like this: ... > It causes a SEGFAULT when its destructor is called every time. ...
    (comp.lang.cpp)
  • Re: Objects and the Stack?
    ... >> Apart from getting a pointer from alloca(), ... Both forms allocate from the stack frame. ...
    (comp.lang.ada)
  • Re: Help with C-based Forth
    ... the top of the stack and *SP as the rest of the stack, ... to declare a long variable like DX and do something like ... in your case you should be using unions. ... The stack pointer is a union of a pointer to an int, ...
    (comp.lang.forth)
  • Re: Always avoid "new"
    ... Of course if I need a variable number of objects, then I use new and store ... object and make that objects destructor ensure cleanup. ... An additional advantage of allocating on the stack instead of the heap, ...
    (comp.lang.cpp)
  • Re: Petition for the removal or voluntary departure of Richard Heathfield from this newsgroup
    ... Declare an object on the stack which automatically initialises itself, ... Technically, C doesn't even require the architecture to provide a stack, so ... it's not surprising that C doesn't provide a feature for declaring objects ... to *follow* the standard. ...
    (comp.programming)

Loading