# Segmentation fault with Dict

**URL:** <https://forum.modular.com/t/segmentation-fault-with-dict/1656>\
**Category:** Mojo\
**Created:** [June 14, 2025, 6:07pm UTC](https://forum.modular.com/t/segmentation-fault-with-dict/1656 "2025-06-14T18:07:58Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![notooth](https://avatars.discourse-cdn.com/v4/letter/n/ce73a5/32.png) [@notooth](https://forum.modular.com/u/notooth)\
**Post date:** [June 14, 2025, 6:07pm UTC](https://forum.modular.com/t/segmentation-fault-with-dict/1656/1 "2025-06-14T18:07:58Z")

</div>

Can anyone let me know why I got `Segmentation fault` with this code:

```auto
struct Number(Copyable & Movable):
    var a: Int128 
    fn __init__ (out self, owned a: Int128):
        self.a = a

fn main():
    var number = Dict[String, Number]()
    for idx in range(5):
        print("Adding number", idx)
        number[String(idx)] = Number(Int128(idx))

```

```auto
$ mojo life.mojo
Adding number 0
Adding number 1
Please submit a bug report to https://github.com/modular/modular/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0.	Program arguments: mojo life.mojo
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0 mojo 0x000055f29485d6ab
1 mojo 0x000055f29485b449
2 mojo 0x000055f29485dd5a
3 libc.so.6 0x00007f1af9c67070
4 libc.so.6 0x00007f1af9cc0334
5 libc.so.6 0x00007f1af9c66fbe gsignal + 30
6 libAsyncRTMojoBindings.so 0x00007f1afbdb2ac2
7 libc.so.6 0x00007f1af9c67070
8 (error) 0x00007f1a04002595
mojo crashed!
Please file a bug report.
Segmentation fault (core dumped)

```

---

<div class="post-metadata">

**Author:** ![clattner](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/clattner/32/201_2.png) [@clattner](https://forum.modular.com/u/clattner)\
**Post date:** [June 15, 2025, 4:22am UTC](https://forum.modular.com/t/segmentation-fault-with-dict/1656/2 "2025-06-15T04:22:17Z")

</div>

> [@notooth](#):
>
> ```auto
> struct Number(Copyable & Movable):
> var a: Int128 
> fn __init__ (out self, owned a: Int128):
> self.a = a
> 
> fn main():
> var number = Dict[String, Number]()
> for idx in range(5):
> print("Adding number", idx)
> number[String(idx)] = Number(Int128(idx))
> 
> ```

this doesn’t crash for me with a recent build. I think I saw someone talking about Int128 → String having a bug and crashing, I think that recently got fixed, can you try it out with the most recent nightly?

---

<div class="post-metadata">

**Author:** ![notooth](https://avatars.discourse-cdn.com/v4/letter/n/ce73a5/32.png) [@notooth](https://forum.modular.com/u/notooth)\
**Post date:** [June 15, 2025, 4:26am UTC](https://forum.modular.com/t/segmentation-fault-with-dict/1656/3 "2025-06-15T04:26:53Z")

</div>

I have just updated it to `Mojo 25.5.0.dev2025061405 (b81f396d)`, and it still crashes.

---

<div class="post-metadata">

**Author:** ![ssslakter](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/ssslakter/32/435_2.png) [@ssslakter](https://forum.modular.com/u/ssslakter)\
**Post date:** [June 15, 2025, 9:39am UTC](https://forum.modular.com/t/segmentation-fault-with-dict/1656/4 "2025-06-15T09:39:00Z")

</div>

I’ve also experimented with this and found that the segfault occurs only when there are at least two dictionary fields in the struct. I guess original question also had dicts in the struct, or maybe it was another question on the forums, though I can’t find it now.  
The code below works fine; however, if you include commented lines, it will segfault. If you then also replace `Int128` with `Int64` or smaller, this would also work fine.

```mojo
struct A(Writable, Copyable & Movable):
    var number: Int128
    var dict: Dict[String, Int]
    # var dict2: Dict[String, Int]

    fn __init__ (out self, number: Int128, dict: Dict[String, Int], dict2: Dict[String, Int]):
        self.number = number
        self.dict = dict
        # self.dict2 = dict2

    fn __copyinit__ (out self, existing: Self):
        print('copying')
        self.number = existing.number
        self.dict = existing.dict
        # self.dict2 = existing.dict2

    fn __moveinit__ (out self, owned existing: Self):
        print('moving')
        self.number = existing.number
        self.dict = existing.dict
        # self.dict2 = existing.dict2

    
    fn write_to[W: Writer](self, mut writer: W) -> None:
        self.write_dict(writer, self.dict)
        writer.write(",")
        # self.write_dict(writer, self.dict2)

    @staticmethod
    fn write_dict[W: Writer, K: KeyElement & Writable, V: Copyable & Movable & Writable](mut writer: W, dict: Dict[K, V]) -> None:
        writer.write("{")
        first = True
        for entry in dict.items():
            if not first: writer.write(',')
            writer.write(entry.key, ":", entry.value)
            first = False
        writer.write("}")

def main():
    var structs = Dict[Int, A]()
    for i in range(2):
        d1 = Dict[String, Int]()
        d2 = Dict[String, Int]()
        structs[i] = A(i, d1, d2)
    for i in range(1):
        print(structs[i])

```

UPD:  
This is the simplest program I found that crashes for me on the latest nightly, it won’t crash when you replace key type from `String` to `Int` or value type to something less than `Int128`

```mojo
fn main():
    var number = Dict[String, Int128]()
    for idx in range(2):
        number[String(idx)] = Int128(idx)

```

---

<div class="post-metadata">

**Author:** ![duck\_tape](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/duck_tape/32/432_2.png) [@duck\_tape](https://forum.modular.com/u/duck_tape)\
**Post date:** [June 15, 2025, 9:55am UTC](https://forum.modular.com/t/segmentation-fault-with-dict/1656/5 "2025-06-15T09:55:16Z")

</div>

I think it’s related to this: [UInt256 returns wrong value - #6 by sora](https://forum.modular.com/t/uint256-returns-wrong-value/1643/6)

When printing numbers \> 64 bits it’s overwriting the inline array and corrupting the program.

---

<div class="post-metadata">

**Author:** ![ssslakter](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/ssslakter/32/435_2.png) [@ssslakter](https://forum.modular.com/u/ssslakter)\
**Post date:** [June 15, 2025, 10:00am UTC](https://forum.modular.com/t/segmentation-fault-with-dict/1656/6 "2025-06-15T10:00:10Z")

</div>

In my latest example, I’m only populating the dictionary—there aren’t any print statements, so I’m not sure if that’s the same issue here.

---

<div class="post-metadata">

**Author:** ![duck\_tape](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/duck_tape/32/432_2.png) [@duck\_tape](https://forum.modular.com/u/duck_tape)\
**Post date:** [June 15, 2025, 10:23am UTC](https://forum.modular.com/t/segmentation-fault-with-dict/1656/7 "2025-06-15T10:23:23Z")

</div>

It might be the `String(idx)`, which also goes through the int formatting methods I think.

---

<div class="post-metadata">

**Author:** ![notooth](https://avatars.discourse-cdn.com/v4/letter/n/ce73a5/32.png) [@notooth](https://forum.modular.com/u/notooth)\
**Post date:** [June 16, 2025, 12:04am UTC](https://forum.modular.com/t/segmentation-fault-with-dict/1656/8 "2025-06-16T00:04:28Z")

</div>

@clattner Can you let me know the nightly version that you are running?

---

<div class="post-metadata">

**Author:** ![samufi](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@samufi](https://forum.modular.com/u/samufi)\
**Post date:** [June 16, 2025, 6:31am UTC](https://forum.modular.com/t/segmentation-fault-with-dict/1656/9 "2025-06-16T06:31:08Z")

</div>

To me it sounds as if this could be another version of this bug: [[BUG] UB in Variant when using SIMD · Issue #4578 · modular/modular · GitHub](https://github.com/modular/modular/issues/4578)  
The Dict uses Variant internally, so that the issue above causes the following issue in Dict, which resembles the one reported here: [[BUG] Segfault if using a struct based on SIMD as key in Dict · Issue #3781 · modular/modular · GitHub](https://github.com/modular/modular/issues/3781)

---

<div class="post-metadata">

**Author:** ![notooth](https://avatars.discourse-cdn.com/v4/letter/n/ce73a5/32.png) [@notooth](https://forum.modular.com/u/notooth)\
**Post date:** [June 17, 2025, 12:36am UTC](https://forum.modular.com/t/segmentation-fault-with-dict/1656/10 "2025-06-17T00:36:52Z")

</div>

> [@samufi](#):
>
> [[BUG] Segfault if using a struct based on SIMD as key in Dict · Issue #3781 · modular/modular · GitHub](https://github.com/modular/modular/issues/3781)

It has been a long time. Is there a fix?

---

<div class="post-metadata">

**Author:** ![samufi](https://avatars.discourse-cdn.com/v4/letter/s/d2c977/32.png) [@samufi](https://forum.modular.com/u/samufi)\
**Post date:** [June 17, 2025, 11:48am UTC](https://forum.modular.com/t/segmentation-fault-with-dict/1656/11 "2025-06-17T11:48:48Z")

</div>

Not that I am aware of. This has been blocking a project of mine for a long time now. Maybe someone from the modular staff can provide us with some insight as to when a fix may be expected.

---

<div class="post-metadata">

**Author:** ![duck\_tape](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/duck_tape/32/432_2.png) [@duck\_tape](https://forum.modular.com/u/duck_tape)\
**Post date:** [June 17, 2025, 1:17pm UTC](https://forum.modular.com/t/segmentation-fault-with-dict/1656/12 "2025-06-17T13:17:33Z")

</div>

[[stdlib] Fix int formatting to not go out of bounds, add MIN and MAX for 128 and 256 sized integers by sstadick · Pull Request #4852 · modular/modular · GitHub](https://github.com/modular/modular/pull/4852#issue-3153481339) fixes both this issue, and the original issue [UInt256 returns wrong value - #6 by sora](https://forum.modular.com/t/uint256-returns-wrong-value/1643/6).

The tests show the fix for format\_int working. I also checked that:

```mojo
fn main():
    var number = Dict[String, Int128]()
    for idx in range(2):
        number[String(idx)] = Int128(idx)

```

failed before the fix and works after.

---

<div class="post-metadata">

**Author:** ![duck\_tape](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/duck_tape/32/432_2.png) [@duck\_tape](https://forum.modular.com/u/duck_tape)\
**Post date:** [June 17, 2025, 2:05pm UTC](https://forum.modular.com/t/segmentation-fault-with-dict/1656/13 "2025-06-17T14:05:01Z")

</div>

Well, I guess that didn’t fix it all the way. The ubuntu test runner seg-faulted with that minimal dict example in there. My changes at least fix it on mac. So @samufi’s issue does seem to also be in play.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)\
**Post date:** [December 14, 2025, 2:05pm UTC](https://forum.modular.com/t/segmentation-fault-with-dict/1656/14 "2025-12-14T14:05:58Z")

</div>

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