www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Problem with writeln

reply Ivan Senji <not_my_real_mail xyz.com> writes:
Hi!

I haven't been using D for a long long long time unfortunately. Recently 
I decided to give it a try again, downloaded D2.040, setup  descent 
(btw. descent looks great) and ddbg.

After some time I managed to get my old program compiling again and it 
works great. That is... it works great when not compiled with -debug.

After a lot of commenting out debug statements i found the problematic 
line was this statement:

debug writeln(".", counter ++);

My program is an OpenGL app and this line is located in my main draw 
method. It prints the numbers (counter) up to 203 and then crashes.

Using ddbg all i was able to get is this (not really that useful to me):

Unhandled D Exception (std.stream.WriteException
  "unable to write to stream") at KERNELBASE.dll (0x75d9b727)

When i change my problematic line to

core.stdc.stdio.printf(".%d\n", counter++);

The app still prints numbers up to 203 and the stops printing to the 
console but otherwise continues to run normally (the OpenGL drawing 
part, mouse input, everything else).

Any idea what could be causing this?

Thanks!

:)
Feb 28 2010
parent reply Walter Bright <newshound1 digitalmars.com> writes:
Since printf and writeln versions both exhibit the same problem, I 
suspect the problem is elsewhere in your program.
Feb 28 2010
next sibling parent reply Ivan <ivan.senji gmail.com> writes:
On 28.2.2010 20:42, Walter Bright wrote:
 Since printf and writeln versions both exhibit the same problem, I
 suspect the problem is elsewhere in your program.

