LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-24-2005, 02:38 AM   #1
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Rep: Reputation: 30
seg. fault because of extern var?


Hello
I'm getting an error when I run my program.
This is what valgrind tells me:
Quote:
==3326== Syscall param modify_ldt(ptr) points to uninitialised byte(s)
==3326== at 0x1CDB83F2: _nv000001gl (in /usr/lib/libnvidia-tls.so.1.0.7174)
==3326== Address 0x52BFE3D4 is on thread 1's stack
==3326==
==3326== Invalid read of size 4
==3326== at 0x1C245946: std:stream::sentry::sentry(std:stream&) (in /usr/lib/libstdc++.so.5.0.6)
==3326== by 0x1C245C54: std::basic_ostream<char, std::char_traits<char> >& std:perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/libstdc++.so.5.0.6)
==3326== by 0x80541A8: logfile::comment(char const*) (basic_string.h:717)
==3326== by 0x80571E7: Sound::Init() (Sound.cpp:170)
==3326== by 0x8056ADF: Sound::Sound() (Sound.cpp:25)
==3326== by 0x80564E8: __static_initialization_and_destruction_0(int, int) (MouseEvent.cpp:24)
==3326== by 0x8056541: _GLOBAL__I_MyTestSound (MouseEvent.cpp:24)
==3326== by 0x8066184: (within /home/hylke/combatofdeath/combatofdeath)
==3326== by 0x804E338: _init (in /home/hylke/combatofdeath/combatofdeath)
==3326== by 0x80660BA: __libc_csu_init (in /home/hylke/combatofdeath/combatofdeath)
==3326== by 0x1C2CF44D: __libc_start_main (in /lib/libc-2.3.4.so)
==3326== by 0x804F840: ??? (start.S:119)
==3326== Address 0xFFFFFFF4 is not stack'd, malloc'd or (recently) free'd
==3326==
==3326== Process terminating with default action of signal 11 (SIGSEGV)
==3326== GPF (Pointer out of bounds?)
==3326== at 0x1C245946: std:stream::sentry::sentry(std:stream&) (in /usr/lib/libstdc++.so.5.0.6)
==3326== by 0x1C245C54: std::basic_ostream<char, std::char_traits<char> >& std:perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/libstdc++.so.5.0.6)
==3326== by 0x80541A8: logfile::comment(char const*) (basic_string.h:717)
==3326== by 0x80571E7: Sound::Init() (Sound.cpp:170)
==3326== by 0x8056ADF: Sound::Sound() (Sound.cpp:25)
==3326== by 0x80564E8: __static_initialization_and_destruction_0(int, int) (MouseEvent.cpp:24)
==3326== by 0x8056541: _GLOBAL__I_MyTestSound (MouseEvent.cpp:24)
==3326== by 0x8066184: (within /home/hylke/combatofdeath/combatofdeath)
==3326== by 0x804E338: _init (in /home/hylke/combatofdeath/combatofdeath)
==3326== by 0x80660BA: __libc_csu_init (in /home/hylke/combatofdeath/combatofdeath)
==3326== by 0x1C2CF44D: __libc_start_main (in /lib/libc-2.3.4.so)
==3326== by 0x804F840: ??? (start.S:119)
==3326==
==3326== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 77 from 2)
==3326== malloc/free: in use at exit: 632281 bytes in 428 blocks.
==3326== malloc/free: 428 allocs, 0 frees, 632281 bytes allocated.
==3326== For counts of detected errors, rerun with: -v
==3326== searching for pointers to 428 not-freed blocks.
==3326== checked 2217172 bytes.
==3326==
==3326== LEAK SUMMARY:
==3326== definitely lost: 0 bytes in 0 blocks.
==3326== possibly lost: 0 bytes in 0 blocks.
==3326== still reachable: 632281 bytes in 428 blocks.
==3326== suppressed: 0 bytes in 0 blocks.
==3326== Reachable blocks (those to which a pointer was found) are not shown.
This is how my function logfile::comment looks like:
Code:
void logfile::comment(const char* logItem)
{
     string writeItem = "## ";
     
     writeItem = "## ";
     writeItem.append(logItem);
     writeItem.append(" ##\n");
     CoDfile << writeItem.c_str();
}
And this is the call from Sound::Init
Code:
	CoDlog.comment("Sound initialisation");
