#Compiling with -Doptimize=ReleaseFast works fine, but compiling with -Doptimize=Debug will be error.

1 messages · Page 1 of 1 (latest)

remote current
#

code: https://github.com/sdzx-1/mazes/tree/bug

➜  roomsAndMazes git:(bug) zig build run -Doptimize=ReleaseFast
rooms: 37
path: 13
info: ----------------------
xx: { 13478418381427711195, 10936887474700444964 }, mk: 37

output: 34
=============
p: 1.00, val: 1ms, key: generateRoom
p: 0.00, val: 0ms, key: floodFill
p: 0.00, val: 0ms, key: findConnPoint
p: 0.00, val: 0ms, key: generateTree
p: 0.00, val: 0ms, key: removeSing
total time: 1ms
➜  roomsAndMazes git:(bug) zig build run -Doptimize=Debug      
rooms: 37
path: 13
info: ----------------------
xx: { 13478418381427711195, 10936887474700444964 }, mk: 37

output: 0
General protection exception (no address available)
/home/hk/zig/zz/lib/zig/std/atomic.zig:16:40: 0x109c017 in lock (maze-exe)
            return @atomicLoad(T, &self.raw, order);
                                       ^
/home/hk/zig/zz/lib/zig/std/heap/general_purpose_allocator.zig:982:28: 0x1073893 in alloc (maze-exe)
            self.mutex.lock();
                           ^
/home/hk/zig/zz/lib/zig/std/mem/Allocator.zig:86:29: 0x109e40c in allocBytesWithAlignment__anon_10576 (maze-exe)
    return self.vtable.alloc(self.ptr, len, ptr_align, ret_addr);
                            ^
/home/hk/zig/zz/lib/zig/std/mem/Allocator.zig:211:40: 0x1076c9a in allocWithSizeAndAlignment__anon_7325 (maze-exe)
    return self.allocBytesWithAlignment(alignment, byte_count, return_address);

GitHub

Room and mazes. Contribute to sdzx-1/mazes development by creating an account on GitHub.

#
➜  roomsAndMazes git:(bug) zig version
0.14.0-dev.2246+d058d972b
#

Compiling with -Doptimize=ReleaseFast works fine, but compiling with -Doptimize=Debug will result in an error.

#

The random function on line 189 prints different results in the two builds, and if I remove the surrounding print functions, the random function gets stuck.

short summit
#

Your issue lies on L100.

#

When you return stru; you return a COPY of it as the Maze return value.

#

This is fine, and generally what you want to do if you can

#

However you cannot here.

#

Because your structure is self-referential.

#

x.random() holds onto &x, and x in your case is a field of the LOCAL variable stru.

#

That means it will be dangling after you return

#

The solutions are:

  1. allocator.create(Maze)
  2. fn init(self: *Stru, ... )
  3. Don't store the std.Random interface value, only store the RNG state itself (Xoroshiro)
#

I would always suggest that you want #3 or #2, in that order.

#

#1 requires the Maze always be allocated, which is both unnecessary and less flexible.

remote current
#

Thank you very much, you successfully solved my problem! ! ! !

short summit
#

You're welcome o7 😄

remote current
#

I'm curious why using -Doptimize=ReleaseFast works fine.