如何避免我的锚程序抛出“堆栈帧中的访问冲突”?

Eva*_*rad 2 rust solana anchor-solana

我的 Anchor 程序有一个如下所示的指令结构:

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

  // ...
}

#[account]
pub struct MyAccount {
  // ... Many different fields
}
Run Code Online (Sandbox Code Playgroud)

当我尝试运行使用该结构的指令时,我收到一个奇怪的堆栈错误,如下所示:

Program failed to complete: Access violation in stack frame 3 at address 0x200003fe0 of size 8 by instruction #28386
Run Code Online (Sandbox Code Playgroud)

是什么赋予了?

Eva*_*rad 8

默认情况下,Anchor 将您的帐户放在堆栈上。但是,很可能,因为您的帐户非常大,或者您有很多帐户,所以您的堆栈空间不足。

如果您查看上面的日志,您可能会遇到如下错误:

Stack offset of -4128 exceeded max offset of -4096 by 32 bytes, please minimize large stack variables
Run Code Online (Sandbox Code Playgroud)

要解决这个问题,您可以尝试Boxing 您的帐户结构,将它们移动到堆中:

Stack offset of -4128 exceeded max offset of -4096 by 32 bytes, please minimize large stack variables
Run Code Online (Sandbox Code Playgroud)