hello, got a quick question regarding stack overflow (I honestly don't know if this is more suited for a wxWidgets forum, once you see what I mean, but I figured I'd try here first). I was tweaking my linked list class and testing it in a testing harness program I wrote for it (performance, making sure functions work, etc) when I now get stack overflow errors when I start the debugger (VS2008 PRO). Here's the message:

Unhandled exception at 0x772e0e5a in Tester.exe: 0xC00000FD: Stack overflow.

I traced it back to the constructor of the window frame:

bool MyApp::OnInit() //initializer
{
	MyFrame *mainFrame = new MyFrame(wxT("Test Harness"), wxSize(700, 600) ); //breakpoint 1
	mainFrame->Show(true);
	
	return true;
}

//...
MyFrame::MyFrame(const wxString& title, const wxSize& size) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, size)
{ 
	CreateStatusBar(); //breakpoint 2
	SetStatusText(wxT("Performance Data"));
	
	//panels and controls
}
//...

//malloc.c

//...
#ifdef _WIN64
    return HeapAlloc(_crtheap, 0, size ? size : 1);
#else  /* _WIN64 */
    if (__active_heap == __SYSTEM_HEAP) {
        return HeapAlloc(_crtheap, 0, size ? size : 1); //VS said this line was the problem
    } else
    if ( __active_heap == __V6_HEAP ) {
        if (pvReturn = V6_HeapAlloc(size)) {
            return pvReturn;
        }
    }
//...

I would start at breakpoint 1 but never hit breakpoint 2, which means it must be failing in the constructor (memory allocation from the inherited constructor).

It started failing after I added another operator function of my linked list (++ so I wouldn't have to keep saying 'iterator = iterator->NextCell()' ), even though I thought it was odd I removed the operator and reverted all the uses of it back to the original. Surprise, surprise that I'm still getting the stack overflow issue.

Things I've tried:
1. Restarting VS
2. Restarting my computer

I made sure I wasn't out of memory, I have 3 gigs and my performance monitor said I was using just under 2, so unless my program needed over a gig to build a relatively simple gui, I think it's something else. You guys can advise if this is the wrong forum, but any suggestions would be appreciated.

~J

Check if heapalloc is failing.

I believe what ithlep means for you to do is

MyFrame *mainFrame = new MyFrame(wxT("Test Harness"), wxSize(700, 600) ); 
if(mainFrame)
  mainFrame->Show(true);
else
  std::cout << "There was an error creating mainFrame!" << std::endl;

thanks for the replies, but it fails before the Show() method, it's in the constructor. And I did isolate the problem further so now the issue has changed scope a little bit. I had forgotten I had done this and it's causing the problem but I don't know why. Here's the issue:

MyFrame has a member of type Row<NodeValue> (Row<T> is my linked list class which has individual cells of type Cell<T>) so in the constructor, it's making that Row and there's where the problem starts.

class MyFrame : public wxFrame
{
	public:
		MyFrame(const wxString& title, const wxSize &size);
		~MyFrame();
		
		Row<NodeDisplay> nodeMap; //it makes this
		//other members
		
	private:
		DECLARE_EVENT_TABLE();
               //other private members
};

//so it goes here: row.h

template <class T> class Row
{
	private:
		unsigned int size;
		Cell<T> *firstCell, *lastCell, *npos; //npos is giving the issue, see constructor
		Row<T> *nextRow, *currentRow, *prevRow;

	public:
		Row();
		~Row();

              //other members
};

template <class T> Row<T>::Row()
{
	firstCell = NULL;
	lastCell = NULL;
	nextRow = NULL;
	prevRow = NULL;
	npos = new Cell<T>();  //this should work but throws the error
	currentRow = this;
	size = 0;
}

I know this method works because I've implemented it on a Map<T> class, so I'm curious why this is causing a problem, suggestions? oh, here's the Cell<T> stuff:

template <class T> class Cell
{
	private:
		Cell<T> *nextCell, *prevCell;

	public:
		T cellValue;

		Cell()	{ nextCell = NULL; prevCell = NULL; }
               //other members
};

Thanks!

~J

I feel pretty stupid, I found the problem and I know why it's not working in this case and why it does work for my other case (I also know a better alternative, so I guess it's ok). Here was the problem (in case anyone was wondering) and I'll try to explain it without having to show my rather extensive class structure. I have a class Node, one of it's members is 'Row<Node> children' (btw MyFrame also has a member of Row<Node> so this is why it gets called in the first place). So in the constructor of Row where it makes the npos and makes a new Cell<Node>, the Node constructor gets called, and makes a new Row<Node> which, in it's constructor makes a new npos, thus making a new Node, calling it's constructor, and down the infinitely recursive line we go.

I hate when my posts turn out like this, maybe I should extend my 3 hour rule before posting to 2 days ;)

Thanks to those who tried to help me, unfortunately it was unsolvable with the information I provided (incomplete), I tried to not make it so.

~J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.