Memory change on function return

Started by
13 comments, last by fastcall22 12 years, 6 months ago
I have a very hard to find bug in one of my routines. An array of byte objects is becoming currupted when a member function (of the same class containing the array) exits. The array memory pointer does not change, but the values of the array indices become "garbled". It seems like I would need to be accessing out-of-bounds memory in order for this to happen. But it happens when a function ends. Within the last line of the function, the data is fine, but as soon as the code stream goes back to where it was called, it turns into a mess. Is there any information that may help me find the reason for this? Any clue related to the data scrambling when the function exits? Would the problem definitely be in the function? Or anywhere before the function was called? Unfortunately, this function calls several other functions within a loop, so even knowing it was happening in it's scope wouldn't help much. Here's what I'm meaning when I say it scrambles on function-exit:

VOID Object::FindBug()
{
  DoSomething();
  DoSomeOtherThing(); // breakpoint shows Object::Array is fine
  int useless_code_breakpoint = 1; // Object::Array is fine
}
.. another module ..
{
  Instance->FindBug(); // breakpoint, Instance->Array is fine
  int another_useless = 1; // Instance->Array is scrambled
}


If anyone can give me a hint, it would make my day.
Advertisement
Can you give more information?

What type is Object::Array? What is the definition of Object? What are the actual functions doing? What function is the array created in? What is the binary data in the array before and after it becomes corrupted?

You probably need to post your actual, real code, so we can help you.

From what you've told us, it sounds like your array is on the stack, rather than the heap (and is disappearing when it goes out of scope). But without more details I can't confirm it.

And I think I've told you before: std::vector instead of raw arrays. Please.
Quote:Original post by Andrew Russell
What type is Object::Array? What is the definition of Object? What are the actual functions doing? What function is the array created in? What is the binary data in the array before and after it becomes corrupted?

BYTE's. It's a world map class, but it's definition is a bit more complex than the problem. The array is created and filled with correct values in the object constructor. The array starts out with 0's and 1's, but becomes filled with garbage. In ascii values, it appears to be a repeating set of symbols: îþîþîþîþîþîþîþîþîþî.

Quote:You probably need to post your actual, real code, so we can help you.

Trust me when I say you wouldn't want to help me if I had to post the entire map implementation. I cannot narrow it down to any specific point, other than that function returning. The function routine doesn't mess with the array data. It doesn't even access it. It's unrelated, yet that's where it happens.

Quote:From what you've told us, it sounds like your array is on the stack, rather than the heap (and is disappearing when it goes out of scope). But without more details I can't confirm it.

The array is not initialized in the problem area, and the memory is definitely on the heap.

Quote:And I think I've told you before: std::vector instead of raw arrays. Please.

I'm not sure why you're requesting the use of vectors here. This is totally unrelated. The array is not the problem. It's just the victom.

I appreciate the try though.
It was a typo. Address-of symbol on an abject when sending it to be read from file. The object had it's own handler for file IO, but I prevented this by passing the address. Pretty dumb mistake. It ended up trying to load a large amount of data into a small 32 bits of memory.

Sorry for whining (my first ever GD bug post!). Thanks again.
Quote:Original post by Jiia
Quote:And I think I've told you before: std::vector instead of raw arrays. Please.

I'm not sure why you're requesting the use of vectors here. This is totally unrelated. The array is not the problem. It's just the victom.


Because std::vector is a known container, thus people know how it works and that it is indeed bug free. You might well assert that its not your code causing the problem, however we cant trust this fact without knowing the code and lets face it, the values are being stomped apon thus there must be a problem your code somewhere, be it with the array class or with something which access it is, but right now we are flying blind into a load of unknown code.

You can assert that your code is bug free as much as you like, the fact remains we cant trust your code as we dont know it.

Quote:Original post by _the_phantom_
You might well assert that its not your code causing the problem, ...

How in the world would I think that my code is not at fault? It's all my code. Nothing else could be at fault.

Quote: ..however we cant trust this fact without knowing the code and lets face it, the values are being stomped apon thus there must be a problem your code somewhere, be it with the array class or with something which access it is, but right now we are flying blind into a load of unknown code.

I'm the one flying. I was just looking for directions. I didn't expect someone to point out the problem. That's why I didn't throw you blindly into a load of unknown code. The details I provided wouldn't have helped anyone much to lead me to the problem. I just didn't know what else would be relevant. Now that I know the answer, I'm still not sure what code I could have posted that would have helped. Other than the exact typo line and the header line where that file IO handler is declared. But these things were totally unrelated to the symptoms. I apologize for wasting your time, but it was not done so carelessly. Still, I should have just stayed at the problem on my own instead of seeking help.

Quote:You can assert that your code is bug free as much as you like, the fact remains we cant trust your code as we dont know it.

I have no idea where you got the idea that I asserted such facts.
[looksaround] um. so what has been accomplished in this post?
Quote:Original post by skillfreak
[looksaround] um. so what has been accomplished in this post?

You're making fun of my post? [grin]

What is your reply accomplishing? [wink]
you caught me. +) ::andy tips his hat::
Quote:Original post by Jiia
Quote:Original post by Andrew Russell
What type is Object::Array? What is the definition of Object? What are the actual functions doing? What function is the array created in? What is the binary data in the array before and after it becomes corrupted?

BYTE's. It's a world map class, but it's definition is a bit more complex than the problem. The array is created and filled with correct values in the object constructor. The array starts out with 0's and 1's, but becomes filled with garbage. In ascii values, it appears to be a repeating set of symbols: îþîþîþîþîþîþîþîþîþî.

I asked for "binary" for a reason *grumble*.

Anyway... I decoded your "îþîþîþîþîþîþîþîþîþ" string. It becomes FEEEFEEE in hexadecimal. A little research tells us that "FEEEFEEE" is "Used by Microsoft's C++ compiler to mark the storage area of a deleted class in debug mode".

In other words, your array, or whatever your array was living in, was deleted.


Where it was deleted is something we'd need to see real code to tell you.

This topic is closed to new replies.

Advertisement