That wouldn't surprise me :) But I am having some problems figuring this out. Somehow playing with ddbg i was able to get this stack trace: #0 ?? () at C:\dmd2\windows\bin\..\..\src\phobos\std\contracts.d:147 from KERNEL32.dll #1 0x00411724 in __d_throw 4 () at C:\dmd2\windows\bin\..\..\src\phobos\std\contracts.d:147 from deh #2 0x004049f8 in _D3std5stdio4File17LockingTextWriter12__T3putTAxaZ3putMFAxaZv () at C:\dmd2\windows\bin\..\..\src\phobos\std\contracts.d:147 #3 0x00404887 in _D3std6format65__T19writeUpToFormatSpecTS3std5stdio4File17LockingTextWriterTAxaZ19writeUpToFormatSpecFKS3std5stdio4File17Loc ingTextWriterKAxaZv () at C:\dmd2\windows\bin\..\..\src\phobos\std\format.d:1724 #4 0x004062cc in _D3std6format62__T14formattedWriteTS3std5stdio4File17LockingTextWriterTaTAyaZ14formattedWriteFKS3std5stdio4File17Locki gTextWriterAxaAyaZv (w = 0x0013fdb8, fmt = "Exception was: %s", _param_2 = "unable to write to stream") at C:\dmd2\windows\bin\..\..\src\phobos\std\format.d:2189 #5 0x00406225 in _D3std5stdio4File21__T8writeflnTAyaTAyaZ8writeflnMFAyaAyaZv () at C:\dmd2\windows\bin\..\..\src\phobos\std\stdio.d:581 #6 0x004061a9 in _D3std5stdio21__T8writeflnTAyaTAyaZ8writeflnFAyaAyaZv (_param_0 = "Exception was: %s", _param_1 = "unable to write to stream") at C:\dmd2\windows\bin\..\..\src\phobos\std\stdio.d:1310 #7 0x004033ea in _Dmain (args = { [0] = "D:\\ivans\\Projects\\D\\eclipse-workspace-d\\ifs3d\\ifs3d.exe" }) at ivan\ifs3d\ifs3d.d:98 #8 0x00411534 in extern (C) int rt.dmain2.main(int, char**) . void runMain(void*) () from dmain2 #9 0x00411571 in extern (C) int rt.dmain2.main(int, char**) . void runAll(void*) () from dmain2 #10 0x004112e4 in _main () from dmain2 #11 0x00473c41 in _mainCRTStartup () from constart #12 0x7c817077 in ?? () from KERNEL32.dll The part of code in my main method is this: try { writefln("Starting main loop..."); global.loop.start(); writefln("Main loop finished..."); } catch(Exception e) { writefln("Exception was: %s", e.msg); } From what I can see an exception was thrown ("unable to write to stream") and the only line I added to the loop is this: writefln(". %s", counter++); If that line is removed, an exception seams to be thrown from the line writefln("Main loop finished..."); And this is the exception: Unhandled D Exception (std.contracts.ErrnoException "C:\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(896): (No error)") at KERNEL32.dll (0x7c812afb) thread(3548) Has anyone ever had an exception like this? Any tips on debugging?
Mar 01 2010
parent reply Ivan <ivan.senji gmail.com> writes:
On 1.3.2010 12:47, Steven Schveighoffer wrote:
 On Mon, 01 Mar 2010 04:11:28 -0500, Ivan <ivan.senji gmail.com> wrote:

 The part of code in my main method is this:

 try {
 writefln("Starting main loop...");
 global.loop.start();
 writefln("Main loop finished...");
 } catch(Exception e) {
 writefln("Exception was: %s", e.msg);
 }

 From what I can see an exception was thrown ("unable to write to
 stream") and the only line I added to the loop is this:

 writefln(". %s", counter++);

 If that line is removed, an exception seams to be thrown from the line
 writefln("Main loop finished...");

Post more code. I suspect you are messing up something. It appears that you may be closing stdout, but I can't be sure because your code is not complete.

The code can be seen here: http://github.com/ivans/ifs3D I didn't and still don't want to bother anyone too much. :) It should compile on windows with a command found in a readme file. I don't think I am closing stdout anywhere and I probably am doing something wrong somewhere, but can't figure out where and what.
Mar 01 2010
parent reply Ivan <ivan.senji gmail.com> writes:
On 1.3.2010 14:43, Steven Schveighoffer wrote:
 On Mon, 01 Mar 2010 07:14:23 -0500, Ivan <ivan.senji gmail.com> wrote:

 On 1.3.2010 12:47, Steven Schveighoffer wrote:
 On Mon, 01 Mar 2010 04:11:28 -0500, Ivan <ivan.senji gmail.com> wrote:

 The part of code in my main method is this:

 try {
 writefln("Starting main loop...");
 global.loop.start();
 writefln("Main loop finished...");
 } catch(Exception e) {
 writefln("Exception was: %s", e.msg);
 }

 From what I can see an exception was thrown ("unable to write to
 stream") and the only line I added to the loop is this:

 writefln(". %s", counter++);

 If that line is removed, an exception seams to be thrown from the line
 writefln("Main loop finished...");

Post more code. I suspect you are messing up something. It appears that you may be closing stdout, but I can't be sure because your code is not complete.

The code can be seen here: http://github.com/ivans/ifs3D I didn't and still don't want to bother anyone too much. :) It should compile on windows with a command found in a readme file. I don't think I am closing stdout anywhere and I probably am doing something wrong somewhere, but can't figure out where and what.

Your code is too complex for me to understand quickly :) Comment out other lines until you figure out which one closes it. Start with commenting out the entire loop :) If that then works, you know something inside the loop is killing stdout. BTW, are you compiling in Windows mode? I.e. do you see a console on the screen when you run it? If not, then there is no stdout or stdin. From your command line in the readme, I don't think you are. However, in that case, I don't think you'd get anywhere or see any output.

