[Solved] Accessing struct member through unique_ptr gives segmentation fault [closed]


First, std::unique_ptr is a class not a struct, so get rid of the struct prefix on the pdfInfo variable declaration. You were probably thinking of this instead:

std::unique_ptr<struct Canvas::LoadedPDFInfo> pdfInfo;

But even when declaring variables (or type-casting) using actual struct types, you still do not need the struct prefix. C needs that, C++ does not.

Second, your segfault is happening because you have merely declared the pdfInfo variable, but it is not actually pointing at a valid LoadedPDFInfo object, so using the -> operator is not a valid operation. Just like a regular pointer, std::unique_ptr (and std::auto_ptr, and std::shared_ptr) have to point at something in order to access that something’s members. For example:

std::unique_ptr<Canvas::LoadedPDFInfo> pdfInfo(new Canvas::LoadedPDFInfo);

3

solved Accessing struct member through unique_ptr gives segmentation fault [closed]