给数组添加对象时报EXC_BAD_ACCESS

给数组添加对象时报EXC_BAD_ACCESS

int indiceCorrente=0; int differenza=delegate.reader.feedItems.count;

while(variable!=0){
switch(variable){

case 1: ----
case 2: ----

default: {

            NSMutableIndexSet *add=[[[NSMutableIndexSet alloc]init] autorelease];
            [add addIndex:indiceCorrente];
            [add addIndex:indiceCorrente+1];
            [add addIndex:indiceCorrente+2];
            [self aggiungiElementoArrayLettura:add];





            //[page addObject:@"makeLayout3"];
            NSMutableArray *pagina=[[NSMutableArray alloc]init];

            indiceCorrente=indiceCorrente+1;
            [pagina addObject:indiceCorrente]; <------ EXC_BAD_ACCESS WHY????
            indiceCorrente=indiceCorrente+1;
            [pagina addObject:indiceCorrente];
            indiceCorrente=indiceCorrente+1;
            [pagina addObject:indiceCorrente];


            [pages addObject:pagina];

        }

}
}

EXC_BAD_ACCESS 是一个内存访问错误的异常,这意味着程序试图访问无效的内存地址。


这个错误可能是由于你尝试访问一个被释放的对象或者一个未初始化的指针所引起的。你应该检查你的代码,确保你的对象和指针在使用时是有效的。


在你的代码中,我看到你使用了 autorelease 来释放 add 对象。这可能是导致问题的原因之一,因为你在后面的代码中仍然使用了这个对象。你应该考虑改为使用 alloc 和 init 来创建和初始化这个对象,然后在不再使用时使用 release 来释放它。


你还应该检查一下 aggiungiElementoArrayLettura 方法,看看是否有任何对象被释放两次。


如果这些建议不能解决你的问题,你可以尝试在调试时使用断点,看看程序在哪里引发了异常。这可以帮助你更精确地定位问题所在。