# Invalid memory access (signal 11) during specs

**URL:** <https://forum.crystal-lang.org/t/invalid-memory-access-signal-11-during-specs/2169>\
**Category:** Help & Support\
**Created:** [May 25, 2020, 7:23pm UTC](https://forum.crystal-lang.org/t/invalid-memory-access-signal-11-during-specs/2169 "2020-05-25T19:23:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwoertink](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jwoertink/32/50_2.png) [@jwoertink](https://forum.crystal-lang.org/u/jwoertink)\
**Post date:** [May 25, 2020, 7:23pm UTC](https://forum.crystal-lang.org/t/invalid-memory-access-signal-11-during-specs/2169/1 "2020-05-25T19:23:31Z")

</div>

I know we’ve all seen this error before, and there’s been a few other issues and forum posts on it. I’m starting this one because all of the others seem to veer off in to more specific OS related deals, but I feel like this is a much larger issue.

I’m currently working on an [update for Avram](https://github.com/luckyframework/avram/pull/369), and during this update I ripped out a bunch of code. As I slowly put some in, there’s several places where adding in macro code will throw this stack when running specs.

```nohighlight
avram (refactors/operation)$ crystal spec spec/operation_specs/save_operation_spec.cr
Invalid memory access (signal 11) at address 0x0
[0x1097f8e97] *CallStack::print_backtrace:Int32 +39
[0x10974247a] __crystal_sigfault_handler +522
[0x7fff6c5075fd] _sigtramp +29
[0x10a7f0047] _ZN4llvm11PointerType3getEPNS_4TypeEj +21
[0x10a3c40cf] _ZN4llvm17GetElementPtrInst16getGEPReturnTypeEPNS_4TypeEPNS_5ValueENS_8ArrayRefIS4_EE +63
[0x10a3e117b] _ZN4llvm17GetElementPtrInst6CreateEPNS_4TypeEPNS_5ValueENS_8ArrayRefIS4_EERKNS_5TwineEPNS_11InstructionE +107
[0x109c1c762] _ZN4llvm9IRBuilderINS_14ConstantFolderENS_24IRBuilderDefaultInserterEE17CreateInBoundsGEPEPNS_4TypeEPNS_5ValueENS_8ArrayRefIS7_EERKNS_5TwineE +130
[0x10a763fec] LLVMBuildInBoundsGEP +84
[0x10935bec8] *Crystal::CodeGenVisitor@Crystal::LLVMBuilderHelper#gep<LLVM::Value, Int32, Int32, String>:LLVM::Value +168
[0x1093bce24] *Crystal::CodeGenVisitor#read_instance_var<Crystal::Type+, Crystal::Type+, String, LLVM::Value>:Bool +100
[0x1093840b6] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +23542
[0x1093c89ca] *Crystal::CodeGenVisitor#visit<Crystal::Assign>:(Bool | Nil) +298
[0x10937ea71] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1457
[0x1093cd9db] *Crystal::CodeGenVisitor#codegen_cond<Crystal::ASTNode+>:LLVM::Value +27
[0x109381e56] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14742
[0x10939aff7] *Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function +4711
[0x1093b73a8] *Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function +2664
[0x1093a9718] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2632
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10939aff7] *Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function +4711
[0x1093b73a8] *Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function +2664
[0x1093a9718] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2632
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x1093b5015] *Crystal::CodeGenVisitor#prepare_call_args_non_external<Crystal::Call, Crystal::Def+, Crystal::Type+>:Tuple(Array(LLVM::Value), Bool) +1461
[0x1093a96f0] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2592
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10937eb49] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1673
[0x109386618] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +33112
[0x1093b4b5b] *Crystal::CodeGenVisitor#prepare_call_args_non_external<Crystal::Call, Crystal::Def+, Crystal::Type+>:Tuple(Array(LLVM::Value), Bool) +251
[0x1093a96f0] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2592
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x1093b5015] *Crystal::CodeGenVisitor#prepare_call_args_non_external<Crystal::Call, Crystal::Def+, Crystal::Type+>:Tuple(Array(LLVM::Value), Bool) +1461
[0x1093a96f0] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2592
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10937eb49] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1673
[0x1093a9edd] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +4621
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10939aff7] *Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function +4711
[0x1093b73a8] *Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function +2664
[0x1093a9718] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2632
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x1093b4b5b] *Crystal::CodeGenVisitor#prepare_call_args_non_external<Crystal::Call, Crystal::Def+, Crystal::Type+>:Tuple(Array(LLVM::Value), Bool) +251
[0x1093a96f0] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2592
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10939aff7] *Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function +4711
[0x1093b73a8] *Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function +2664
[0x1093a9718] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2632
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x1093b4b5b] *Crystal::CodeGenVisitor#prepare_call_args_non_external<Crystal::Call, Crystal::Def+, Crystal::Type+>:Tuple(Array(LLVM::Value), Bool) +251
[0x1093a96f0] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2592
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10939aff7] *Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function +4711
[0x1093b73a8] *Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function +2664
[0x1093a9718] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2632
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x1093cd9db] *Crystal::CodeGenVisitor#codegen_cond<Crystal::ASTNode+>:LLVM::Value +27
[0x109381e56] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14742
[0x10937eb49] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1673
[0x1093a9edd] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +4621
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10937eb49] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1673
[0x10939aff7] *Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function +4711
[0x109380651] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +8593
[0x1093a8fbe] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +750
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10937eb49] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1673
[0x10939aff7] *Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function +4711
[0x109380651] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +8593
[0x1093a8fbe] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +750
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10937eb49] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1673
[0x10939aff7] *Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function +4711
[0x109380651] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +8593
[0x1093a8fbe] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +750
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10937eb49] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1673
[0x10937ef54] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +2708
[0x10937eb49] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1673
[0x109331a39] *Crystal::Compiler#codegen<Crystal::Program, Crystal::ASTNode+, Array(Crystal::Compiler::Source), String>:(Tuple(Array(Crystal::Compiler::CompilationUnit), Array(String)) | Nil) +185
[0x10975c8d7] *Crystal::Compiler#compile<Array(Crystal::Compiler::Source), String>:Crystal::Compiler::Result +135
[0x1097cfb13] *Crystal::Command#spec:NoReturn +3043
[0x10974f2ec] *Crystal::Command#run:(Bool | Crystal::Compiler::Result | Nil) +27948
[0x108daae64] __crystal_main +6836
[0x108db00e1] main +65

```

In this case, adding [these lines](https://github.com/luckyframework/avram/blob/b02502dab8b7fc304bcd2d7bf21a165dfa3dac57/src/avram/save_operation.cr#L185-L192) will cause is to blow up. If I remove these lines and run the spec, I’m able to get a normal compile error.

In my case here, this seems to be localized to the `SaveOperation` stuff. If I remove that, then I can get specs to pass. The difference between a `SaveOperation` and the `Operation` is that `SaveOperation` will map DB columns to methods for use when saving a model. These lines will generate a `cast_value` method that takes the type of each DB column.

Even more interesting that I noticed in the first few lines of the stack was mention of `Bool`. It also appears that the column that’s trying to be mapped is a `Bool` column. So maybe this is trying to cast Bool to String and then Crystal freaks out?

I’m currently using 0.34. I wanted to try this out with 0.35, but there’s a lot more issues to fix to get it to compile with that.

```nohighlight
avram (refactors/operation)$ crystal -v
Crystal 0.34.0 (2020-04-07)

LLVM: 10.0.0
Default target: x86_64-apple-macosx

```

I also mentioned some references to a similar error in [this issue](https://github.com/crystal-lang/crystal/issues/9148), but I think that original issue is something separate and not really related. There’s also [a lot of other related issues](https://github.com/crystal-lang/crystal/issues?q=is%3Aissue+is%3Aopen+Invalid+memory+access) so I didn’t want to just open a new one, and I’m not sure which error to append this to.

If anyone has any insight, or ideas of how to get around this, I’d very much appreciate it. I’ll keep hammering away until then though.

Thanks!

---

<div class="post-metadata">

**Author:** ![oprypin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/oprypin/32/859_2.png) [@oprypin](https://forum.crystal-lang.org/u/oprypin)\
**Post date:** [May 25, 2020, 9:51pm UTC](https://forum.crystal-lang.org/t/invalid-memory-access-signal-11-during-specs/2169/2 "2020-05-25T21:51:31Z")

</div>

Actual repro:

```
git init               
git fetch https://github.com/luckyframework/avram b02502dab8b7fc304bcd2d7bf21a165dfa3dac57
git checkout FETCH_HEAD
shards install
crystal build spec/operation_specs/save_operation_spec.cr

```

`Invalid memory access (signal 11) at address 0x0`

---

<div class="post-metadata">

**Author:** ![jwoertink](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jwoertink/32/50_2.png) [@jwoertink](https://forum.crystal-lang.org/u/jwoertink)\
**Post date:** [May 25, 2020, 11:21pm UTC](https://forum.crystal-lang.org/t/invalid-memory-access-signal-11-during-specs/2169/3 "2020-05-25T23:21:43Z")

</div>

Thanks for posting that!

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [June 5, 2020, 8:50pm UTC](https://forum.crystal-lang.org/t/invalid-memory-access-signal-11-during-specs/2169/4 "2020-06-05T20:50:23Z")

</div>

I managed to reduce it to this:

```crystal
class Foo
  @x = 0
end

class Bar(T) < Foo
  @x = 0
end

class Baz < Bar(Int32)
  @y = true
end

Baz.new

```

---

<div class="post-metadata">

**Author:** ![jwoertink](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jwoertink/32/50_2.png) [@jwoertink](https://forum.crystal-lang.org/u/jwoertink)\
**Post date:** [June 5, 2020, 9:31pm UTC](https://forum.crystal-lang.org/t/invalid-memory-access-signal-11-during-specs/2169/5 "2020-06-05T21:31:31Z")

</div>

wow. that’s wild. Great find! Sorry you had to dig through a mountain to find it ☹ but this will be huge!
