Currently, I'm trying to use the following piece of code to open a file for reading and then closing it. The file name would be dof(in the first line). In the process, the file may contain commands that calls itself or others for reading, so a stack _dofileStack is created to store the previously opened file streams for later use.
From what I've been told, the following code seems to create memory leaks and some problems during recursion which results in a system freeze.
Truly need help debugging this... Many thanks in advance~
Since you haven't posted the declaration for Cmdparser or how you're using openDoFile and CloseDoFile, I can't comment on problems related to how you're using those two functions. I can only comment on what I see in those two functions.
If your stack size > 1024 and you call openDoFile again, you're going to get unexpected results. You'll create a new ifstream (L#3), then overwrite _dofile by getting the top of stack element (L#15) effectively losing the ifstream you created.
Your stack and your _dofile pointer are going to get out of sync if you try to recurse more that 1024 levels.
Not clear what the asset is for at L#22 since you don't explicitly set _dofile to NULL in the limited code you've posted.