Thank you for helping. I did what you suggested and started commenting out code to get it working. I finally got to a working code with just two lines commented out: http://github.com/ivans/ifs3D/commit/339ca5ee58661d636bfcd878c08b06bacede7dca These lines have nothing to do with streams and stdout, so I still don't understand what is going on. But this part of code is very old and stupid (allocating a lot of unneccessary real[][]'s) so I guess the next step is to figure out a smarter way to do that part and see if the exception goes away :). Uh I have missed debuging D programs :).
Mar 01 2010
parent Ivan <ivan.senji gmail.com> writes:
On 1.3.2010 17:15, Steven Schveighoffer wrote:
 On Mon, 01 Mar 2010 10:42:00 -0500, Ivan <ivan.senji gmail.com> wrote:

 On 1.3.2010 14:43, Steven Schveighoffer wrote:
 On Mon, 01 Mar 2010 07:14:23 -0500, Ivan <ivan.senji gmail.com> wrote:

 On 1.3.2010 12:47, Steven Schveighoffer wrote:
 On Mon, 01 Mar 2010 04:11:28 -0500, Ivan <ivan.senji gmail.com> wrote:

 The part of code in my main method is this:

 try {
 writefln("Starting main loop...");
 global.loop.start();
 writefln("Main loop finished...");
 } catch(Exception e) {
 writefln("Exception was: %s", e.msg);
 }

 From what I can see an exception was thrown ("unable to write to
 stream") and the only line I added to the loop is this:

 writefln(". %s", counter++);

 If that line is removed, an exception seams to be thrown from the
 line
 writefln("Main loop finished...");

Post more code. I suspect you are messing up something. It appears that you may be closing stdout, but I can't be sure because your code is not complete.

The code can be seen here: http://github.com/ivans/ifs3D I didn't and still don't want to bother anyone too much. :) It should compile on windows with a command found in a readme file. I don't think I am closing stdout anywhere and I probably am doing something wrong somewhere, but can't figure out where and what.

Your code is too complex for me to understand quickly :) Comment out other lines until you figure out which one closes it. Start with commenting out the entire loop :) If that then works, you know something inside the loop is killing stdout. BTW, are you compiling in Windows mode? I.e. do you see a console on the screen when you run it? If not, then there is no stdout or stdin. From your command line in the readme, I don't think you are. However, in that case, I don't think you'd get anywhere or see any output.

Thank you for helping. I did what you suggested and started commenting out code to get it working. I finally got to a working code with just two lines commented out: http://github.com/ivans/ifs3D/commit/339ca5ee58661d636bfcd878c08b06bacede7dca These lines have nothing to do with streams and stdout, so I still don't understand what is going on. But this part of code is very old and stupid (allocating a lot of unneccessary real[][]'s) so I guess the next step is to figure out a smarter way to do that part and see if the exception goes away :).

I suspect there may be a bug in dmd when dealing with reals, specifically with your array creation routine. That code doesn't seem to be incorrect by any means. Try building the resulting arrays manually (that is, instead of using that array template function), and see if it helps. Also, try removing the calls to the inner sq function (BTW, you don't need to make that an inner function, doing so unnecessarily complicates things). If you can find the thing that fixes it, we may be able to find a reduced test case. Also, try upgrading your compiler to the latest if you haven't already.

