Karmo Rosental
Karmo Rosental

Reputation: 652

C++ const struct pointer not accessible

I have const struct aiScene *scene; declared in class.

In the function where I define scene = importer.ReadFile(file, aiProcess_Triangulate); , the scene struct is accessible. I can print out scene->mNumMeshes for example.

Problem is that scene is not correclty accessible from another functions. If I try to print out scene->mNumMeshes then it prints different numbers every time (memory addresses?).

How can I make scene accessible from every function in class?

Upvotes: 1

Views: 395

Answers (1)

hmakholm left over Monica
hmakholm left over Monica

Reputation: 23342

It's impossible to be sure without knowing what's in importer.ReadFile, but a good guess would be that importer.ReadFile returns a pointer to a structure on the stack. After it returns, the structure will quickly get overwritten by other data -- in your first experiment you may just have gotten lucky to get at it before anything else reused that location.

Make sure that ReadFile returns a heap-allocated structure rather than a local one.

Upvotes: 2

Related Questions