At loss with a memory bug

Hey guys,

I'm currently writing a C++ program and I've come across some weird segmentation fault. With luck I found out that some variable of some class of mine suddenly changes into some weird values that I've never assigned to that specific variable. However, when trying to debug my progam with gdb, it seems impossible to find out why the variable is 'changed', although it seems more like some sort of access violation occurs. I've tried to use the tools valgrind and electric fence to find any such errors, but the only errors I find are those that eventually occur because the values of my variables seem to be assigned random values before that for some unknown reason. It also seems that

So here is what happends. I have this class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	class FormatStatement : public Statement {
	public:
		chi::SPtr<chi::StringBase> format;
		chi::SPtr<Statement> statement;

		FormatStatement() : Statement( StatementType_Format ) {}
		FormatStatement( const FormatStatement& copy ) : Statement( StatementType_Format ) {
			this->format = copy.format;
			this->statement = copy.statement;
			printf( "Copying FORMATSTATEMENT!!!! %p %d\n", format.ptr(), *format.count );
		}
		~FormatStatement() {
			printf("Destructing FORMATSTATEMENT!!! %p %d\n", format.ptr(), *format.count );
		}
	};
It also seems that
It also seems that what?

Firstly, what are the semantics of chi::SPtr? It would appear to be some kind of smart pointer class. Is it unique ownership or shared ownership?

Secondly, FormatStatement containing a pointer to an instance of a base class raises a red flag for me. Does your object graph contain any cycles? I don't know of any smart pointer implementations that are able to reclaim object graphs with cycles.
An example of an object graph with cycles is a directory tree structure where a folder points to the files it contains and a file points to the folder that contains it.
I don't know of any smart pointer implementations that are able to reclaim object graphs with cycles.
A few weeks ago @Cubbi pointed me to Herb Sutter's 2016 CPPCon presentation. The punchline was an (experimental) smart pointer which does automatic cleanup of arbitrary ownership graphs.
https://github.com/hsutter/gcpp
https://www.youtube.com/watch?v=JfmTagWcqoE
Last edited on
If you know which "some variable" is guilty, simply print out that variable every time it's updated and see where your code is at fault.
Topic archived. No new replies allowed.