473,326 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

Aug 28 '07 #1
4 2289
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
Aug 28 '07 #2

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

Aug 28 '07 #3
Goran <po**********@gmail.comwrote 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

Aug 28 '07 #4
On Aug 28, 1:48 pm, Joe Greer <jgr...@doubletake.comwrote:
Goran <postmasch...@gmail.comwrote 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

Aug 29 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
59
by: Christian Christmann | last post by:
Hi, I'm wondering why this program does not crash with a segmentation fault: #include <malloc.h> #include <string.h> #include <stdio.h> int main()
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
14
by: asit | last post by:
#include <stdio.h> int main() { int i; for(i=1;i<=10;i++) main(); printf("C is urs.."); return 0; }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.