#MF6 (Class Hierarchy Task) Test 2 Succeeds Locally, Fails on Remote?

I’m stuck on Stage #MF6. When I copy-paste the the test case and run it locally, things are fine. But when I try to run it remotely, it’s hitting an assert I wrote.

I’ve tried to turn on the debug logging but it hasn’t shown anything useful.

Here are my logs (remote):

[tester::#MF6] [test-2] Writing contents to ./test.lox:
[tester::#MF6] [test-2.lox] {
[tester::#MF6] [test-2.lox]   class A {}
[tester::#MF6] [test-2.lox] 
[tester::#MF6] [test-2.lox]   // B is a subclass of A
[tester::#MF6] [test-2.lox]   class B < A {}
[tester::#MF6] [test-2.lox] 
[tester::#MF6] [test-2.lox]   // C is also a subclass of A
[tester::#MF6] [test-2.lox]   class C < A {}
[tester::#MF6] [test-2.lox] 
[tester::#MF6] [test-2.lox]   print A();
[tester::#MF6] [test-2.lox]   print B();
[tester::#MF6] [test-2.lox]   print C();
[tester::#MF6] [test-2.lox] }
[tester::#MF6] [test-2] $ ./your_program.sh run test.lox
[your_program] [Compile] Already a variable B+init in scope

(that last line is my assert)

Here are my logs (local):

codecrafters-interpreter-cpp % cat test.lox                  
{
  class A {}

  // B is a subclass of A
  class B < A {}

  // C is also a subclass of A
  class C < A {}

  print A();
  print B();
  print C();
}
codecrafters-interpreter-cpp % ./your_program.sh run test.lox
-- Configuring done (0.1s)
-- Generating done (0.0s)
-- Build files have been written to: codecrafters-interpreter-cpp/build
[100%] Built target interpreter
A instance
B instance
C instance
codecrafters-interpreter-cpp % echo $?

0

I doubt my code is correct to pass this stage, but this is the first time I’ve not been able to reporduce an issue locally, so I thought I would raise it. I wouldn’t be surprised if it’s some silly mistake on my end :grin:

Hey @panda-593, the issue comes from using std::string_view as the key type in the scopes map. Since string_view doesn’t own the memory it points to, it can easily become dangling when the underlying string goes out of scope.

Fix: Change the key type in scopes to std::string.

You’ll also need to update any related methods to either accept std::string directly or convert string_view to string before inserting into the map.

Let me know if you’d like any further clarification!

Thanks for pointing that out - yeah that totally makes sense that the lack of repro was due to UB. I’ll see if I can add ASAN to my local run to catch issues like these in the future!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.