I recently started using the beta version of Visual C++ 11 and had several problems with it, such as when starting the debugger.
After using it for a few hours, it started getting sluggish, and then ridiculously slow: closing a file would take between 20 and 30 seconds or regular hangs for several seconds when saving or navigating a file. I found a post about a huge suo file in the project directory for Visual C++ 2008. I took a look just for fun:
Unsurprisingly, deleting it fixed the problem.
I have no idea why this happened nor how to prevent it from happening again.
Monday, April 23, 2012
Wednesday, February 1, 2012
Timezone woes
Nothing much today, just some rambling.
We have a client in Novokuznetsk, Russia and we were having issues with displaying the right time since Russia stopped using daylight saving time. There's a WAMP server running over there with php 5.3.6 that has an old tz database. Upgrading to 5.3.9 fixed the issue, although there doesn't seem to be VC6 binaries compiled for the moment and there's a clear warning about not using VC9 version with apache.org binaries. The VC9 x86 thread safe binaries seem to work fine.
However, we were a bit uncertain about the actual time in Novokuznetsk (although we later confirmed it is +7.) Just for fun, I started looking around, taking the first few sites when searching for "timezone" on google:
I was surprised to see google getting it wrong.
We have a client in Novokuznetsk, Russia and we were having issues with displaying the right time since Russia stopped using daylight saving time. There's a WAMP server running over there with php 5.3.6 that has an old tz database. Upgrading to 5.3.9 fixed the issue, although there doesn't seem to be VC6 binaries compiled for the moment and there's a clear warning about not using VC9 version with apache.org binaries. The VC9 x86 thread safe binaries seem to work fine.
However, we were a bit uncertain about the actual time in Novokuznetsk (although we later confirmed it is +7.) Just for fun, I started looking around, taking the first few sites when searching for "timezone" on google:
| Source | Time | Timezone |
| php 5.3.6 | 14:00 | +6 |
| php 5.3.9 | 15:00 | +7 |
| timeanddate.com | 15:00 | +7 |
| worldtimezone.com | 15:00 | +7 |
| 16:00 | +8 |
I was surprised to see google getting it wrong.
Wednesday, December 7, 2011
Visual C++ 2010 std::istringstream crash in destructor
tl;dr: You have a /vd2 switch in your project settings that you don't know about. Check your property sheets, gtkmm in particular has it in the additional options. [edit 11-dec-2011: but read the update at the bottom, it's not that simple]
I have a Visual C++ 2008 project that uses gtkmm, sqlite and curl that I wanted to port 2010. Most of the times, upgrading a project to a newer version of Visual C++ works quite well, but I got a crash immediately after the first build on a destructor for a std::istringstream. std::ostringstream also crashes in the destructor. The error message is
After multiple rebuilds, checking dll dependencies and generally not having much of a clue, I started looking at the exact command line for my project in the property pages, but still could not find anything useful.
I then started to look at the individual property sheets, starting with gtkmm, where I obviously found this little /vd2 switch in the additional options of the command line. Funnily enough, it looks like these additional options are not shown on the "all options" text box of the project itself. Removing the switch fixed the crash.
Now, fixing a crash is always nice, but finding the reason for the crash is better, so it can be avoided in the future.
This gtkmm bug report dated from November 2004 states that Visual Studio 2003 had a problem with using dynamic_cast in constructors. The fix on Visual Studio 2005 was to use the /vd2 flag. Moving on to 2010, /vd2 broke for unknown reasons, but this is not fixable without breaking binary compatibility, which is not happening until the next major release. I haven't tested whether the /vd2 flag is still necessary (two bug reports seem to indicate it still is, as no resolution has been posted), but preliminary tests seem to be okay.
Therefore, it looks like the /vd flag was extended as a bugfix for 2005, but 2010 introduced a bug on the bugfix. Hopefully, the 2005 bug is gone or we'll need a bugfix for the bugfix. Who said programming wasn't fun?
There's not much internets on this, although there are two threads on gnome's mailing list and a bug report on googletest (where a guy closed it quite unceremoniously "as this is a compiler bug") that appear when searching specifically for "gtkmm" and "vd2".
[edit 11-dec-2011:
I just got linked in a stackoverflow question. I was wondering where that traffic came from.
My "preliminary tests" was successfully running a somewhat complex gtkmm application without /vd2. It was obviously not enough. This (adapted from the OP's code):
fails with Visual C++ 2010, but not with g++ 4.5.3. Adding /vd2 to the compiler settings fixes the problem, but then changing main() to
again fails in istringstream::~istringstream().
What happens in base::base() is legal as per 12.7/5 in C++03 and 12.7/6 in C++11:
As far as I can tell, there is no way to fix this so that both dynamic_cast in a constructor and stringstreams can work. I don't even know the extent of the problem with /vd2 in Visual C++ 2010 (why only stringstreams?). Documentation on the /vd2 switch for 2010 says that
[edit 28-jun-2012:
Although the dynamic_cast still fails miserably without /vd2, stringstreams now behave correctly on Visual C++ 2012 RC]
I have a Visual C++ 2008 project that uses gtkmm, sqlite and curl that I wanted to port 2010. Most of the times, upgrading a project to a newer version of Visual C++ works quite well, but I got a crash immediately after the first build on a destructor for a std::istringstream. std::ostringstream also crashes in the destructor. The error message is
Unhandled exception at 0xwherever (ntdll.dll) in program.exe: 0xC0000005:right in
Access violation reading location 0x00000004.
virtual ~basic_istringstream()
{ // destroy the object
}
with this stack trace (edited for sanity)ntdll.dll!_NtRaiseException@12() + 0x12 bytesI also saw
ntdll.dll!_NtRaiseException@12() + 0x12 bytes
msvcr100d.dll!_free() + 0x10 bytes
program.exe!std::istringstream::~istringstream() Line 469 + 0x20
program.exe!std::istringstream::`vbase destructor'() + 0x2e bytes
ntdll.dll!_NtRaiseException@12() + 0x12 bytesand
ntdll.dll!_NtRaiseException@12() + 0x12 bytes
msvcr100d.dll!__unlock() + 0x16 bytes
ntdll.dll!_NtRaiseException@12() + 0x12 bytesThe first thing I found was a bug report on Microsoft Connect that was about the /vd2 switch, which I'd never heard of before and therefore do not use in my projects. I dismissed it quickly and moved on to not finding anything else.
ntdll.dll!_NtRaiseException@12() + 0x12 bytes
msvcr100d.dll!__free_dbg() + 0x68 bytes
msvcp100d.dll!std::_DebugHeapDelete<_RTL_CRITICAL_SECTION>() + 0x15 bytes
After multiple rebuilds, checking dll dependencies and generally not having much of a clue, I started looking at the exact command line for my project in the property pages, but still could not find anything useful.
I then started to look at the individual property sheets, starting with gtkmm, where I obviously found this little /vd2 switch in the additional options of the command line. Funnily enough, it looks like these additional options are not shown on the "all options" text box of the project itself. Removing the switch fixed the crash.
Now, fixing a crash is always nice, but finding the reason for the crash is better, so it can be avoided in the future.
This gtkmm bug report dated from November 2004 states that Visual Studio 2003 had a problem with using dynamic_cast in constructors. The fix on Visual Studio 2005 was to use the /vd2 flag. Moving on to 2010, /vd2 broke for unknown reasons, but this is not fixable without breaking binary compatibility, which is not happening until the next major release. I haven't tested whether the /vd2 flag is still necessary (two bug reports seem to indicate it still is, as no resolution has been posted), but preliminary tests seem to be okay.
Therefore, it looks like the /vd flag was extended as a bugfix for 2005, but 2010 introduced a bug on the bugfix. Hopefully, the 2005 bug is gone or we'll need a bugfix for the bugfix. Who said programming wasn't fun?
There's not much internets on this, although there are two threads on gnome's mailing list and a bug report on googletest (where a guy closed it quite unceremoniously "as this is a compiler bug") that appear when searching specifically for "gtkmm" and "vd2".
[edit 11-dec-2011:
I just got linked in a stackoverflow question. I was wondering where that traffic came from.
My "preliminary tests" was successfully running a somewhat complex gtkmm application without /vd2. It was obviously not enough. This (adapted from the OP's code):
#include <cassert>
#include <sstream>
struct object
{
virtual ~object() {}
};
struct base : virtual object
{
base()
{
object* b = static_cast<object*>(this);
base* d = dynamic_cast<base*>(b);
assert(this == d);
}
};
struct derived : base
{
int i;
};
int main()
{
derived d;
}
fails with Visual C++ 2010, but not with g++ 4.5.3. Adding /vd2 to the compiler settings fixes the problem, but then changing main() to
int main()
{
derived d;
std::istringstream iss;
}
again fails in istringstream::~istringstream().
What happens in base::base() is legal as per 12.7/5 in C++03 and 12.7/6 in C++11:
When a dynamic_cast is used in a constructor [..] if the operand of the dynamic_cast refers to the object under construction or destruction, this object is considered to be a most derived object that has the type of the constructor or destructor’s class.Therefore, as long as T refers to a direct or indirect base type of U, U::U() can dynamic_cast this to T and back.
As far as I can tell, there is no way to fix this so that both dynamic_cast in a constructor and stringstreams can work. I don't even know the extent of the problem with /vd2 in Visual C++ 2010 (why only stringstreams?). Documentation on the /vd2 switch for 2010 says that
/vd supports incorrect behavior in an early version of Visual C++, and is no longer needed.but that is not what I and others see.]
[edit 28-jun-2012:
Although the dynamic_cast still fails miserably without /vd2, stringstreams now behave correctly on Visual C++ 2012 RC]
Saturday, July 9, 2011
Internet explorer 9 always in quirks mode for local files
While working on canui, I was trying to check some examples with internet explorer 9. These examples were on my local apache server. After getting some errors about the canvas object, I opened up the developer tools (in the sprocket icon) and noticed the "document mode" was in "quirks mode."
I first realized that I hadn't included a doctype declaration. The HTML5 doctype is simply
However, even with the doctype, the page was still staying in quirks mode. Pages like the fish test were working fine. When I noticed that my examples were working fine from sourceforge, my google searches changed from "always in quirks mode" to "quirks mode localhost" and I quickly found a KB article about compatibility view.
After feeling like an idiot for a few seconds about not finding the "compatibility view settings" item in the sprocket menu, I stopped looking at the images and started reading the actual text and understood that the Tools menu is not the sprocket menu. Pressing alt brings it up. I blame the lack of coffee at that time.
In the compatibility view settings, there is a checkbox "Display intranet sites in compatibility view" that is checked by default. Unchecking it fixes problem.
Therefore, make sure:
I first realized that I hadn't included a doctype declaration. The HTML5 doctype is simply
<!DOCTYPE html>
However, even with the doctype, the page was still staying in quirks mode. Pages like the fish test were working fine. When I noticed that my examples were working fine from sourceforge, my google searches changed from "always in quirks mode" to "quirks mode localhost" and I quickly found a KB article about compatibility view.
After feeling like an idiot for a few seconds about not finding the "compatibility view settings" item in the sprocket menu, I stopped looking at the images and started reading the actual text and understood that the Tools menu is not the sprocket menu. Pressing alt brings it up. I blame the lack of coffee at that time.
In the compatibility view settings, there is a checkbox "Display intranet sites in compatibility view" that is checked by default. Unchecking it fixes problem.
Therefore, make sure:
- the html page starts with
<!DOCTYPE html>but without anything before it, including a BOM mark; - the compatility view settings (in the Tools menu) for local files is unchecked;
- you're not forcing any specific document mode in the developers tools;
- you're not using any kind of X-UA-Compatible meta tags;
- the windows group policy isn't forcing compatibility mode.
Monday, July 4, 2011
Canvas user interface
I am currently developing a user interface library for the HTML5 canvas element. I named it canui and it is hosted on sourceforge.
Wednesday, May 25, 2011
Inheritance and virtual functions in javascript
Javascript has no built-in facilities for inheritance or virtual functions. Inheritance lets you define common functionality in a base class that can be extended or modified in a derived class. A virtual function is a function that can be redefined in a derived class and will correctly be called on an instance of that class. Although crude virtual functions can be done by assigning a different function to the object, calling the base class implementation becomes harder.
I've come up (although it might already be popular, I'm not entirely sure) with a scheme to create hierarchies with virtual function tables. Although it requires more typing and some discipline, it emulates inheritance and virtual functions correctly.
A class that is meant to be derived from is defined as a regular function, taking the derived class's this object as a parameter.
The "virtual function table" is defined at the end of inherit_base. The derived class can call the base's implementation while adding functionality.
If the base class can either be derived from or instantiated (that is, it is not what's commonly called abstract),
then a base class can be defined and derived from inherit_base:
This requires discipline: functions in the base class need to have a distinct name (here I use the base__ prefix) and be assigned to the "virtual" function's name afterwards. As with most languages, the base class implementation needs to be called manually from the derived class.
I've come up (although it might already be popular, I'm not entirely sure) with a scheme to create hierarchies with virtual function tables. Although it requires more typing and some discipline, it emulates inheritance and virtual functions correctly.
A class that is meant to be derived from is defined as a regular function, taking the derived class's this object as a parameter.
function inherit_base(self)
{
self.member = 1;
self.base__frob = function()
{
alert("frobbing base");
}
self.frob = self.base__frob;
}
function derived()
{
inherit_base(this);
this.frob = function()
{
this.base__frob();
alert("frobbing derived");
}
}
var d = new derived();
d.frob();
The "virtual function table" is defined at the end of inherit_base. The derived class can call the base's implementation while adding functionality.
If the base class can either be derived from or instantiated (that is, it is not what's commonly called abstract),
then a base class can be defined and derived from inherit_base:
function base()
{
inherit_base(this);
}
This requires discipline: functions in the base class need to have a distinct name (here I use the base__ prefix) and be assigned to the "virtual" function's name afterwards. As with most languages, the base class implementation needs to be called manually from the derived class.
Wednesday, March 30, 2011
Code::Blocks, Visual C++ and precompiled headers
C::B supports precompiled headers that are created like gcc does: compile the header into a .gch and gcc will find it if it's in the same directory as the header. Visual C++'s precompiled headers require distinct flags for creating and using the precompiled header.
Because C::B doesn't support per-file configuration, giving different options to files is impossible, unless you use the "custom build" option in the properties.
So, here's how you can use precompiled headers with Visual C++ 2005/2008. I'm assuming the names pch.cpp and pch.h.
This also relies on cl.exe being able to choose between /Yu (set in the build options, included through $options in the custom command) and /Yc (set in the custom command) when both are specified, which is the case while compiling pch.cpp. This seems to work fine on both 2005 and 2008.
[edit 31-mar-2011: Ugh, there's more problems than I thought with this. The above tricks the build system into thinking that pch.obj is a regular object file. This way, it is generated and cleaned correctly. However, it is not an object file, it is a precompiled header. That's why you need to uncheck "Link file".
I realized that a pch.obj was still being generated in the root directory. This is because /Fo is not specified in the custom command. That pch.obj is the real object file generated by pch.cpp.
I now understand that Visual C++ generates two files when compiling pch.cpp: a precompiled header (pch.pch) and an object file (pch.obj). The former is used in each subsequent file using /Yu and the latter is linked at the end. This allows adding definitions in pch.cpp.
The scheme above does not link with pch.cpp because its object file is the precompiled header.
There are two possibilities here:
I could probably make this work if C::B had a place to specify additional files to clean, but there's not.]
[edit 14-sep-2011: typos]
Because C::B doesn't support per-file configuration, giving different options to files is impossible, unless you use the "custom build" option in the properties.
So, here's how you can use precompiled headers with Visual C++ 2005/2008. I'm assuming the names pch.cpp and pch.h.
- Add pch.cpp and pch.h to the project.
- Go in the project's build options, "Compiler settings" tab, "Other options" section, and add:
/Yupch.h /Fp$objects_output_dir\pch.obj
Careful: if pch.cpp is not in the same directory as the project file, you'll need to specify it manually. I usually have the project file in / and my sources in /src. Therefore, I use:/Fp$objects_output_dir\src\pch.obj
This is because C::B creates a directory structure in $objects_out_dir that's similar to the one for source files. - Right-click on pch.cpp, select "Properties".
- In the "Build" tab, uncheck "Link file" and move the priority slider to 0. This makes sure the pch is compiled first.
- In the "Advanced" tab, check "Use custom command to build this file" and enter:
$compiler /nologo $options $includes /c $file /Ycpch.h
This also relies on cl.exe being able to choose between /Yu (set in the build options, included through $options in the custom command) and /Yc (set in the custom command) when both are specified, which is the case while compiling pch.cpp. This seems to work fine on both 2005 and 2008.
[edit 31-mar-2011: Ugh, there's more problems than I thought with this. The above tricks the build system into thinking that pch.obj is a regular object file. This way, it is generated and cleaned correctly. However, it is not an object file, it is a precompiled header. That's why you need to uncheck "Link file".
I realized that a pch.obj was still being generated in the root directory. This is because /Fo is not specified in the custom command. That pch.obj is the real object file generated by pch.cpp.
I now understand that Visual C++ generates two files when compiling pch.cpp: a precompiled header (pch.pch) and an object file (pch.obj). The former is used in each subsequent file using /Yu and the latter is linked at the end. This allows adding definitions in pch.cpp.
The scheme above does not link with pch.cpp because its object file is the precompiled header.
There are two possibilities here:
- Getting rid of the pch.obj in the root directory: This would be fine if you didn't need to link with pch.cpp at all. I can't find any way to do this. cl.exe has no way of saying: use this file to generate a precompiled header, but no object file. The /Yc flag is meant to generate the precompiled header in addition to the regular object file.
You can however add a post-build step "cmd /c del pch.obj" which will delete the stray pch.obj from disk. This is something of a hack, but it works. - Generate both the precompiled header and object file correctly and link with the object file: This is the behavior of Visual C++. To do this, the precompiled header needs to use a different extension, such as .pch, so it doesn't clash with the object file:
- Change /Fp in the project build options to generate "pch.pch" instead of "pch.obj".
- Recheck "Link file" in pch.cpp's properties and add "/Fo$object" back to the custom command.
This way, you'll end up with both pch.pch and pch.obj in the output directory and pch.obj will be linked correctly. This is the typical Visual C++ behavior. However, you now have a file pch.pch of which C::B knows nothing about, because it was generated behind its back with /Yc. Therefore, cleaning the project will not delete that file.
I could probably make this work if C::B had a place to specify additional files to clean, but there's not.]
[edit 14-sep-2011: typos]
Subscribe to:
Posts (Atom)

