Ncurses and Segmentation Fault

Hi, I'm using gentoo - its a fairly clean environment - specifically set up for just this one project - ie. almost straight from the Gentoo Handbook (10.1 Gentoo AMD64 No-Multilib gentoo-source kernel).

I was trying to write a simple ncurses program, I have done several projects using Visual C++ for windows, but have ventured into linux for something new.

I have a total of four files, which I will list below, they throw no errors on build, but upon execution, I get a segmentation fault, I'm almost certain it is from the use of ncurses, but I lack experience with linux and ncurses and have exhausted my intellect trying to determine the fault.

The first two files are the Console.h and Console.cpp files...

Console.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once
class Console
{
public:
	Console(void);
	~Console(void);
	bool Create(void);
private:
	void Clear(void);
	void Destroy(void);
	char* TerminalType;
	FILE* InputText;
	FILE* OutputText;
	SCREEN* ConsoleTerminal;
	SCREEN* OriginalTerminal;
	WINDOW* ConsoleWindow;
};

Console.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "curses.h"
#include "Console.h"
Console::Console(void)
{
	Clear();
}
Console::~Console(void)
{
	Destroy();
}
void Console::Clear(void)
{
	OutputText=NULL;
	InputText=NULL;
	TerminalType=NULL;
	ConsoleTerminal=NULL;
	OriginalTerminal=NULL;
	ConsoleWindow=NULL;
	return;
}
bool Console::Create(void)
{
	OutputText=new FILE;
	InputText=new FILE;
	TerminalType=(char*)"linux";
	ConsoleTerminal=newterm(TerminalType, OutputText, InputText);
	OriginalTerminal=set_term(ConsoleTerminal);
	ConsoleWindow=initscr();
	return true;
}
void Console::Destroy(void)
{
	endwin();
	set_term(OriginalTerminal);
	delscreen(ConsoleTerminal);
	Clear();
	return;
}

This class is invoked by my entry program...

Entry.cpp
1
2
3
4
5
6
7
#include "Console.cpp"
int main()
{
	Console* output;
	output=new Console;
	output->Create();
}

and built by a rough makefile (please excuse this makefile, I'm real new to this, and kinda had to wing it...)

Makefile
1
2
3
4
all:Entry.cpp Console.h Console.cpp
	@g++ -g -Wall -l ncurses -o Server Entry.cpp
clean:
	@rm -f Server


If anyone could explain where I am going wrong, and why this throws a seg fault, I'd be very gratefull for the assistance!
I doubt this is why you get the segfault, but you shouldn't use that #pragma. Use an include guard instead:
1
2
3
4
5
6
7
8
#ifndef _HEADERFILE_H
#define _HEADERFILE_H

/*
 * Code goes here
 */

#endif /* ! _HEADERFILE_H */ 


In "Entry.cpp" you don't delete the pointer you allocated. I also don't see the point in assigning the variable on the second line but whatever.

In fact, you don't delete any of your dynamic pointers. This could cause your segfault.
Last edited on
thanx chrisname, I had assumed the error was occuring before the end of execution, but I'll look into cleaning up some of the pointers, and see if this is in fact the problem. In the mean time, if anyone browsing through here happens to know of something else that could be causing this problem please get back to me, I'll be back later when I have done as chrisname suggests.
I changed the following...

Console.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "curses.h"
#include "Console.h"
Console::Console(void)
{
	Clear();
}
Console::~Console(void)
{
	Destroy();
}
void Console::Clear(void)
{
	OutputText=NULL;
	InputText=NULL;
	TerminalType=NULL;
	ConsoleTerminal=NULL;
	OriginalTerminal=NULL;
	ConsoleWindow=NULL;
	return;
}
bool Console::Create(void)
{
	OutputText=new FILE;
	InputText=new FILE;
	TerminalType=(char*)"linux";
	ConsoleTerminal=newterm(TerminalType, OutputText, InputText);
	OriginalTerminal=set_term(ConsoleTerminal);
	ConsoleWindow=initscr();
	return true;
}
void Console::Destroy(void)
{
	endwin();
	set_term(OriginalTerminal);
	delscreen(ConsoleTerminal);
	delete OutputText;
	delete InputText;
	delete TerminalType;
	delete ConsoleTerminal;
	delete OriginalTerminal;
	delete ConsoleWindow;
	return;
}

Entry.cpp
1
2
3
4
5
6
7
8
#include "Console.cpp"
int main()
{
	Console* output;
	output=new Console;
	output->Create();
	delete output;
}


The compiler throws a few stray warnings when I try to delete the SCREEN* pointers, and a note saying something along the lines that they won't be deleted.(lines 39 & 40 of Console.cpp)

I still get a seg fault.
Last edited on
Can you show me the warnings?
You should also be returning 0 at the end of main, and in in Console::Clear(), you need to free the memory and then re-allocate it rather than just assign NULL to those pointers. Are there any other functions there? If not, what are you doing with the FILE pointers?
In file included from Entry.cpp:1:
Console.cpp: In member function 'void Console::Destroy():
Console.cpp:39: warning: possible problem detected in invocation of delete operator:
Console.cpp:39: warning: invalid use of incomplete type 'struct screen'
/usr/include/curses.h:321: warning: forward declaration of 'struct screen'
Console.cpp:39: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.
Console.cpp:40: warning: possible problem detected in invocation of delete operator:
Console.cpp:40: warning: invalid use of incomplete type 'struct screen'
/usr/include/curses.h:321: warning: forward declaration of 'struct screen'
Console.cpp:40: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.

I added the return 0; to the main function.
As for freeing the memory and re-allocating, I'm not sure what you mean exactly, could you maybe supply me an example with just one of the pointers...
The FILE pointers are used by newterm on line 26 of Console.cpp, there is no more program - just the four files listed (3 source and 1 makefile).
Last edited on
Hmm... I should mention that you shouldn't be including source files. You #include "Console.cpp" which is incorrect -- you should be including Console.h. Change the inclusion and change your g++ command-line to g++ -g -Wall -l ncurses -o Server Entry.cpp Console.cpp

I'm not sure what's causing those warnings, but they and your segfault are likely linked. Fixing one will most probably fix the other.

As for freeing the memory and re-allocating, I'm not sure what you mean exactly, could you maybe supply me an example with just one of the pointers...

Sure
1
2
3
4
5
6
foo bar = new foo; /* Allocate */

/* ...  */

delete bar; /* Free */
bar = new foo; /* Re-allocate */
Topic archived. No new replies allowed.