malloc: 对象 0x10003b3c4 的 *** 错误:未分配正在释放的指针 *** 在malloc_error

malloc: *** error for object 0x10003b3c4: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug

本文关键字:malloc 释放 指针 error 对象 0x10003b3c4 错误 分配      更新时间:2023-10-16

我是编程C++新手,请耐心等待:)我的问题是模型(DGVM(运行到最后,但我收到的最后一条消息是"malloc:* 对象 0x10003b3c4 的错误:未分配正在释放的指针* 在malloc_error_break中设置断点以进行调试"。调试器指出这一点:

clTreePop::~clTreePop() {free(Trees);}

调试器指向free(Trees)并给出消息:"EXC_BAD_INSTRUCTION(代码=EXC_i386_INVOP,子代码=0x0("。我做错了什么?谢谢。

代码中可能对此问题很重要的部分:

void clTreePop::addFirstTree( double init_mass)
{
   clTree Tree( init_mass, -1. , pop_size_, count_trees_);
   Trees = (clTree *) malloc(sizeof(clTree));
   Trees[0] = Tree;
   pop_size_ ++;
   new_born_ ++;
   count_trees_ ++;
   root_biomass_  += Tree.getBr();
   stem_biomass_  += Tree.getBS();
   leaf_biomass_  += Tree.getBl();
   canopy_area_   += Tree.getCanopyArea();
   gc_weighted_   += Tree.getGc();
   max_height_    += MyMax(max_height_,Tree.getHeight());
   basal_area_    += Tree.getStemArea();
   return; }

首先C++ 您不需要使用 malloc,因为分配可以通过不同的、更好的方式完成,如果没有,至少更简单。Malloc 是一种旧的、低级的 C(不是C++(方式。尝试使用

clTree *Trees = new clTree;

您复制的代码没有完全显示情况,尽管我可以看到的是

Trees = (clTree *) malloc(sizeof(clTree));

您应该使用:

clTree *Trees = (clTree *) malloc(sizeof(clTree));

这样,您可以创建一个指针,然后您向其附加您分配的结构。

错误"EXC_BAD_INSTRUCTION(代码=EXC_i386_INVOP,子代码=0x0("表示您的代码与计算机体系结构(处理器、系统等(之间存在某种不兼容性。我不知道这件事,但我认为这是由我之前列出的错误引起的。