Question

I am working on a project where I have source files I can edit (.cpp, .h) and object files that I cannot (.obj). I am trying to add a variable to one of the source file's classes. It compiles and links fine, but it causes a random crash somewhere in the application's life cycle.

My question is this: Is there any way I can add member variables to a class residing within a source file being linked to and used by OBJ files (without recompiling the OBJ files)?

I understand that it's entirely possible I am misunderstanding what the actual issue is, but this is my best guess as I've encountered it before and would appreciate answers in response to my question. That is of course unless you are absolutely sure my issue is something else.

Here is some background: I am working with another programmer's code. They have set up multiple singletons used everywhere in the code base. I am trying to decouple a particular group of systems from all the singletons using messages so that I can manage, modify, and build upon the systems more easily. Upon adding a variable to one of the singletons, it crashes the application in the same place every time (of which I cannot see the source code of). Depending upon what type of variable I add, that crash will happen at some different point in the application. I have considered that there is a buffer overflow somewhere in the code base, but I do not have the time to deal with that issue and am looking at other more concrete possible causes first.

Of course the "simple" solution would be to have the programmer who is dishing out the OBJ files recompile them with my new variable added (which they've had to do once before), but I am still working out the best way to decouple the systems, and the turn-around of doing and undoing that would be too time consuming due to the other programmer working an opposite schedule of mine.

I would have posted some source code or debug call stacks if they were relevant, but they aren't. The issue is exactly as stated. I added a variable to a class. It now crashes the program in some random OBJ file's routine.

Thanks for your time.

Was it helpful?

Solution

If I am reading your question correctly (you have two different versions of the same type in the final executable), what you ask for will lead to undefined behavior at runtime.

Say you have a struct named "foo", and it has two int members, and that is compiled into a separate object file by your fellow programmer. Then you take "foo", add an int member to it, and you compile it to another obj file. You now link the two obj files creating the executable.

You have two problems -- what is the sizeof(foo) during the running of the program? Is it equal to the smaller version or larger version of foo? Second, the linker more than likely reserved space for the smaller version -- what happens when the program attempts to access the int that you added to the larger version of foo? A memory "overread" or overwrite will occur.

You should coordinate your efforts with your fellow programmer(s). Do you use a source revision control system? If so, this is usually where these issues are ironed out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top