Strange bug when passing a slice to another function

I’m writing a test, and the test is failing on this line because x.chars contains garbage:

try std.testing.expectEqualStrings("Hello, world!", x.chars);

x.chars is a slice that points to an array. When I debug I can see that the contents of x.chars correctly contains “Hello, world!”, but as soon as I step inside expectEqualStrings() the contents completely change (but len stays the same).

Inside my test:


Inside expectEqualStrings():
image

What am I doing wrong?

Nevermind. The problem was in my init() function. I was creating an object on the stack, then assigning chars to that array, so in my test chars was pointed to something that happened to still be on the stack until the expectEqualStrings() call.

1 Like