Recently I started learning C++. I'm used to programing in Objective-C so maybe I'm doing some things wrong. I have made a transcript of the gdb session:
Script started on Sat Jan 15 17:47:24 2011
bash-3.2$ gdb sscm
GNU gdb 6.3.50-20050815 (Apple version gdb-1510) (Wed Sep 22 02:45:02 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries ... done
(gdb) break ServerOptions::ServerOptions
[0] cancel
[1] all
[2] ServerOptions::ServerOptions(int, char const**) at ServerOptions.cpp:33
[3] ServerOptions::ServerOptions(int, char const**) at ServerOptions.cpp:33
> 3
Breakpoint 1 at 0x7595: file ServerOptions.cpp, line 33.
(gdb) list
9
10 #include <iostream>
11
12 #include "Logger.h"
13 #include "ServerOptions.h"
14
15
16 int main(int argc, const char **argv) {
17 using namespace std;
18 ServerOptions options(argc, argv);
(gdb) run
Starting program: /Users/ief2/Desktop/SSCM-1.0.0.9/server/sscmd
Reading symbols for shared libraries ++. done
Breakpoint 1, ServerOptions::ServerOptions (this=0xbffff7a8, argc=1, argv=0xbffff7f0) at ServerOptions.cpp:33
33 ServerOptions::ServerOptions(int argc, const char **argv)
(gdb) s
36 bool noErrorsEncountered = true;
(gdb) list
31 is a fatal error.
32 */
33 ServerOptions::ServerOptions(int argc, const char **argv)
34 {
35 /* setup default values */
36 bool noErrorsEncountered = true;
37 int isDaemon = true;
38 int displayHelp = false;
39 int port = DEFAULT_BIND_PORT;
40 std::string rootFolder = "";
(gdb) s
37 int isDaemon = true;
(gdb) s
38 int displayHelp = false;
(gdb) s
39 int port = DEFAULT_BIND_PORT;
(gdb)
40 std::string rootFolder = "";
(gdb) list
35 /* setup default values */
36 bool noErrorsEncountered = true;
37 int isDaemon = true;
38 int displayHelp = false;
39 int port = DEFAULT_BIND_PORT;
40 std::string rootFolder = "";
41 int isSilent = 0;
42
43 Logger logger = Logger::sharedLogger();
44
(gdb)
45 /* Process the configuration file */
46 {
47 int status;
48 std::string anArgument;
49 KeyFile file(DEFAULT_CONFIGURATION_FILE,
50 &status);
51
52 if(status != 0 && status != ENOENT) {
53 std::stringstream strstr;
54 strstr <<
(gdb) lists[Klists[K
41 int isSilent = 0;
(gdb)
43 Logger logger = Logger::sharedLogger();
(gdb)
Logger::sharedLogger () at Logger.cpp:28
28 if(logger == NULL) {
(gdb) list
23 }
24
25 Logger& Logger::sharedLogger(void) {
26 static Logger *logger = NULL;
27
28 if(logger == NULL) {
29 new Logger(开发者_StackOverflow中文版"");
30 }
31
32 return *logger;
(gdb) s
29 new Logger("");
(gdb)
Logger::Logger (this=0x100160, fullPath=@0xbffff568) at Logger.cpp:16
16 Logger::Logger(std::string fullPath) {
(gdb) list
11
12 #pragma mark -
13 #pragma mark Public Implementation
14
15 #pragma mark Constructors and Destructors
16 Logger::Logger(std::string fullPath) {
17 _filename = fullPath;
18 _isSilent = false;
19 }
20
(gdb) s
17 _filename = fullPath;
(gdb)
18 _isSilent = false;
(gdb)
19 }
(gdb)
Logger::sharedLogger () at Logger.cpp:32
32 return *logger;
(gdb)
33 }
(gdb)
Logger::Logger (this=0xbffff6ec) at Logger.h:21
21 class Logger {
(gdb) llist ist
16 #include <cerrno>
17 #include <cstring>
18
19 #pragma mark -
20 #pragma mark Private Interface
21 class Logger {
22 private:
23 std::string _filename;
24 bool _isSilent;
25
(gdb)
26 public:
27 #pragma mark Constructors and Destructors
28 Logger(std::string fullPath);
29 ~Logger(void);
30 static Logger& sharedLogger(void);
31
32 #pragma mark Getters and Setters
33 std::string getFilename(void) const;
34 void setFilename(std::string newF);
35
(gdb) s
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00000000
0x980f7b33 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string ()
(gdb) ki
Kill the program being debugged? (y or n) y
(gdb) q
bash-3.2$ exit
exit
Script done on Sat Jan 15 17:48:59 2011
Any idea why I'm getting this error? If you need more code, I'll certainly post it!
Thank you in advance, ief2
You are dereferencing the NULL
pointer on line 32. On line 29, you create a new Logger
, but you never assign it to logger
. So when you try to return it from sharedLogger
, logger
is still NULL
, and you get the access violation. Change line 29 to
logger = new Logger("");
and it should work.
I think this is the problem:
Logger& Logger::sharedLogger(void) {
static Logger *logger = NULL;
if(logger == NULL) {
new Logger("");
}
return *logger;
}
If you are aiming at the Singleton pattern - assign that newly created instance address to the static logger
variable, i.e.:
logger = new Logger("");
精彩评论