Line 8 is what's crashing my program. Usually I would expect it to mean that either nextPlat or currPlat are NULL, but neither of them are. Both of them should be pointing to a valid Platform object, because otherwise line 7 would stop it, right?
what will happen, if you will pass platform contained in head ListNode?
A I can see, it will initialise curr with head, then will get next platform, and only then will test if Node contains needed data.
TL;DR: try to place routine on line 4 of find between lines 6 and 7.
Most likely find function doesn't find next platform. In that case function will terminate without returning anything. In that case random garbage value at the top of the stack will be used as return variable in line 2 of getNext function. Then you trying to dereference pointer to random memory address, which will cause segfault.
I thought the issue might be with the getNext function, so in order to try and find the issue, I wrote a chunk of code into main which calls getNext() on EVERY platform in my program, calling it using pointers, the same way I do in the board() function:
This worked fine and did NOT crash, indicating to me that getNext() is not the problem (unless there's something I'm missing). This is very confusing, because I feel like that's the most likely root of the issue.
Just to give you an update, I've narrowed down the problem some. It has to do with the find(Platform plat) function in the CTrainLine class. Before program starts, it's called on all platforms and works fine. But then during board() it's called on a2RedUptown and crashes (even though it should be returning a3RedUptown). Still not sure why this is happening.
Ok, I have looked into this and found out that next pointer from second platform became invalid.
It because you had passed CTrainLine into Train constructor by value. It will make a shallow copy of the class and could lead to all sort of errors. I changed class a little and now it will take CTrainLine by reference:
I haven't tried your solution yet, but based on some more experimentation I've been doing since last I posted, I think you're right. It definitely has something to do with a Platform pointer becoming invalid.
I've been working on this bug for the better part of 8 hours...
THANK YOU THANK YOU THANK YOU!!
And yes, if you wouldn't mind, please tell me step by step what's going on, because I really want to learn from this and avoid it happening again the future.
Your list class does not have a copy constructor or a copy assignment operator. It needs one if you're going to be passing it around by value, and if you don't intend for it to be passed around by value, disable copy initialization and copy assignment by making those methods private (or using the "=delete" syntax if your compiler supports it) so they can't be used accidentally.
You passing redLine by value and it get copied. *head pointer is copied too, but not the data it points to. Then you assign tLine to trainline in Train constructor, and pointer copy happens again. Now we have three instances of CTrainLine (redLine in main, tLine in currently running Train constructor and trainline in "pelham" Train instance) pointing to same data. When Train constructor finished, tLine goes out of scope, and its destructor called which destroys all nodes redline and trainline still pointing to. *head pointer in trainline is now invalid adn who knows what will happen, when we dereference it.
Either provide ypur own cope constructor and assigment operator, or forbid it and pass your objects by reference/pointer