# [ C++ ] Declaring an ifstream corrupts unrelated list<object>

This topic is 2779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi gamedev.net, my name is james and I usually only come here in moments of dire need, as I like to think of myself being perfectly capable of solving most programming problems on my own with a little google-fu and common sense. However, for the past two weeks, the project I've been tirelessly working on for the past five months has been put on hold because of a mind-boggling problem that has completely shut down my program and eluded any of my attempted fixes.

The problem takes place when the program calls a login validation function that has been working perfectly fine for about three months:
int validateLogin(string inputName, string inputPass, list<USER> &userList)

Now I know that people are going to tell me that if my function has been working for three months and only recently started malfunctioning without any changes, then it must mean that the problem lies elsewhere in my program. I know that's probably the case, and I don't expect you guys to be able to fix my problem just by looking at this one function itself. I came here because I'm completely at a loss for what to even look for. My program is a shredded, bloody mess - I've ripped out and reverted nearly two months of progress trying to find anything that could have possibly been causing the crash, to no avail. I'm at my wits end, and all enthusiasm for this project is dwindling. My last hope is that someone reading this post will give me some sort of hint as to what other parts of my program I can eradicate to find the source of the problem.

Anyway - the problem. The purpose of this validateLogin function is to check a text file where all the account information is held, and match it up with the inputName and inputPass arguments provided from the user, and then, if the name and password match up with a user described in the text file, the function will create a USER object with that information and add it to the userList. The userList (an std::list<USER> container) is passed by reference to this function so other classes are able to access it after it adds the new USER object. That's where the problem comes in. Whenever the validateLogin function declares an ifstream to read from the file, the list<USER> userList becomes unaccessable from any other part of the program without causing a crash.

I know the ifstream is causing the problem because I've tested the function with EVERYTHING commented out except for this line:
int validateLogin(string inputName, string inputPass, list<USER> &userList) { ifstream fromFile; };

When trying to cout the userList.size() after calling this function, the program still crashes, a windows box popping up to alert me that the program needs to close.

Before I came here I tried making a new piece of code just to test lists and ifstreams working together, to make sure I hadn't missed anything obvious, and to see if a simplified replication of my original code would work without any crashing:
#include <iostream> #include <list> #include <fstream> using namespace std; class Foo { public: int number; Foo() { number = 1; }; }; class Derp { public: int number; Derp() { number = 2; }; int performActionsWithList(list<Foo> &fooList) { ifstream fromFile("db_accounts.txt", ifstream::in); fromFile.close(); return 1; }; void passListHere(list<Foo> &fooList) { int hello = performActionsWithList(fooList); cout << fooList.size() << endl; }; }; int main() { Foo fooOne; Foo fooTwo; Derp derp; list<Foo> fooList; fooList.push_back(fooOne); fooList.push_back(fooTwo); derp.passListHere(fooList); return 0; };
It works fine, outputting the number 2.

I know this isn't very much information about my code itself, so it will probably be difficult to give me any good advice on what to do next. I'm hoping that someone just has some kind of general knowledge about ifstreams and lists that could point me in the right direction. ie: have you experienced ifstreams crashing a program before? Could there be something wrong with the text file? Is the std::list the problem? Could the problem be something completely unrelated to both ifstreams and lists?

Thanks for reading my long post - any help is appreciated; even throwing out a little comment could potentially give me an idea to help solve the problem.

-james

##### Share on other sites
I think the problem is elsewhere. Strange problems can happen when you have a memory corruption caused by other parts of the program. All I can recommend is to use a debugger and see if you can find something that doesn't look right.

##### Share on other sites
There are tools that generate reports for erroneous memory use. One I've used recently is Valgrind.

##### Share on other sites
Holy crap, thank you Wooh and alvaro for your comments! I looked into the subject of memory corruption, and read through the Valgrind website. It got me thinking - I went back to my program and combed through some of my older code with the search function, and it turns out, at some point I tried to incorrectly delete a stringstream object that was initialized early on in the program. Got rid of that line and everything is now working perfectly! I'm so glad I made this thread, even though I probably look pretty dumb right now.

even throwing out a little comment could potentially give me an idea to help solve the problem.
[/quote]

looks like I was right about that, at least

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 11
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633722
• Total Posts
3013540
×