Thursday, July 13, 2006

C# programs/source code

Found this page to be useful as it has links to useful C# projects that include source code. and Most of the projects have implementation of popular algorithms used in computational geometry as well as searching, etc.

void * __cdecl operator new(unsigned int) error during linking

Recently while trying to create a DLL in managed C++, i came across an error about : error "void * __cdecl >operator new(unsigned int)" (??2@YAPAXI@Z) being undefined!

This occured when I tried to create a dynamic unmanaged array in a managed class.

That is : within a gc class, I could not do the following int __nogc* buffer = new __nogc[100];

but in a non-gc class I could do it!

After much searching I found the resolution to be this:

Add msvcrt.lib to the input section of the linker property pages of your project settings. Thats all!

Monday, July 03, 2006

Memory corruption in Managed C++ .NET in Release Mode

Recently we were working with some .NET Managed C++ code, that would work in debug mode but not in Release mode. What was weirder was that on most machines, it would work in Release mode. Except when it got built by the build machine. Further probing made us realize that the biggest difference between the developer machines and the build machine was it had the Professional version of Visual Studio - which meant that it had a different set of optimizations enabled. Looking closer - the optimization we were using on the compiler was /O2 : Optimize for speed. which used a couple of underlying switches (/Og /Oi /Ot /Oy /Ob2 /Gs /GF /Gy). If we turned of Optimization the code would run properly and turning it out would give us garbage output. Further investigation lead us to pinpoint /Og switch as the culprit. /Og uses global optimizations. Looking at the documentation, I figured the issue is most probably because we were pinning some arrays and using both the original array and the pinned pointer to access the data. I believe the problem is most probably to do with aliasing, where the optimizer gets confused when aliasing is used in code. So our solution: We could have just disabled optimizations on the project. But we wanted to have access to the speed. So we left the optimization swith /O2 in. Instead what we did was use the pragma directive "#pragma optimize" to turn off the "Og" optimization only for those methods that used pinning and aliasing. And voila - the problem is all gone and the code still runs nice and fast.