Segmentation Fault On Program Termination?

G

Goran

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
 
V

Victor Bazarov

Goran said:
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
 
G

Goran

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

No, but I hope so...
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);
 
J

Jim Langston

Goran said:
No, but I hope so...



Yes, sloopy posting... It should be:

aObject1 = new MyObject1_t(aObject2);

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.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.
 
W

werasm

Goran said:
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
 
J

Joe Greer

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
 
G

Goran

@y42g2000hsy.googlegroups.com:















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
 

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
473,781
Messages
2,569,619
Members
45,315
Latest member
VernellPos

Latest Threads

Top