我有一个基本的库,我用它来绘制OpenGL文本,当我使用valgrind确保它是气密的.我一直得到一个不寻常的错误,看起来好像linux c ++库有问题.我想看看你们是否可以发现我的错误或认证我担心的问题,那就是我的c ++库有问题并且需要更换.代码非常简单,但它同时使用OpenGL和FreeImage,因此某些行没有意义.
这是fontsystem.h:
#ifndef FONTSYSTEM_H
#define FONTSYSTEM_H
/*
The Font System works by loading all the font images (max image size 32px^2) into memory and storing
the OpenGL texture ID's into an array that can be access at all times. The DrawString() functions will
search through the string for the specified character requested to draw and then it will draw a quad
and paint the texture on it. Since all the images are pre-loaded, no loading of the …Run Code Online (Sandbox Code Playgroud) 我想知道(在 C++ 中)您是否可以实例化一个类(类 foo)然后说类返回已经实例化的对象。(foo::instance())
换句话说,我可以让一个类通过它自己的方法返回它自己吗?我希望能够在我的程序早期创建一个类(即类 foo),这样它就已经设置好了,可以开始使用了。然后,更进一步,我希望能够从该类调用函数,而不必将该对象作为参数传递给我的调用函数。我可以这样做吗:
MyClass::ReturnSelf()->foo();
或
MyClass::ReturnSelf().foo();
编辑:我刚刚意识到这可能有点不清楚。我希望能够让另一个类调用这个“自返回”方法,这样它就可以使用已经实例化的对象的方法和成员,而无需创建新对象。
我有另一个valgrind错误,我似乎无法弄清楚.我有一些代码加载脚本然后使用strtok()来标记已加载到缓冲区的所述脚本中的数据.我使用以下内容在堆上为我的缓冲区分配内存:
char *pPngStr = new char[4096];
Run Code Online (Sandbox Code Playgroud)
然后在函数结束时,我使用以下内容释放内存:
delete[] pPngStr;
Run Code Online (Sandbox Code Playgroud)
Valgrind报告分配如下:
173 bytes in 1 blocks are definitely lost in loss record 3,753 of 4,627
Run Code Online (Sandbox Code Playgroud)
Valgrind还报告我释放内存的行:
Invalid free() / delete / delete[] / realloc()
Run Code Online (Sandbox Code Playgroud)
这一切都在同一个函数中,变量只对那个函数本地而不是类.我对valgrind如何认为这是一个内存泄漏感到茫然.我正在分配我需要的所有内存,然后当我完成时我将它释放.我不知道问题是什么.
这是完整的功能(可能有点乱):
void CSprite::LoadSprite()
{
FILE *pF = fopen("Data/Art/coin/coin.sprite", "rb");
if(!pF)
{
return;
}
fseek(pF, 0, SEEK_END);
int length = ftell(pF);
fseek(pF, 0, SEEK_SET);
char *aData = new char[length];
fread(aData, 1, length, pF);
CheckGLError();
/**
* The scripts are setup like so:
***********************************************
* Pngs:
* 4; …Run Code Online (Sandbox Code Playgroud)