www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Non-unittest code crashes when built in unittest mode, but not when in

reply "Nicolas F." <ddev fratti.ch> writes:
So I've been chasing after this issue for the better part of an 
hour, and as I dug deeper, it just got weirder and weirder.

I have an Interface, let's call it PartyAnimal, and three 
classes, one of which is exclusive to unittest.
class Equine is an abstract class that implements some of 
PartyAnimal, but not the part where the bug occurs. class Pony 
inherits from Equine, and has the code which triggers the crash. 
Method whinny() is defined in Equine (without implementation), 
implemented in Pony, but not defined or implemented in 
PartyAnimal.
class Cow, which also inherits from PartyAnimal, is solely for 
unittesting another class which has a list full of PartyAnimals, 
and it only exists in unittest mode with version(unittest) { 
/*definition goes here*/ }.

The unittests pass, everything is okay, and then the actual main 
code calls the problematic method of Pony. It throws a 
core.exception.InvalidMemoryOperationError error when creating a 
new dynamic array of a size bigger than 2046 bytes, or allocating 
memory (with GC.malloc) of a size bigger than 2048 bytes. But 
only when compiled in unittest mode. It works like a charm in 
debug and release mode. But it's code never touched by any 
unittest.

Platform is Windows 7, compiler is DMD32 D Compiler v2.064. I 
have tried to reproduce the problem in a test code, but was 
unable to. This is why I'm not submitting it as bug report right 
away: it's useless without test code.

However, maybe someone here has heard of the problem before, or 
has any pointers to what could be going wrong, or ideas on how to 
debug this any further.
Feb 11 2014
next sibling parent "Daniel Murphy" <yebbliesnospam gmail.com> writes:
"Nicolas F."  wrote in message news:fnamiajuljitahfgvfmw forum.dlang.org...

 So I've been chasing after this issue for the better part of an hour, and 
 as I dug deeper, it just got weirder and weirder.

I suggest downloading the latest beta and seeing if that solves your problem - the bug may have already been fixed! Otherwise, you may be able to use dustmite to reduce your program down to a test case: https://github.com/CyberShadow/DustMite/wiki
Feb 12 2014
prev sibling parent "Nicolas F." <ddev fratti.ch> writes:
I think I have found the issue.

The problem was a circlular reference in some class that managed 
the creation and destruction of PartyAnimals. Now I still don't 
know how that triggered the behaviour that I saw, but I do have a 
theory. When a certain size of memory is allocated, the GC may 
have said that it's time to clean up. If there was an instance 
created prior to the actual code being run, e.g. through a 
unittest, it would get a problem while cleaning up.

So the problem essentially was that my unittests weren't 
extensive enough, as they did not cover deallocation of some 
instances. Ironically, by reducing the unittest to a few lines, I 
found which one was actually triggering the problem.

TL;DR: not a D bug, problem caused by my own incompetence.
Feb 13 2014