Someone handed me a piece of code that works really well one time through but, if you call it a second time, it returns an offset from where it should point...I think.
My code calls his function which returns a pointer to a node structure. This node, and all its children, are allocated with malloc and I have a 'free function' to free everything when I'm done.
I've been calling his stuff just once and exiting with no problems but, today, I started calling it consecutively and it's as if the pointers to other nodes are correct at the beginning but making stuff up as it goes along.
What I found is his code allocates memory for the file we're reading, creates the nodes and pointers to that data, then returns the top node pointer to me.
As you can tell, the allocated nodes get free'd but the memory holding the file does not. This probably worked before because I exit the program at that point but, now, I have a new routine that calls it twice.
The file pointer is set up as you would expect, fp=malloc(size of file), so on the second call, the same fp gets re-allocated at the same point in his function on the second call.
The results give me three string, "cart", "order", "item". But the second call gives me "cart", "order", "cart". Which is why I think something's gotten offset somehow. The nodes themselves are still set up correctly, just the wrong content.
So my question is, would you guys agree that not freeing that file pointer is the source of the issue here? I know anything's possible since I'm not showing the code but does someone have some insight as to why that is? I'm throwing that out since I am just not in the mood to work on his code and it would be a pain to edit down a simple sample for you. I'd hate to start on it and have someone later tell me that's not the problem.