stanleyheng

hi all

i wrote a small piece of self modifying code and it can work correctly in the debug build but not in the release build..the release build program crashed..

the code:

int i = 100;

unsigned char *p = (unsigned char*)0x4113D5;

*p = 0x65;

int j = 100;

int k = i + j;

cout << k;

i have included "/SECTION:.text,ERW" in "Project --> Properties --> Configuration Properties --> Linker --> Command Line" for both the debug and release build

Thanks

Stanley



Re: Visual C++ Language code can't work in release build

Bruno van Dooren

This is bad code and I am not surprised it crashes.

You blindly assume that you can predict where your compiled code is located in memory.

Now this MAY be true for debug builds because the compiler translates your source code to machine quite literally, but in realease mode the optimizer kicks in and your machine code will look very different from what it looked like.

There is no way for this to work like you created it. Even if you somehow found the correct address that is valid for release builds, it could be different between different builds because the optimizer is not guaranteed to do the same things if your source code changes.

To do this correctly your app would have to parse its own pdb file to find out where functions are located, and even then I don't know if you can locate the correct instructions to overwrite because you cannot know in advance how the optimizer will molest your code. The optimizer can and will do very radical things.





Re: Visual C++ Language code can't work in release build

stanleyheng

may I know where I can read more about this optimizer



Re: Visual C++ Language code can't work in release build

MamthaR

Hi All,

With related to this post, May I know the detailed Difference between the "DEBUG" and "RELEASE" build in VC++.

I tried to search in MSDN forums. No Luck...!!!!!!! Would really appreciate any useful weblinks/references.

Thanks in advance.





Re: Visual C++ Language code can't work in release build

Bruno van Dooren

The only difference between release builds and debug builds is that for he release builds the runtime checks are disabled whil optimizations are enabled. As far as the linker and compiler are concerned there is no difference. in fact, debug and release are only IDE concepts.

For example, in release mode the default optimization setting is /O2: optimize for speed whereas for debug builds this option is set to /O0 or whatever it is called.

If you use google to search for the text you put in bold, you will get a lot of hits, including these 2:

http://www.codeguru.com/forum/archive/index.php/t-269905.html

http://www.codeproject.com/debug/survivereleasever.asp





Re: Visual C++ Language code can't work in release build

Bruno van Dooren

Ayman Shoukry of the VC++ team did a great talk about the optimizer at tech-ed in barcelona, 2006.

I have blogged about it here:

http://msmvps.com/blogs/vandooren/archive/2006/11/10/Tech_2D00_Ed-developers-Barcelona_3A00_-Friday.aspx

I don't know if the presentation itself is up for download.