EntBlog
Code, 3D, Games, Linux and much more...
Beware of what you get when locking DirectX Resources Buffers
January 30, 2006 @ 17:09 | In Programming | 5 Comments |
I’m writing this because is a topic that is hard to find in the net. This is my contribution to the google database.
Due to a Windows Kernel bug, sometimes the pointers returned by the Lock functions of DirectX (Index Buffers, Vertex Buffers, etc) are invalid. Citing the DirectX 9.0 SDK:
A limitation of the Windows2000 Kernel can result in some resources being freed while the resource is locked and being accessed by the application
This results in the app writing to freed memory, causing an exception
The problem only occurs with D3DPOOL_DEFAULT resources that the display driver chooses to place in system memory. When the device is put into a lost state (due to a mode change or ALT+TAB), all D3DPOOL_DEFAULT resources are freed. Direct3D has code to alias video memory resource pointers and redirect them to a dummy page, but D3DPOOL_DEFAULT system memory resources are not protected. The most common D3DPOOL_DEFAULT resources that get placed in system memory are Index Buffers, but some drivers may choose to place other resources in system memory as well. Apps can protect themselves by wrapping all resource accesses inside try…catch blocks
I’ve seen this problem in Vertex and Index buffers (always in the default pool) not only when doing ALT + TAB. And remember that WindowsXP is a Windows2000 kernel too.
You have four options to take (from worst to better) in this scenario:
- Do nothing. Your application will crash sometimes. You will say to your client that it is a Microsoft bug but the client won’t trust you.
- Use the IsBadWritePtr function against the pointers returned by the DirectX API. This won’t save you the scenario in which the pointer is invalidated after testing with IsBadWritePtr (for example, when you are writing to the buffer)
- Wrap with a try…catch block the code between Lock and Unlock. Your code have to be prepared to be exception safe. And if your code is not prepared, you are in trouble. At least you will get a lot of memory leaks. In some architectures this can be a serious problem. You should fix a lot of code and be prepared to fix future code. Even more, try…catch(…) blocks are evil. You shouldn’t be using them. You may be hiding exceptions not related with the Lock/Unlock problem. I will write about this in the future.
- Use the IsBadWritePtr function against the pointers returned by the DirectX API and if you get a bad pointer return a pointer to a dummy memory block internally allocated by you. To me, this is the cleanest and safest option. You should return a memory block long enough to hold the size requested by the Lock.
One last thing, IsBadWritePtr is not implemented doing some magic kernel operations. It checks the memory trying to write to it and catching the exceptions if it fails.
We will have to wait for Windows Vista to forget this horror.
delicious!
January 27, 2006 @ 0:57 | In Internet | 3 Comments |
I discovered del.icio.us months ago and now I can’t live without it. It allows you to store your bookmarks online, so you can access them from everywhere (work, home, travelling…)
Even you have a plugin for Firefox, so you can use del.icio.us as a traditional bookmark.
The idea is simple, but very powerful.
VisualStudio 2005
January 18, 2006 @ 1:18 | In Programming | 3 Comments |

