Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

c++ - Acces violation when defining an array on stack

An odd error appears after a function call if an array of VkQueueFamilyProperties is defined on a stack instead of on the heap. I do not understand why this would happen since the variable is in no way related to the given function call.

I have the following code:

#include <stdlib.h>
#include <vulkan/vulkan.h>
#include <stdio.h>
#include <GLFW/glfw3.h>

GLFWwindow* window;

void main(){
    glfwInit();
    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);

    window = glfwCreateWindow(1280, 720, "tArh", 0, 0);

    VkInstance instance;

    VkApplicationInfo info;
    info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
    info.pApplicationName = "tArh";
    info.applicationVersion = VK_MAKE_VERSION(0, 0, 1);
    info.pEngineName = "Tarh";
    info.engineVersion = VK_MAKE_VERSION(0, 0, 1);
    info.apiVersion = VK_API_VERSION_1_0;

    VkInstanceCreateInfo cinfo;
    cinfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    cinfo.pApplicationInfo = &info;
    cinfo.enabledLayerCount = 0;
    cinfo.enabledExtensionCount = 0;
    cinfo.ppEnabledExtensionNames = 0;

    //ERROR OCCURS AFTER THIS LINE IF props[2] is defined below
    //The code after this line does not get executed
    int fail = vkCreateInstance(&cinfo, 0, &instance);

    if(fail)
        printf("Vulkan Failed
");

    VkPhysicalDevice device = 0;
    uint count = 1;

    //Works fine
    // VkQueueFamilyProperties* props = (VkQueueFamilyProperties*) malloc(sizeof(VkQueueFamilyProperties) * 2);

    //Causes an error to occur at vkCreateInstance
    VkQueueFamilyProperties props[2];

    //Needs to be defined for the error to occur
    vkGetPhysicalDeviceQueueFamilyProperties(device, &count, props);
}

It is build using CL on windows for x64 architectures using the following build script

$SOURCES = ls src -Recurse -Filter *.cpp | % { $_.FullName}

cl /Zi /EHsc `
/Fe: build\window.exe `
/Fo: build\ `
/Fd: build\vc140.pdb `
/FI arharh.h `
/I C:UsersDemiDesktopFilesInstallsvcpkginstalledx64-windowsinclude `
/I C:UsersDemiDesktopFilesInstallsimgui `
/I "C:VulkanSDK1.2.162.1Include" `
/I ./inc `
@SOURCES `
vulkan-1.lib `
glfw3dll.lib `
/link `
/LIBPATH:"C:UsersDemiDesktopFilesInstallsvcpkginstalledx64-windowslib" `
/LIBPATH:"C:VulkanSDK1.2.162.1Lib"

The following error appears right after the call to vkCreateInstance after which the debugger halts and no more code is executed.

Exception has occurred: W32/0xc0000005
Unhandled exception at 0x00007FFDD6ACD240 (vulkan-1.dll) in window.exe: 0xC0000005: Access violation reading location 0x00000000FFFFFFFF.
question from:https://stackoverflow.com/questions/65863350/acces-violation-when-defining-an-array-on-stack

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...