简体   繁体   中英

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?

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 Box ing 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>>,
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM