Segmentation Fault On Program Termination? - c++

This is a discussion on Segmentation Fault On Program Termination? - c++ ; Hi all, i have a problem on program termination. At the moment when main() ends my program creates a segmentation fault. #include <libmylib/myObject.h> int main() { using namespace Mylib; MyObject1_t * aObject1 = 0; MyObject2_t aObject2("someString"); aObject1 = new Object1_t(aObject2); ...

+ Reply to Thread
Results 1 to 8 of 8

Segmentation Fault On Program Termination?

  1. Default Segmentation Fault On Program Termination?

    Hi all,

    i have a problem on program termination.

    At the moment when main() ends my program creates a segmentation
    fault.


    #include <libmylib/myObject.h>

    int main() {

    using namespace Mylib;

    MyObject1_t * aObject1 = 0;
    MyObject2_t aObject2("someString");

    aObject1 = new Object1_t(aObject2);

    delete aObject1; // program still works

    // cout or other still works here

    return(0); // program creates an seg. fault

    }

    I know it's maybe tricky to say something to my problem due to missed
    postings of Object1_t and Object2_t but I hope this is some beginner-
    failure

    Goran


  2. Default Re: Segmentation Fault On Program Termination?

    Goran wrote:
    > i have a problem on program termination.
    >
    > At the moment when main() ends my program creates a segmentation
    > fault.
    >
    >
    > #include <libmylib/myObject.h>


    I suppose you think that whatever is in that header is more or
    less irrelevant here, right?

    >
    > int main() {
    >
    > using namespace Mylib;
    >
    > MyObject1_t * aObject1 = 0;
    > MyObject2_t aObject2("someString");
    >
    > aObject1 = new Object1_t(aObject2);


    You seem to freely substitue 'MyObject1_t' with 'Object1_t'. Is
    that intentional or is it just sloppy posting? If the latter,
    please consider what *we* have to work with. We don't have the
    rest of your code, we don't have a telepathic link to your brain,
    and our crystal ball is having a foggy day today.

    >
    > delete aObject1; // program still works
    >
    > // cout or other still works here
    >
    > return(0); // program creates an seg. fault
    >
    > }
    >
    > I know it's maybe tricky to say something to my problem due to missed
    > postings of Object1_t and Object2_t but I hope this is some beginner-
    > failure


    It most likely is, judging by the manner in which the material is
    presented.

    The usual advice is the following: try reducing your program to the
    bare minimum to reproduce the problem. If you can, you might just
    be able to figure it out yourself. If not, read the FAQ 5.8 and
    follow its recommendations. No, it's not a joke.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask



  3. Default Re: Segmentation Fault On Program Termination?

    On Aug 28, 3:53 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
    > Goran wrote:
    > > i have a problem on program termination.

    >
    > > At the moment when main() ends my program creates a segmentation
    > > fault.

    >
    > > #include <libmylib/myObject.h>

    >
    > I suppose you think that whatever is in that header is more or
    > less irrelevant here, right?
    >


    No, but I hope so...

    >
    >
    > > int main() {

    >
    > > using namespace Mylib;

    >
    > > MyObject1_t * aObject1 = 0;
    > > MyObject2_t aObject2("someString");

    >
    > > aObject1 = new Object1_t(aObject2);

    >
    > You seem to freely substitue 'MyObject1_t' with 'Object1_t'. Is
    > that intentional or is it just sloppy posting? If the latter,



    Yes, sloopy posting... It should be:

    aObject1 = new MyObject1_t(aObject2);

    > please consider what *we* have to work with. We don't have the
    > rest of your code, we don't have a telepathic link to your brain,
    > and our crystal ball is having a foggy day today.
    >
    >
    >
    > > delete aObject1; // program still works

    >
    > > // cout or other still works here

    >
    > > return(0); // program creates an seg. fault

    >
    > > }

    >
    > > I know it's maybe tricky to say something to my problem due to missed
    > > postings of Object1_t and Object2_t but I hope this is some beginner-
    > > failure

    >
    > It most likely is, judging by the manner in which the material is
    > presented.
    >
    > The usual advice is the following: try reducing your program to the
    > bare minimum to reproduce the problem. If you can, you might just
    > be able to figure it out yourself. If not, read the FAQ 5.8 and
    > follow its recommendations. No, it's not a joke.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask




  4. Default Re: Segmentation Fault On Program Termination?

    "Goran" <postmaschine> wrote in message
    news:1188266356.482183.289160@y42g2000hsy.googlegroups.com...
    > On Aug 28, 3:53 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
    >> Goran wrote:
    >> > i have a problem on program termination.

    >>
    >> > At the moment when main() ends my program creates a segmentation
    >> > fault.

    >>
    >> > #include <libmylib/myObject.h>

    >>
    >> I suppose you think that whatever is in that header is more or
    >> less irrelevant here, right?
    >>

    >
    > No, but I hope so...
    >
    >>
    >>
    >> > int main() {

    >>
    >> > using namespace Mylib;

    >>
    >> > MyObject1_t * aObject1 = 0;
    >> > MyObject2_t aObject2("someString");

    >>
    >> > aObject1 = new Object1_t(aObject2);

    >>
    >> You seem to freely substitue 'MyObject1_t' with 'Object1_t'. Is
    >> that intentional or is it just sloppy posting? If the latter,

    >
    >
    > Yes, sloopy posting... It should be:
    >
    > aObject1 = new MyObject1_t(aObject2);
    >
    >> please consider what *we* have to work with. We don't have the
    >> rest of your code, we don't have a telepathic link to your brain,
    >> and our crystal ball is having a foggy day today.
    >>
    >>
    >>
    >> > delete aObject1; // program still works

    >>
    >> > // cout or other still works here

    >>
    >> > return(0); // program creates an seg. fault

    >>
    >> > }

    >>
    >> > I know it's maybe tricky to say something to my problem due to missed
    >> > postings of Object1_t and Object2_t but I hope this is some beginner-
    >> > failure

    >>
    >> It most likely is, judging by the manner in which the material is
    >> presented.
    >>
    >> The usual advice is the following: try reducing your program to the
    >> bare minimum to reproduce the problem. If you can, you might just
    >> be able to figure it out yourself. If not, read the FAQ 5.8 and
    >> follow its recommendations. No, it's not a joke.


    Also, a guess, without seeing code, would point to your copy constructor
    doing something it shouldn't.

    Again, just guessing since you're not showing the code. Another valid guess
    would be line 42 of your program.



  5. Default Re: Segmentation Fault On Program Termination?

    On 2007-08-28 03:26, Goran wrote:
    > Hi all,
    >
    > i have a problem on program termination.
    >
    > At the moment when main() ends my program creates a segmentation
    > fault.
    >
    >
    > #include <libmylib/myObject.h>
    >
    > int main() {
    >
    > using namespace Mylib;
    >
    > MyObject1_t * aObject1 = 0;
    > MyObject2_t aObject2("someString");
    >
    > aObject1 = new Object1_t(aObject2);
    >
    > delete aObject1; // program still works
    >
    > // cout or other still works here
    >
    > return(0); // program creates an seg. fault
    >
    > }
    >
    > I know it's maybe tricky to say something to my problem due to missed
    > postings of Object1_t and Object2_t but I hope this is some beginner-
    > failure


    Can't say anything for sure until you show us the code for MyObject1_t
    and MyObject2_t.

    My guess would be that they both contains a std::string* (or worse
    char*) and that the text used in the constructor for aObject2 is stored
    in that. When you create aObject1 with the copy constructor you somehow
    fail to properly copy the string and when you later delete aObject1 it
    too is deleted. For some reason you then try to dereference the pointer
    in the destructor which is why the program crashes.

    --
    Erik Wikström

  6. Default Re: Segmentation Fault On Program Termination?


    Goran wrote:
    > Hi all,
    >
    > i have a problem on program termination.
    >
    > At the moment when main() ends my program creates a segmentation
    > fault.
    >
    >
    > #include <libmylib/myObject.h>
    >
    > int main() {
    >
    > using namespace Mylib;
    >
    > MyObject1_t * aObject1 = 0;
    > MyObject2_t aObject2("someString");
    >
    > aObject1 = new Object1_t(aObject2);
    >
    > delete aObject1; // program still works
    >
    > // cout or other still works here
    >
    > return(0); // program creates an seg. fault
    >
    > }
    >
    > I know it's maybe tricky to say something to my problem due to missed
    > postings of Object1_t and Object2_t but I hope this is some beginner-
    > failure


    My guess would be that something in aObject2 gets deleted when you
    call
    delete aObject1 due to the fact that MyObject1_t stole a pointer from
    MyObject2_t during the assignment. When the destructor of MyObject1_t
    was called, it deleted its member, whilst the member was never truly
    owned.

    Read up on "Deep Copying", and "Copy Constructors" in the FAQs.

    Regards,

    Werner


  7. Default Re: Segmentation Fault On Program Termination?

    Goran <postmaschine> wrote in news:1188264416.738284.208330
    @y42g2000hsy.googlegroups.com:

    > Hi all,
    >
    > i have a problem on program termination.
    >
    > At the moment when main() ends my program creates a segmentation
    > fault.
    >
    >
    > #include <libmylib/myObject.h>
    >
    > int main() {
    >
    > using namespace Mylib;
    >
    > MyObject1_t * aObject1 = 0;
    > MyObject2_t aObject2("someString");
    >
    > aObject1 = new Object1_t(aObject2);
    >
    > delete aObject1; // program still works
    >
    > // cout or other still works here
    >
    > return(0); // program creates an seg. fault
    >
    > }
    >


    See Victor's posting about crystal balls etc, but if I had to guess...

    aObject2 is the only variable on the stack and I would guess that it's
    destruction at the return is what's causing the fault. Since you pass
    in a literal text string, I further guess that aObject2 has grabbed that
    pointer and you are trying to delete it when aObject2 is destroyed.

    Can't say much more than that with out actual code.

    joe


  8. Default Re: Segmentation Fault On Program Termination?

    On Aug 28, 1:48 pm, Joe Greer <jgr...@doubletake.com> wrote:
    > Goran <postmasch...> wrote in news:1188264416.738284.208330
    > @y42g2000hsy.googlegroups.com:
    >
    >
    >
    > > Hi all,

    >
    > > i have a problem on program termination.

    >
    > > At the moment when main() ends my program creates a segmentation
    > > fault.

    >
    > > #include <libmylib/myObject.h>

    >
    > > int main() {

    >
    > > using namespace Mylib;

    >
    > > MyObject1_t * aObject1 = 0;
    > > MyObject2_t aObject2("someString");

    >
    > > aObject1 = new Object1_t(aObject2);

    >
    > > delete aObject1; // program still works

    >
    > > // cout or other still works here

    >
    > > return(0); // program creates an seg. fault

    >
    > > }

    >
    > See Victor's posting about crystal balls etc, but if I had to guess...
    >
    > aObject2 is the only variable on the stack and I would guess that it's
    > destruction at the return is what's causing the fault. Since you pass
    > in a literal text string, I further guess that aObject2 has grabbed that
    > pointer and you are trying to delete it when aObject2 is destroyed.
    >
    > Can't say much more than that with out actual code.
    >
    > joe


    Thanks to all! I've found the prob. My object used pointers for member
    var. and I had NO copy constructor... Still learning

    Goran


+ Reply to Thread

Similar Threads

  1. segmentation fault...
    By Application Development in forum c++
    Replies: 4
    Last Post: 07-23-2007, 02:50 PM
  2. Re: IDL Segmentation Fault
    By Application Development in forum Idl-pvwave
    Replies: 13
    Last Post: 11-07-2006, 11:07 AM
  3. I want my segmentation fault!
    By Application Development in forum C
    Replies: 12
    Last Post: 10-07-2006, 06:47 PM
  4. segmentation fault
    By Application Development in forum ASM x86 ASM 370
    Replies: 7
    Last Post: 05-19-2006, 10:09 AM
  5. segmentation fault?!
    By Application Development in forum IMAP
    Replies: 1
    Last Post: 05-21-2004, 05:44 PM