无法解除分配基于静态的变量

Not able to deallocate static based variable

我在尝试取消分配基于静态的变量时得到了奇怪的结果。下面是我创建的一个小测试程序,用于演示我遇到的问题。基于静态的变量位于该测试程序的 proc1 子过程中。它的名字是myIx,基址指针为myIx_p。我在程序的主体中添加了注释以指出我遇到的问题。这里有什么问题?

 D ix              s              5i 0                                                       

  /free                                                                                      
   ix = proc1(*off);     // ix is 1 after this statement => Expected                          
   ix = proc1(*off);     // ix is 2 after this statement => Expected.                         
   ix = proc1(*on);      // ix is 0, which is expected.  But myIx_p is not being set to null => NOT Expected
   ix = proc1(*off);     // ix is 3 after this statement => NOT Expected.  Expecting 1.       
   ix = proc1(*on);      // try to shutdown again and get CEE0810 error => NOT Expected                          

   *inlr = *on;                                                                              

  *------------------------------------------------------------------------------------------
  * proc1 to test based static variable                                                      
  *------------------------------------------------------------------------------------------
 P proc1           B                                                                         
 D                 pi             5i 0                                                       
 D  piShutDown                     n   const                                                 
 D myIx            s              5i 0 based(myIx_p)                                         
 D myIx_p          s               *   static                                                

    // caller only wants to shutdown the proc        
    if (piShutDown and myIx_p <> *null);             
      dealloc myIx_p;                                
      return 0;                                      
    endif;                                           

    // allocate myIx if not yet allocated            
    if (myIx_p = *null);                             
      myIx_p = %alloc(%size(myIx));                  
    endif;                                           

    // increase the value by 1 as a test             
    myIx += 1;                                       

    return myIx;                                     

 P                 E                                 
  /end-free                                                                                          

答案就在您的评论中。你没有在 dealloc 之后设置 myIx_p = *null。未能将其设置为 *null 意味着它仍指向相同的内存位置,只是操作系统不再将其视为已分配。您所看到的行为完全是意料之中的。修复非常简单:

// caller only wants to shutdown the proc        
if (piShutDown and myIx_p <> *null);             
  dealloc myIx_p;
  myIx_p = *null;                                
  return 0;                                      
endif;                                           

您也可以按照IBMi文档这样解决:

// caller only wants to shutdown the proc        
if (piShutDown and myIx_p <> *null);             
  dealloc(n) myIx_p;
  return 0;                                      
endif; 

如果您不使用 (n),则必须像第一个示例一样将其设置为 *null。请在此处查看有关 dealloc 的文档:https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_74/rzasd/zzdeall.htm