473,766 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("someS tring");

aObject1 = new Object1_t(aObje ct2);

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 2320
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("someS tring");

aObject1 = new Object1_t(aObje ct2);

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("someS tring");

aObject1 = new Object1_t(aObje ct2);

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**********@g mail.comwrote in news:1188264416 .738284.208330
@y42g2000hsy.go oglegroups.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("someS tring");

aObject1 = new Object1_t(aObje ct2);

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...@doublet ake.comwrote:
Goran <postmasch...@g mail.comwrote in news:1188264416 .738284.208330
@y42g2000hsy.go oglegroups.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("someS tring");
aObject1 = new Object1_t(aObje ct2);
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
6809
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 works fine and do all what I need. But at the end of the execution, I can read "Segmentation Fault". The segmentation fault appear at the end of my script execution,
3
1939
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: /var/www/cgi-bin/script.cgi when i debug the program i get Segmentation fault gdb ./script.cgi
16
8995
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 who don't speak french arbre means tree.
3
11443
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 () from /lib/tls/libc.so.6 It's really strange; I just call malloc() like "tmp=malloc(size);" the system gives me Segmentation fault I want to write a code to do like a dynamic array, and the code is as
5
2997
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 understood that a segmentation fault can occur whenever I declare a pointer and I leave it un-initialized. So I thought the problem here is with the (const char *)s in the stuct flightData (please note that I get the same fault declaring as char * the...
59
2911
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
5879
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 testcases we experiance Segmentation fault from the python libraries. If i know how to handle the exception for Segmentation fault , it will help me complete the run on any testcase , even if i experiance Seg Fault due to any one or many functions in...
6
5043
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 gcc. The only difference is that in first I only call "main" and in second call
14
2667
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
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9837
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.