VisualStudio 2005 is finally here (yes, I know, months ago). If you are a native C++ programmer (nonmanaged) you may have found hard to find the news incorporated in this version (bloated by the managed improvements). Here, the ones I have found:
- Live source browsing (call graph, callers graph and more, integrated with Visual C++ IntelliSense
- Property Manager. Finally you can share settings between multiple projects
- Profiled-Guided Optimization. An improved Whole Program Optimization allowing thinks like Virtual Call Speculation.
- 64 Bits Support. Now, you can target this platform (Intel EM64T & Itanium and AMD 64)
- Improvements in STL like Debug Iterator Support and Checked Iterators (similar to the ones you have had since ages in STLport)
- Multithreading: OpenMP standard comes to Visual C++. A specification for multithreaded programs using #pragmas
- New Floating Point Model (2003 model is deprecated). Now you have fp:precise, fp:fast and fp:strict. fp:precise is the default
- SafeCRT. Memory and String standard functions get a warning when used. You are recommended to use a new group of functions that are safer. Possibly you will want to disable this warning
- Build multiple projects concurrently (those that are independent) on computers that have more than one CPU. Those are the numbers (1> 2> etc) that you are seeing in the output window
- VCBUILD. A command line tool to build your project for your night builds (concurrency is still supported in this tool)
- CustomBuildRules. You can associate extensions with build tools and register additional options to appear in the property pages. C++ builder is integrated as default using this system
- Switch /Oa to eliminate aliasing have been eliminated. Now the keyword __restrict is used in a similar way to the C99 restrict keyword. If you don’t know what pointer aliasing is, please make you a favor and read this excellent presentation
- Solution Explorer allows you to view files in the disk (Physical view) . You can rename files in the project without having to delete from project, rename and add to project.
- There is a wizard that allows you to create a new project from existing code files (adding all of them automatically to the project)
C++ Templates are Turing Complete
January 13, 2006 @ 11:25 | In Programming | No Comments |
Reading one of the latest books of Scott Meyers I discovered that C++ templates have been proved to be Turing Complete. Turing Complete means that C++ templates can emulate a Turing Machine, so they can execute any algorithm (of course, in a theory world because for example Turing Machines have unlimited storage capacity). This means that you can metaprogam with templates anything you can code in C++. Obviously this doesn’t say anything about how to implement it or how easy it would be. In theory you could, for example, run a OS like Windows using a C++ compiler as a MetaOS.
I always found C++ Metaprogramming a little bit hacky. If you are interested, this is a paper demonstrating the Turing-completeness of C++ templates.
Advanced C++
January 12, 2006 @ 0:42 | In Books, Programming | No Comments |

Advanced C++, Programming Styles and Idioms
Author: James O. Coplien
Pages: 520
Published: December, 1994
I decided that I had to read this book when Scott Meyers in his More Effective C++ wrote : “.. but when you’ve read it, you’ll never look at C++ the same way again.”
The truth is that I was expecting a very interesting book but the beginning was a little bit disappointed to me. The first chapters of the book are not more that a mere review of topics that a professional c++ programmer would consider as basics (abstract data types, classes, constructor, destructors, inline functions, pointer to member functions, inheritance …). I think that most of this topics are better described in the books: Effective C++, More Effective C++, Exceptional C++ and More Exceptional C++.
I started to enjoy the book when reading chapter 5. There you can read about the Handle - Body Idiom, Virtual Constructors (with an interesting technique of constructing objects on top of one another), Functors…
Undoubtly, the chapter that justify buying this book is the one dedicated to something that Coplien calls Exemplars (something very similar to prorotypes). Exemplars are used to simulate dynamic types in C++ (like smalltalk). The concept itself is interesting but the examples given by the author are not very good (putting in a base class all the methods from classes inheriting is not a good programming technique). Using Exemplars, the author build the Symbolic Canonical Form to emulate the main advantages of languages like Smalltalk: garbage collector and dynamic types. The section dedicated to dynamic reloading of virtual functions is really interesting (obviously, the code used here is non-portable)
Rest of the book (chapter 11 and following) is less interesting: scheduling, threads, exception handling (this topic is outdated)
In conclusion, a book where you can find techniques you though that were impossible in C++. But most of those techniques will have a very little usage in your daily work as a C++ programmer.
Rating: 7 / 10
Geometry Wars
January 5, 2006 @ 1:33 | In Videogames | No Comments |
Xbox Live Arcade is one of the best things that Xbox360 has brought to us. Most of the games are incredibly addictive. Geometry Wars is one of the best games of Xbox360. Yes, a 5€ game competing with 60€ games. If you are addict to this game, you will enjoy this video: 4.191 million points!
Future of C++ (C++0x)
January 3, 2006 @ 19:11 | In Programming | No Comments |
Artima Developer Community offers a preview written by Bjarne Stroustrup about what we should be expecting from C++ in the next years:
- Template typedefs (but with a different syntax)
- Sequence constructors, allowing
vectorv = {1.0, 2.0, 3.0} - Concepts (not a detailed explanation of how this could be implemented but it is an idea that is clearly needed)
- Autotypes (
for ( auto p = v.begin(); p!=v.end(); ++p)). This will improve the quality of metaprogramming in C++. The benefit for libraries like spirit will be huge.
Fri, 25 Jul 2008 11:41:45 +0200 / 21 queries. 1.625 seconds / 3 Users Online
|
|
|
|
|
Theme modified from Pool theme. Valid XHTML and CSS
About
Categories