The problem wasn't with reals. I added an alias so I can easily change my floating point type and application crashed with all of them, but after a diferent amount of iterations. I managed to get rid of the problem by removing a lot of dynamic array allocations and I am now mostly using static arrays. I still have this same problem in another peace of that same program and will still try to get a reproducable and small test case. Thanks for all the help and good advice. :)
Mar 05 2010
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 01 Mar 2010 04:11:28 -0500, Ivan <ivan.senji gmail.com> wrote:

 The part of code in my main method is this:

 	try {
 		writefln("Starting main loop...");
 		global.loop.start();
 		writefln("Main loop finished...");
 	} catch(Exception e) {
 		writefln("Exception was: %s", e.msg);
 	}

  From what I can see an exception was thrown ("unable to write to  
 stream") and the only line I added to the loop is this:

 	writefln(". %s", counter++);

 If that line is removed, an exception seams to be thrown from the line
 	writefln("Main loop finished...");

Post more code. I suspect you are messing up something. It appears that you may be closing stdout, but I can't be sure because your code is not complete. -Steve
Mar 01 2010
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 01 Mar 2010 07:14:23 -0500, Ivan <ivan.senji gmail.com> wrote:

 On 1.3.2010 12:47, Steven Schveighoffer wrote:
 On Mon, 01 Mar 2010 04:11:28 -0500, Ivan <ivan.senji gmail.com> wrote:

 The part of code in my main method is this:

 try {
 writefln("Starting main loop...");
 global.loop.start();
 writefln("Main loop finished...");
 } catch(Exception e) {
 writefln("Exception was: %s", e.msg);
 }

 From what I can see an exception was thrown ("unable to write to
 stream") and the only line I added to the loop is this:

 writefln(". %s", counter++);

 If that line is removed, an exception seams to be thrown from the line
 writefln("Main loop finished...");

Post more code. I suspect you are messing up something. It appears that you may be closing stdout, but I can't be sure because your code is not complete.

The code can be seen here: http://github.com/ivans/ifs3D I didn't and still don't want to bother anyone too much. :) It should compile on windows with a command found in a readme file. I don't think I am closing stdout anywhere and I probably am doing something wrong somewhere, but can't figure out where and what.

Your code is too complex for me to understand quickly :) Comment out other lines until you figure out which one closes it. Start with commenting out the entire loop :) If that then works, you know something inside the loop is killing stdout. BTW, are you compiling in Windows mode? I.e. do you see a console on the screen when you run it? If not, then there is no stdout or stdin. From your command line in the readme, I don't think you are. However, in that case, I don't think you'd get anywhere or see any output. -Steve
Mar 01 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 01 Mar 2010 10:42:00 -0500, Ivan <ivan.senji gmail.com> wrote:

 On 1.3.2010 14:43, Steven Schveighoffer wrote:
 On Mon, 01 Mar 2010 07:14:23 -0500, Ivan <ivan.senji gmail.com> wrote:

 On 1.3.2010 12:47, Steven Schveighoffer wrote:
 On Mon, 01 Mar 2010 04:11:28 -0500, Ivan <ivan.senji gmail.com> wrote:

 The part of code in my main method is this:

 try {
 writefln("Starting main loop...");
 global.loop.start();
 writefln("Main loop finished...");
 } catch(Exception e) {
 writefln("Exception was: %s", e.msg);
 }

 From what I can see an exception was thrown ("unable to write to
 stream") and the only line I added to the loop is this:

 writefln(". %s", counter++);

 If that line is removed, an exception seams to be thrown from the  
 line
 writefln("Main loop finished...");

Post more code. I suspect you are messing up something. It appears that you may be closing stdout, but I can't be sure because your code is not complete.

The code can be seen here: http://github.com/ivans/ifs3D I didn't and still don't want to bother anyone too much. :) It should compile on windows with a command found in a readme file. I don't think I am closing stdout anywhere and I probably am doing something wrong somewhere, but can't figure out where and what.

Your code is too complex for me to understand quickly :) Comment out other lines until you figure out which one closes it. Start with commenting out the entire loop :) If that then works, you know something inside the loop is killing stdout. BTW, are you compiling in Windows mode? I.e. do you see a console on the screen when you run it? If not, then there is no stdout or stdin. From your command line in the readme, I don't think you are. However, in that case, I don't think you'd get anywhere or see any output.

Thank you for helping. I did what you suggested and started commenting out code to get it working. I finally got to a working code with just two lines commented out: http://github.com/ivans/ifs3D/commit/339ca5ee58661d636bfcd878c08b06bacede7dca These lines have nothing to do with streams and stdout, so I still don't understand what is going on. But this part of code is very old and stupid (allocating a lot of unneccessary real[][]'s) so I guess the next step is to figure out a smarter way to do that part and see if the exception goes away :).

I suspect there may be a bug in dmd when dealing with reals, specifically with your array creation routine. That code doesn't seem to be incorrect by any means. Try building the resulting arrays manually (that is, instead of using that array template function), and see if it helps. Also, try removing the calls to the inner sq function (BTW, you don't need to make that an inner function, doing so unnecessarily complicates things). If you can find the thing that fixes it, we may be able to find a reduced test case. Also, try upgrading your compiler to the latest if you haven't already. -Steve
Mar 01 2010