最近我开始学习C ++。我习惯用Objective-C编程,所以也许我做错了。我已经制作了gdb会话的成绩单:
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("");
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
知道我为什么会收到此错误?如果您需要更多代码,我一定会发布它!
提前谢谢你,ief2
答案 0 :(得分:2)
您要取消引用第32行的NULL
指针。在第29行,您创建了一个新的Logger
,但您从未将其指定给logger
。因此,当您尝试从sharedLogger
返回时,logger
仍然是NULL
,并且您获得了访问冲突。将第29行更改为
logger = new Logger("");
它应该有用。
答案 1 :(得分:1)
我认为这是问题所在:
Logger& Logger::sharedLogger(void) {
static Logger *logger = NULL;
if(logger == NULL) {
new Logger("");
}
return *logger;
}
如果你的目标是Singleton pattern - 将新创建的实例地址分配给静态logger
变量,即:
logger = new Logger("");