Destructor called early

I have a class which is initialized, then calls its destructor straight away for some reason. SaveDataWrapper is the class, it is a global variable.

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
SaveDataWrapper saveWrapper; // Relevant Line

void drawGraphWindow(PATHS_TYPE paths, vector<string>* pathsFileNames,
						Vertex* smallest, Vertex* largest);
void glutInitialization(Plugin* plugin);
void fillVBOs(Data* data);

int main()
{
	Vertex* smallest = new Vertex(DBL_MAX, DBL_MAX);
	Vertex* largest = new Vertex(DBL_MIN, DBL_MIN);
	PATHS_TYPE paths = readInput(*smallest, *largest);
	vector<string>* pathsFileNames = new vector<string>();
	pathsFileNames->push_back("hello.csv");
	pathsFileNames->push_back("goodbye.csv");

	drawGraphWindow(paths, pathsFileNames, smallest, largest);
	cout << "hello" << endl; // Never prints this before destructor
	return 0;
}

// Pass a subclass of plugin as plugin if you want to use its draw method
// otherwise pass NULL and the default draw method will be used
void drawGraphWindow(PATHS_TYPE paths, vector<string>* pathsFileNames,
						Vertex* smallest, Vertex* largest)
{
	Data* data = new Data(paths, pathsFileNames, smallest, 
					largest, windowWidth, windowHeight);

	saveWrapper = SaveDataWrapper(data); // Relevant Line
        // Destructor called before code gets here.
	GLDisplay::data = data;
	GlutUI::data = data;

	glClearColor(0.0, 0.0, 0.0, 0.0);	
	glutInitialization(new GLDisplay());
	glewInit();
	fillVBOs(data);
	glutMainLoop();	
}
Last edited on
Yes - there should be a destructor call (for the temporary SaveDataWrapper variable created by SaveDataWrapper(data) )
Last edited on
Thanks for the reply. Is there a way I can assign to saveWrapper without making a variable first, therefore not call the destructor (there). I thought saveWrapper(data); would work, but apparently not.
This ain't java. Delete those dynamic allocated objects or allocate them statically.
Why is a problem to call the destructor?
I do delete all my objects when I'm done with them. This isn't assigned with new, so it will kill itself when it goes out of global scope won't it? That was the idea anyway, so the destructor could save stuff when my app exits.
Topic archived. No new replies allowed.