通过一个shared_ptr访问calloc'd数据 [英] Accessing calloc'd data through a shared_ptr
问题描述
我试图访问我美元,在释放calloc
方法通过一个shared_ptr pviously分配p $的数据。出于某种原因,我不能访问它(保持与 EXC_BAD_ACCESS
崩溃)在 glTexImage2D
(我的$的最后一行C $ç段)。
I'm trying to access the data that I previously allocated with the calloc
method through a shared_ptr. For some reason I can't access it (keeps on crashing with EXC_BAD_ACCESS
) on glTexImage2D
(last line of my code snippets).
我UTIL方法加载数据:
My util method to load the data:
shared_ptr<ImageData> IOSFileSystem::loadImageFile(string path) const
{
// Result
shared_ptr<ImageData> result = shared_ptr<ImageData>();
...
// Check if file exists
if([[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:NO])
{
...
GLubyte *spriteData = (GLubyte*) calloc(width * height * 4, sizeof(GLubyte));
...
// Put result in shared ptr
shared_ptr<GLubyte> spriteDataPtr = shared_ptr<GLubyte>(spriteData);
result = shared_ptr<ImageData>(new ImageData(path, width, height, spriteDataPtr));
}
else
{
cout << "IOSFileSystem::loadImageFile -> File does not exist at path.\nPath: " + path;
exit(1);
}
return result;
}
头为的ImageData code>:
class ImageData
{
public:
ImageData(string path, int width, int height, shared_ptr<GLubyte> data);
~ImageData();
string getPath() const;
int getWidth() const;
int getHeight() const;
shared_ptr<GLubyte> getData() const;
private:
string path;
int width;
int height;
shared_ptr<GLubyte> data;
};
文件调用的Util类:
File that calls the util class:
void TextureMaterial::load()
{
shared_ptr<IFileSystem> fileSystem = ServiceLocator::getFileSystem();
shared_ptr<ImageData> imageData = fileSystem->loadImageFile(path);
this->bind(imageData);
}
void TextureMaterial::bind(shared_ptr<ImageData> data)
{
// Pointer to pixel data
shared_ptr<GLubyte> pixelData = data->getData();
...
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data->getWidth(), data->getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, &pixelData);
}
只是为了记录:如果我扔掉了所有的shared_ptr的我能够访问数据。签名 glTexImage2D
:
void glTexImage2D(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *data);
其他问题:通常你必须释放(spriteData),但因为我给的数据一个shared_ptr,会当的shared_ptr被删除的数据free'd
Additional question: normally you have to free(spriteData) but since I gave the data to a shared_ptr, will the data be free'd when the shared_ptr is removed?
推荐答案
我觉得这是你的问题:
..., &pixelData);
您正在一个局部变量的地址(类型为的shared_ptr&LT; GLubyte&GT;
),它是默默转换为无效*
,而不是得到从它的指针。其替换为:
You are taking an address of a local variable (of type shared_ptr<GLubyte>
), which is silently cast to void*
, instead of getting the pointer from it. Replace it with:
..., pixelData.get());
这篇关于通过一个shared_ptr访问calloc'd数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!