Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 710
Evan Conrad Avatar asked Mar 20 '26 01:03

Evan Conrad


1 Answers

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>>,
}
like image 89
Evan Conrad Avatar answered Mar 22 '26 20:03

Evan Conrad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!