Because I thought the above code will not generate a segmentation fault and by viewing the line:
Quote:
Address 0xFFFFFFF4 is not stack'd, malloc'd or (recently) free'd
From my valgrind output I thought it might be CoDlog that is causing the segmentation fault.
CoDlog is defined in extern_vars.h as this:
Code:
extern logfile CoDlog;
After that it's redefined in ErrorCheck.cpp(extern_vars.h is included in ErrorCheck, And ErrorCheck.h is included by ErrorCheck.cpp) like this:
Code:
logfile CoDlog;
My class Sound includes ErrorCheck.h in Sound.h.
But I can't put
Code:
logfile CoDlog;
in Sound.cpp because it's already redefined in ErrorCheck.cpp.
So i'm thinking that is causing the problem.
Could that be correct or is it absolutly nonsense?
Thanx Hylke
 
Old 06-24-2005, 03:42 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Try removing the extern and #including ErrorCheck.cpp in Sound.cpp temporarily to see if that fixes it. Address 0xFFFFFFF4 seems like it started as a NULL pointer to an object somewhere and was successfully offset by -12 (except normally it would go the other way like when accessing a data member). I have a strong feeling that the extern is not the cause of the problem because all that does is tell the compiler that it will be defined and not to worry. Stack variables that are not pointers don't generally cause segfaults unless they contain pointers or uninitialized data. What exactly is CoDfile?
ta0kira
 
Old 06-24-2005, 05:54 AM   #3
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by ta0kira
Try removing the extern and #including ErrorCheck.cpp in Sound.cpp temporarily to see if that fixes it.
I do not have an externvariable in Sound.cpp, all my extern variables are in extern_vars.h, and #include "ErrorCheck.cpp" is in Sound.h.
But I'Il try that to see if that fixes it.
Quote:
Address 0xFFFFFFF4 seems like it started as a NULL pointer to an object somewhere and was successfully offset by -12 (except normally it would go the other way like when accessing a data member). I have a strong feeling that the extern is not the cause of the problem because all that does is tell the compiler that it will be defined and not to worry. Stack variables that are not pointers don't generally cause segfaults unless they contain pointers or uninitialized data. What exactly is CoDfile?
ta0kira
CoDfile is a (darn I forgot how it's called) thing from the class logfile.
My logfile class looks like this:
Code:
class logfile
{
     //variabels
     ofstream CoDfile;
     
public:
     logfile();
     ~logfile();
          
     void event(const char*);
     void comment(const char*);
};
EDIT:
When I remove ErrorCheck.cpp from Sound.h I don't have a segmentation fault.
Btw, I appreciate your help so far, I actually did not think anyone would help me

Last edited by hylke; 06-24-2005 at 06:04 AM.
 
Old 06-24-2005, 06:45 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
When I remove ErrorCheck.cpp from Sound.h I don't have a segmentation fault.
I think this is your problem here; you have more than one definition of CoDfile during the link process, which is remedied by removing ErrorCheck.cpp from Sound.h. The linker would naturally link to the definition that is included rather than the one that is in the ErrorCheck.o file, and chances are the one you want to use is intialized in the opposite one. You shouldn't ever include a .cpp in a .h, and make sure that all global definitions are in .cpp. What in Sound.cpp depends on ErrorCheck.cpp? If you are compiling ErrorCheck.cpp separately (which it seems like you are doing) then linking it to the compiled Sound.cpp then you should be fine by including ErrorCheck.h in Sound.h (that is; only if you only declare functions and declare/define classes in your .h files and you only define functions and global variables in your .cpp files).
ta0kira
 
Old 06-24-2005, 06:52 AM   #5
hylke
Member
 
Registered: Apr 2004
Location: the Netherlands
Distribution: Ubuntu 7.04
Posts: 329

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by ta0kira
I think this is your problem here; you have more than one definition of CoDfile during the link process, which is remedied by removing ErrorCheck.cpp from Sound.h. The linker would naturally link to the definition that is included rather than the one that is in the ErrorCheck.o file, and chances are the one you want to use is intialized in the opposite one. You shouldn't ever include a .cpp in a .h, and make sure that all global definitions are in .cpp. What in Sound.cpp depends on ErrorCheck.cpp? If you are compiling ErrorCheck.cpp separately (which it seems like you are doing) then linking it to the compiled Sound.cpp then you should be fine by including ErrorCheck.h in Sound.h (that is; only if you only declare functions and declare/define classes in your .h files and you only define functions and global variables in your .cpp files).
ta0kira
Oh sorry, I meant ErrorCheck.h instead of ErrorCheck.cpp.
But just renaming the variable from CoDlog in to something else in my ErrorCheck.* files fixed it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
weird seg fault zaichik Programming 7 01-29-2005 06:34 AM
RH 7.2 crash...getting seg fault when using ls sakima Red Hat 1 10-11-2004 09:14 AM
C seg fault drigz Programming 5 10-01-2004 03:35 PM
xmms - seg fault :( :( xconspirisist Linux - Software 2 02-07-2004 05:38 PM
seg fault on fclose (); lackluster Programming 4 06-28-2003 07:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration