Search code examples
rustsolanaanchor-solana

How do I avoid my anchor program throwing an "Access violation in stack frame"?


My Anchor program has an instruction struct that looks like this:

#[derive(Accounts)]
pub struct MyInstruction<'info> {
  pub my_account: Account<'info, MyAccount>,

  // ...
}

#[account]
pub struct MyAccount {
  // ... Many different fields
}

When I try to run an instruction that uses that struct, I get a weird stack error like this:

Program failed to complete: Access violation in stack frame 3 at address 0x200003fe0 of size 8 by instruction #28386

What gives?


Solution

  • Anchor puts your accounts on the stack by default. But, likely, because your accounts are quite big, or you have a lot of them, you're running of space on the stack.

    If you look above in your logs, you might have an error that looks like this:

    Stack offset of -4128 exceeded max offset of -4096 by 32 bytes, please minimize large stack variables
    

    To solve this problem, you could try Boxing your account structs, to move them to the heap:

    #[derive(Accounts)]
    pub struct MyInstruction<'info> {
      // Note the Box<>! 
      pub my_account: Box<Account<'info, MyAccount>>,
    }