C++ 删除指向动态分配对象的指针
C++ delete pointer to dynamic allocated object
我加载一个文本文件,然后根据它的数据创建动态分配的对象,然后将它们的指针存储在一个向量中,并根据每个对象类型将其存储在另外两个容器之一中,我有两个问题:
首先:如果我在读取文件函数中声明并初始化对象,然后将指针添加到向量中,那么在我删除它之前,该对象是否可以在函数外部使用?如果没有,解决方案是什么?
第二:我使用以下函数来释放内存:
for (int it = 0; it < phongItems.size(); it++) {
delete phongItems[it];
phongItems[it] = nullptr;
}
所以我从包含所有对象的主容器中删除对象,是否必须对具有相同指针的其他容器执行此操作?
注意:我在这一点上不是很强,所以如果有误会请澄清,希望少投反对票。
编辑:
我使用以下方法加载文本文件,我迭代它的行,然后每一行都应该创建一个项目,该项目必须添加到向量中以供以后使用:
void Game::loadLevel(std::string file) {
initAssets(mgr, dataPath, file);
std::string line;
ifstream f(dataPath + file);
if (!f.is_open())
LOGE("game error while opening file %s", file.c_str());
while (getline(f, line)) {
std::vector<std::string> tokens;
sSplit(line, ' ', tokens);
if (tokens[0] == "MESH") {
std::vector<std::string> typeToken;
sSplit(tokens[2], '=', typeToken);
std::vector<std::string> nameToken;
sSplit(tokens[1], '=', nameToken);
std::vector<std::string> countToken;
sSplit(tokens[3], '=', countToken);
std::vector<std::string> matrixToken;
sSplit(tokens[4], '=', matrixToken);
int count = atoi(countToken[1].c_str());
//if I declare the pointer here and added it to the vector I can't use it later
StaticItem* item;
std::vector<GLfloat> mToken;
fSplit(matrixToken[1], ',', mToken);
item = new StaticItem(*engine, "/models/" + nameToken[1] + ".obj",nullptr,0);
item->narrow = true;
engine->addItem(item, true, true);
}
}
if (f.bad())
LOGE("game error while reading file %s", file.c_str());
f.close();
}
void Engine::addItem(EngineItem* item, bool collision, bool loader) {
if (collision)
collisionItems.push_back(item);
if (loader)
loadItems.push_back(item);
phongItems.push_back(item);
}
如果我使用智能指针或原始指针,函数完成后指针将超出范围,这是什么想法?
许多问题合二为一,但我会尽力解释。
if I declare and initialize the object inside the read file function
then added the pointer to the vector, will this object be available
outside the function until I delete it or not?
是的,如果对象是在堆上创建的(使用 new),它将可以从提到的向量中获得。
so I delete the objects from the main container which contains all objects, must I do that for the other containers which have the same pointers or not?
删除或取消引用(使用)已删除的指针是错误的,您应该只从矢量中删除指针,以防止它被进一步使用。
if I use smart pointers or raw pointers the pointer will be out of
scope after the function finishes, so which is the idea?
是否要使用智能指针视情况而定。在 vector 中存储非智能指针是无用的,因为对象将在您指向时被删除。所以你应该把共享指针放在向量中,在这种情况下,直到任何共享指针存在,对象才会被删除。
总而言之,我敢打赌使用共享指针是最好的解决方案。创建共享指针,将其放入您需要的任何向量中。当你想删除对象时,只需从任何向量中删除共享对象就会自动删除。
我加载一个文本文件,然后根据它的数据创建动态分配的对象,然后将它们的指针存储在一个向量中,并根据每个对象类型将其存储在另外两个容器之一中,我有两个问题: 首先:如果我在读取文件函数中声明并初始化对象,然后将指针添加到向量中,那么在我删除它之前,该对象是否可以在函数外部使用?如果没有,解决方案是什么? 第二:我使用以下函数来释放内存:
for (int it = 0; it < phongItems.size(); it++) {
delete phongItems[it];
phongItems[it] = nullptr;
}
所以我从包含所有对象的主容器中删除对象,是否必须对具有相同指针的其他容器执行此操作?
注意:我在这一点上不是很强,所以如果有误会请澄清,希望少投反对票。
编辑:
我使用以下方法加载文本文件,我迭代它的行,然后每一行都应该创建一个项目,该项目必须添加到向量中以供以后使用:
void Game::loadLevel(std::string file) {
initAssets(mgr, dataPath, file);
std::string line;
ifstream f(dataPath + file);
if (!f.is_open())
LOGE("game error while opening file %s", file.c_str());
while (getline(f, line)) {
std::vector<std::string> tokens;
sSplit(line, ' ', tokens);
if (tokens[0] == "MESH") {
std::vector<std::string> typeToken;
sSplit(tokens[2], '=', typeToken);
std::vector<std::string> nameToken;
sSplit(tokens[1], '=', nameToken);
std::vector<std::string> countToken;
sSplit(tokens[3], '=', countToken);
std::vector<std::string> matrixToken;
sSplit(tokens[4], '=', matrixToken);
int count = atoi(countToken[1].c_str());
//if I declare the pointer here and added it to the vector I can't use it later
StaticItem* item;
std::vector<GLfloat> mToken;
fSplit(matrixToken[1], ',', mToken);
item = new StaticItem(*engine, "/models/" + nameToken[1] + ".obj",nullptr,0);
item->narrow = true;
engine->addItem(item, true, true);
}
}
if (f.bad())
LOGE("game error while reading file %s", file.c_str());
f.close();
}
void Engine::addItem(EngineItem* item, bool collision, bool loader) {
if (collision)
collisionItems.push_back(item);
if (loader)
loadItems.push_back(item);
phongItems.push_back(item);
}
如果我使用智能指针或原始指针,函数完成后指针将超出范围,这是什么想法?
许多问题合二为一,但我会尽力解释。
if I declare and initialize the object inside the read file function then added the pointer to the vector, will this object be available outside the function until I delete it or not?
是的,如果对象是在堆上创建的(使用 new),它将可以从提到的向量中获得。
so I delete the objects from the main container which contains all objects, must I do that for the other containers which have the same pointers or not?
删除或取消引用(使用)已删除的指针是错误的,您应该只从矢量中删除指针,以防止它被进一步使用。
if I use smart pointers or raw pointers the pointer will be out of scope after the function finishes, so which is the idea?
是否要使用智能指针视情况而定。在 vector 中存储非智能指针是无用的,因为对象将在您指向时被删除。所以你应该把共享指针放在向量中,在这种情况下,直到任何共享指针存在,对象才会被删除。
总而言之,我敢打赌使用共享指针是最好的解决方案。创建共享指针,将其放入您需要的任何向量中。当你想删除对象时,只需从任何向量中删除共享对象就会自动删除。