0

I have a relatively large class that I'm working with and it's all worked fine so far (note: I didn't actually write the class, I'm just adding some functionality). However, after declaring one more string in the header file, everything now crashes (I get a memory access error). If I erase that string and rebuild, everything works fine.

I'm not actually doing ANYTHING with that string....just the act of declaring it is causing some weird memory error.

I can't explain in much more detail than this, since it would be a waste to try to explain every function. What kind of things should I look for here to find the problem? What might cause this weird behavior?

The error itself is:
Unhandled exception at 0x65fd17fd (msvcp80d.dll) in myFile.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd.

Basically all that changed in the .h file was:

StringType string1;

Turned into:

StringType string1;
StringType string2;

StringType is an extension of basic_string

Casey Patton
  • 4,021
  • 9
  • 41
  • 54
  • Apparently the string shifts the memory alignment and reveals a bug that did not occur so far. I would look mainly for pointers that are not valid. The location seems somewhat suspicious to me maybe it is overwritten with a value you inserted? – Nobody moving away from SE Jul 20 '11 at 18:12
  • Did you clean and rebuild everything after the change? If you have old object files that are using the old definition then things will get hairy. – Martin York Jul 20 '11 at 18:16
  • 1
    "I have this class that does a bajillion things" -- That's your real problem. Your current error is just a side effect of bad design. Work on improving [Separation of Concern](http://en.wikipedia.org/wiki/Separation_of_concerns) – Benjamin Lindley Jul 20 '11 at 18:22
  • As an aside, when you've got some time on your hands you might consider refactoring your ["class that does a bajillion things"](http://en.wikipedia.org/wiki/God_object). – razlebe Jul 20 '11 at 18:23
  • a) By bajillion, I just meant it wasn't really worth going into all the specific functions. It's definitely not a God class. It has a specific purpose b) I didn't write it, I'm just adding to it – Casey Patton Jul 20 '11 at 18:23
  • Can't you at least post an extract of the class header? I'm curious to see the exact type of what you call "string". – paercebal Jul 20 '11 at 18:36
  • We're actually using an extension of the basic_string class that we wrote ourselves. So, it's an extension of basic_string. (I feel uncomfortable posting any actual code here because I'm working with proprietary stuff that my manager might not like me posting!). Literally all that changed was a new basic_string extension type was declared in the .h file. – Casey Patton Jul 20 '11 at 18:43
  • You could always to a sizeof(StringType), and then replace the second string by an array of ints or doubles (or even a simple struct with built-in members) whose sizeof is equal to the sizeof(StringType). If it still crashes, then somehow your class went larger than expected somewhere. If not, then your StringType could be the problem... Make sure your "struct/array" has the same alignment as your StringType, though... – paercebal Jul 20 '11 at 19:44
  • Solved: it looks like doing a rebuild all fixed the situation. – Casey Patton Jul 20 '11 at 20:08

1 Answers1

5

You've allocated some memory on the heap and failed to initialize it.

0xcd is a fill pattern used by the debug heap: before dynamically allocated memory is given to your program, it is filled with that pattern to help you find uninitialized variables.

As for why changing the class definition affects the outcome, you may be doing incorrect pointer arithmetic, accessing something beyond the end of a dynamically allocated object, or one of any number of other things that no longer manifests as a bug when you have a larger object. You could also be violating the one-definition rule if some of the source was built using the old definition and some of the source is built with the new definition.

There are many things that can cause this kind of problem: your best bet is to break in the debugger when it happens, and trace backwards to see where the error originated (sometimes this can be lots of fun; I had to trace an uninitialized variable across a network connection once).

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • I'm interested where you learned about these specific things about memory. Where could I go to get more information on this kind of thing? – Casey Patton Jul 20 '11 at 18:18
  • Amazon has [a good list](http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dstripbooks&field-keywords=pointers+on+c&x=0&y=0) – sehe Jul 20 '11 at 18:20
  • Which things? Dynamic allocation? How to use pointers? How to debug this sort of issue? The one-definition rule? – James McNellis Jul 20 '11 at 18:21
  • See this article: http://msdn.microsoft.com/en-us/library/bebs9zyz(v=vs.71).aspx – Chad Jul 20 '11 at 18:23
  • I was referring to how you learned what parts of memory handle which specific tasks. Looks like I've already received some good references! – Casey Patton Jul 20 '11 at 18:25
  • Selecting and voting up this answer. Helped me learn a lot, though the actual solution wound up just being: rebuild everything. – Casey Patton Jul 20 '11 at 20:10