CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2017
    Posts
    50

    [RESOLVED] std::vector suddenly throw read access violation(Corrupted)

    I'm completely clueless what's going on here.Why it keep throwing read access violation.

    Name:  Untitled.jpg
Views: 1526
Size:  35.2 KB
    Here's the header file.

    Code:
    #pragma once
    
    #include "Renderer\VulkanApplication.h"
    #include "Renderer\VulkanRenderPass.h"
    #include "Renderer\VulkanGeneralBuffer.h"
    #include "Renderer\VulkanTexture.h"
    #include "Renderer\VulkanShaderManager.h"
    #include "GeometryGenerator.h"
    #include "Mouse.h"
    #include "Camera.h"
    
    class OneMillionCubesApplication :public VulkanApplication
    {
    	VkFramebufferArray mMSAAFramebuffers // it's std::vector<VkFramebuffer>;
    	VulkanRenderPass mMSAAColorRenderPass;
    	VulkanTexture mMSAAColorTexture;
    	VulkanTexture mMSAADepthTexture;
    	VkPipelineLayoutWrapper mPipelineLayout;
    	VkPipelineWrapper mColorPipeline;
    	VulkanGeneralBuffer mCubeVertexBuffer;
    	VulkanGeneralBuffer mCubeIndexBuffer;
    	Camera mFlyingCamera;
    
    public:
    	OneMillionCubesApplication();
    
    	void CreateRenderPasses() override;
    	
    	void CreateColorRenderTextures() override;
    
    	void CreateMeshes() override;
    
    	void CreatePipelines() override;
    
    	void OnResize() override;
    
    	void MainLoop() override;
    
    	void DrawFrame() override;
    
    	void UpdateCamera();
    };
    Here's the cpp file

    Code:
    #include "OneMilionCubesApplication.h"
    
    OneMillionCubesApplication::OneMillionCubesApplication()
    {
    	mAppName = "OneMillionCubesApplication";
    }
    
    void OneMillionCubesApplication::CreateRenderPasses() 
    {
    	VkAttachmentDescriptionArray attachementDescriptions(3);
    	attachementDescriptions[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
    	attachementDescriptions[0].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
    	attachementDescriptions[0].format = mSwapchainContext.GetVkFormat();
    	attachementDescriptions[0].samples = VK_SAMPLE_COUNT_8_BIT;
    	attachementDescriptions[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
    	attachementDescriptions[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
    	attachementDescriptions[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
    	attachementDescriptions[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
    
    	attachementDescriptions[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
    	attachementDescriptions[1].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
    	attachementDescriptions[1].format = mSwapchainContext.GetVkFormat();
    	attachementDescriptions[1].samples = VK_SAMPLE_COUNT_1_BIT;
    	attachementDescriptions[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
    	attachementDescriptions[1].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
    	attachementDescriptions[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
    	attachementDescriptions[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
    
    	attachementDescriptions[2].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
    	attachementDescriptions[2].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
    	attachementDescriptions[2].format = VK_FORMAT_D32_SFLOAT;
    	attachementDescriptions[2].samples = VK_SAMPLE_COUNT_8_BIT;
    	attachementDescriptions[2].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
    	attachementDescriptions[2].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
    	attachementDescriptions[2].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
    	attachementDescriptions[2].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
    
    	VkAttachmentReference attachemntReference[3] = {};
    	attachemntReference[0].attachment = 0;
    	attachemntReference[0].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
    
    	attachemntReference[1].attachment = 1;
    	attachemntReference[1].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
    
    	attachemntReference[2].attachment = 2;
    	attachemntReference[2].layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
    
    	VkSubpassDescriptionArray subpassDescription(1);
    	subpassDescription[0].colorAttachmentCount = 1;
    	subpassDescription[0].pColorAttachments = &attachemntReference[0];
    	subpassDescription[0].pResolveAttachments = &attachemntReference[1];
    	subpassDescription[0].pDepthStencilAttachment = &attachemntReference[2];
    
    	VulkanRenderPassInitInfo renderPassInitInfo = {};
    	renderPassInitInfo.attachmentDescriptions = attachementDescriptions;
    	renderPassInitInfo.subpassDescriptions = subpassDescription;
    
    	CHECK_RESULT(mMSAAColorRenderPass.Init(mpDeviceContext.get(), renderPassInitInfo));
    }
    
    void OneMillionCubesApplication::CreateColorRenderTextures()
    {
    	VulkanTextureInitInfo textureInitInfo = {};
    	textureInitInfo.dimension = mSwapchainContext.GetVkExtent2D();
    	textureInitInfo.format = mSwapchainContext.GetVkFormat();
    	textureInitInfo.sampleCount = VK_SAMPLE_COUNT_8_BIT;
    	textureInitInfo.usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
    
    	CHECK_RESULT(mMSAAColorTexture.Init(mpDeviceContext.get(), textureInitInfo));
    	CHECK_RESULT(mMSAAColorTexture.CreateVkImageView());
    
    	textureInitInfo.format = VK_FORMAT_D32_SFLOAT;
    	textureInitInfo.sampleCount = VK_SAMPLE_COUNT_8_BIT;
    	textureInitInfo.usageFlags = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
    
    	CHECK_RESULT(mMSAADepthTexture.Init(mpDeviceContext.get(), textureInitInfo));
    	CHECK_RESULT(mMSAADepthTexture.CreateDepthVkImageView());
    
    	mMSAAFramebuffers.clear();
    	mMSAAFramebuffers.resize(mSwapchainContext.GetImageCount());
    
    	VkImageViewArray framebufferAttachment(3);
    	framebufferAttachment[0] = mMSAAColorTexture.GetVkImageView();
    	framebufferAttachment[2] = mMSAADepthTexture.GetVkImageView();
    
    	for (uint i = 0; i < mMSAAFramebuffers.size(); ++i)
    	{
    		framebufferAttachment[1] = mSwapchainContext.GetVkImageView(i);
    		CHECK_RESULT(mMSAAColorRenderPass.CreateFramebuffer(framebufferAttachment, mSwapchainContext.GetVkExtent2D(), 1, &mMSAAFramebuffers[i]));
    	}
    }
    
    void OneMillionCubesApplication::CreateMeshes()
    {
    	float vertices[] =
    	{
    		-1.0f,1.0f,-1.0f,   1.0f,0.0f,0.0f,
    		1.0f,1.0f,-1.0f,    0.0f,1.0f,0.0f,
    		1.0f,-1.0f,-1.0f,   0.0f,0.0f,1.0f,
    		-1.0f,-1.0f,-1.0f,  1.0f, 0.5f, 0.25f,
    
    		1.0f,1.0f,-1.0f,  1.0f,0.0f,0.0f,
    		1.0f,1.0f,1.0f,   0.0f,1.0f,0.0f,
    		1.0f,-1.0f,1.0f,  0.0f,0.0f,1.0f,
    		1.0f,-1.0f,-1.0f, 1.0f, 0.5f, 0.25f,
    
    		1.0f,1.0f,1.0f,    1.0f,0.0f,0.0f,
    		-1.0f,1.0f,1.0f,   0.0f,1.0f,0.0f,
    		-1.0f,-1.0f,1.0f,  0.0f,0.0f,1.0f,
    		1.0f,-1.0f,1.0f,   1.0f, 0.5f, 0.25f,
    
    		-1.0f,1.0f,1.0f,    1.0f,0.0f,0.0f,
    		-1.0f,1.0f,-1.0f,   0.0f,1.0f,0.0f,
    		-1.0f,-1.0f,-1.0f,  0.0f,0.0f,1.0f,
    		-1.0f,-1.0f,1.0f,   1.0f, 0.5f, 0.25f,
    
    		-1.0f,1.0f,1.0f,   1.0f,0.0f,0.0f,
    		1.0f,1.0f,1.0f,    0.0f,1.0f,0.0f,
    		1.0f,1.0f,-1.0f,   0.0f,0.0f,1.0f,
    		-1.0f,1.0f,-1.0f,  1.0f, 0.5f, 0.25f,
    
    		-1.0f,-1.0f,-1.0f,  1.0f,0.0f,0.0f,
    		1.0f,-1.0f,-1.0f,   0.0f,1.0f,0.0f,
    		1.0f,-1.0f,1.0f,    0.0f,0.0f,1.0f,
    		-1.0f,-1.0f,1.0f,   1.0f, 0.5f, 0.25f,
    	};
    
    	uint indices[] =
    	{
    		0,1,2,
    		0,2,3,
    
    		4,5,6,
    		4,6,7,
    
    		8,9,10,
    		8,10,11,
    
    		12,13,14,
    		12,14,15,
    
    		16,17,18,
    		16,18,19,
    
    		20,21,22,
    		20,22,23,
    	};
    
    	VkBufferUsageFlags bufferUsage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
    	VkMemoryPropertyFlags memoryProperties = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
    
    	CHECK_RESULT(mCubeVertexBuffer.Init(mpDeviceContext.get(), sizeof(vertices), bufferUsage, memoryProperties));
    
    	bufferUsage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
    
    	CHECK_RESULT(mCubeIndexBuffer.Init(mpDeviceContext.get(), sizeof(indices), bufferUsage, memoryProperties));
    
    	VulkanStagingBufferManager stagingBufferManager;
    	CHECK_RESULT(stagingBufferManager.Init(mpDeviceContext.get()));
    
    	VulkanStagingBuffer* vertexStagingBuffer = stagingBufferManager.CreateStagingBuffer();
    	VulkanStagingBuffer* indexStagingBuffer = stagingBufferManager.CreateStagingBuffer();
    
    	CHECK_RESULT(vertexStagingBuffer->Init(sizeof(vertices)));
    	vertexStagingBuffer->OpenForDataUpload();
    	vertexStagingBuffer->DataUpload(vertices, 0, vertexStagingBuffer->GetSize());
    	vertexStagingBuffer->CloseForDataUpload();
    	vertexStagingBuffer->BeginVkCommandBuffer();
    	vertexStagingBuffer->RecordBufferCopy(mCubeVertexBuffer.GetVkBuffer());
    	vertexStagingBuffer->EndVkCommandBuffer();
    
    	CHECK_RESULT(indexStagingBuffer->Init(sizeof(indices)));
    	indexStagingBuffer->OpenForDataUpload();
    	indexStagingBuffer->DataUpload(indices, 0, indexStagingBuffer->GetSize());
    	indexStagingBuffer->CloseForDataUpload();
    	indexStagingBuffer->BeginVkCommandBuffer();
    	indexStagingBuffer->RecordBufferCopy(mCubeIndexBuffer.GetVkBuffer());
    	indexStagingBuffer->EndVkCommandBuffer();
    
    	stagingBufferManager.ExecuteTransfers();
    	stagingBufferManager.WaitIdle();
    }
    
    void OneMillionCubesApplication::OnResize()
    {
    	CHECK_VK_RESULT_2(vkDeviceWaitIdle(mpDeviceContext->GetVkDevice()));
    
    	mMSAAColorTexture.Purge();
    	mMSAADepthTexture.Purge();
    	mMSAAColorRenderPass.DestroyFramebuffers(mMSAAFramebuffers);
    
    	CreateColorRenderTextures();
    
    	float aspectRatio = (float)mSwapchainContext.GetVkExtent2D().width / (float)mSwapchainContext.GetVkExtent2D().height;
    
    	mFlyingCamera.Perspective(45.0f, aspectRatio, 0.1f, 1000.0f);
    }
    
    void OneMillionCubesApplication::CreatePipelines()
    {
    	VkPushConstantRange pushConstantRange = {};
    	pushConstantRange.offset = 0;
    	pushConstantRange.size = sizeof(glm::mat4) * 2;
    	pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
    
    	VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = {};
    	pipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
    	pipelineLayoutCreateInfo.setLayoutCount = 0;
    	pipelineLayoutCreateInfo.pSetLayouts = nullptr;
    	pipelineLayoutCreateInfo.pushConstantRangeCount = 1;
    	pipelineLayoutCreateInfo.pPushConstantRanges = &pushConstantRange;
    
    	mPipelineLayout.Set(mpDeviceContext->GetVkDevice());
    	CHECK_VK_RESULT_2(vkCreatePipelineLayout(mpDeviceContext->GetVkDevice(), &pipelineLayoutCreateInfo, nullptr, &mPipelineLayout));
    
    	VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = {};
    	inputAssemblyState.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
    	inputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
    
    	VkPipelineColorBlendAttachmentState colorBlendAttachement = {};
    	colorBlendAttachement.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
    
    	VkPipelineColorBlendStateCreateInfo colorBlendState = {};
    	colorBlendState.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
    	colorBlendState.attachmentCount = 1;
    	colorBlendState.pAttachments = &colorBlendAttachement;
    
    	VkVertexInputBindingDescription vertexInputBindingDescription = {};
    	vertexInputBindingDescription.binding = 0;
    	vertexInputBindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
    	vertexInputBindingDescription.stride = sizeof(float) * 6;
    
    	VkVertexInputAttributeDescription vertexInputAttributeDescription[2] = {};
    	vertexInputAttributeDescription[0].binding = 0;
    	vertexInputAttributeDescription[0].format = VK_FORMAT_R32G32B32_SFLOAT;
    	vertexInputAttributeDescription[0].location = 0;
    	vertexInputAttributeDescription[0].offset = 0;
    
    	vertexInputAttributeDescription[1].binding = 0;
    	vertexInputAttributeDescription[1].format = VK_FORMAT_R32G32B32_SFLOAT;
    	vertexInputAttributeDescription[1].location = 1;
    	vertexInputAttributeDescription[1].offset = sizeof(float) * 3;
    
    	VkPipelineVertexInputStateCreateInfo vertexInputState = {};
    	vertexInputState.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
    	vertexInputState.vertexBindingDescriptionCount = 1;
    	vertexInputState.pVertexBindingDescriptions = &vertexInputBindingDescription;
    	vertexInputState.vertexAttributeDescriptionCount = 2;
    	vertexInputState.pVertexAttributeDescriptions = vertexInputAttributeDescription;
    
    	VkPipelineRasterizationStateCreateInfo rasterizerStateCullBack = {};
    	rasterizerStateCullBack.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
    	rasterizerStateCullBack.frontFace = VK_FRONT_FACE_CLOCKWISE;
    	rasterizerStateCullBack.cullMode = VK_CULL_MODE_BACK_BIT;
    	rasterizerStateCullBack.polygonMode = VK_POLYGON_MODE_FILL;
    
    	VkPipelineRasterizationStateCreateInfo rasterizerStateCullFront = {};
    	rasterizerStateCullFront.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
    	rasterizerStateCullFront.frontFace = VK_FRONT_FACE_CLOCKWISE;
    	rasterizerStateCullFront.cullMode = VK_CULL_MODE_FRONT_BIT;
    	rasterizerStateCullFront.polygonMode = VK_POLYGON_MODE_FILL;
    
    	VkPipelineRasterizationStateCreateInfo rasterizerStateWireFrame = {};
    	rasterizerStateWireFrame.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
    	rasterizerStateWireFrame.frontFace = VK_FRONT_FACE_CLOCKWISE;
    	rasterizerStateWireFrame.cullMode = VK_CULL_MODE_NONE;
    	rasterizerStateWireFrame.polygonMode = VK_POLYGON_MODE_LINE;
    	rasterizerStateWireFrame.lineWidth = 3.0f;
    
    	VkPipelineMultisampleStateCreateInfo multisampleStateMSAA = {};
    	multisampleStateMSAA.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
    	multisampleStateMSAA.rasterizationSamples = VK_SAMPLE_COUNT_8_BIT;
    
    	VkPipelineMultisampleStateCreateInfo multisampleStateNoMSAA = {};
    	multisampleStateNoMSAA.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
    	multisampleStateNoMSAA.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
    
    	VkDynamicState dynamicStates[2];
    	dynamicStates[0] = VK_DYNAMIC_STATE_SCISSOR;
    	dynamicStates[1] = VK_DYNAMIC_STATE_VIEWPORT;
    
    	VkPipelineDynamicStateCreateInfo dynamicStateInfo = {};
    	dynamicStateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
    	dynamicStateInfo.dynamicStateCount = 2;
    	dynamicStateInfo.pDynamicStates = dynamicStates;
    
    	VkPipelineViewportStateCreateInfo viewportState = {};
    	viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
    	viewportState.viewportCount = 1;
    	viewportState.scissorCount = 1;
    
    	VkPipelineDepthStencilStateCreateInfo depthStencilState = {};
    	depthStencilState.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
    	depthStencilState.depthTestEnable = true;
    	depthStencilState.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
    	depthStencilState.depthWriteEnable = true;
    
    	VulkanShaderManager shaderManager;
    	shaderManager.Init(mpDeviceContext->GetVkDevice());
    
    	VkPipelineShaderStageCreateInfo msaaColorPipelineShaders[2] = {};
    	msaaColorPipelineShaders[0] = shaderManager.CreateVkShaderFromFile("Shaders/Color.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
    	CHECK_RESULT(msaaColorPipelineShaders[0].module);
    
    	msaaColorPipelineShaders[1] = shaderManager.CreateVkShaderFromFile("Shaders/Color.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
    	CHECK_RESULT(msaaColorPipelineShaders[1].module);
    
    	VkGraphicsPipelineCreateInfo graphicsPipelineCreateInfos[1] = {};
    	graphicsPipelineCreateInfos[0].sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
    	graphicsPipelineCreateInfos[0].layout = mPipelineLayout;
    	graphicsPipelineCreateInfos[0].pInputAssemblyState = &inputAssemblyState;
    	graphicsPipelineCreateInfos[0].pVertexInputState = &vertexInputState;
    	graphicsPipelineCreateInfos[0].pRasterizationState = &rasterizerStateCullBack;
    	graphicsPipelineCreateInfos[0].pMultisampleState = &multisampleStateMSAA;
    	graphicsPipelineCreateInfos[0].pDynamicState = &dynamicStateInfo;
    	graphicsPipelineCreateInfos[0].pDepthStencilState = &depthStencilState;
    	graphicsPipelineCreateInfos[0].stageCount = 2;
    	graphicsPipelineCreateInfos[0].pStages = msaaColorPipelineShaders;
    	graphicsPipelineCreateInfos[0].pColorBlendState = &colorBlendState;
    	graphicsPipelineCreateInfos[0].pViewportState = &viewportState;
    	graphicsPipelineCreateInfos[0].renderPass = mMSAAColorRenderPass.GetVkRenderPass();
    	graphicsPipelineCreateInfos[0].subpass = 0;
    	graphicsPipelineCreateInfos[0].flags = VK_PIPELINE_CREATE_ALLOW_DERIVATIVES_BIT;
    
    	CHECK_VK_RESULT_2(vkCreateGraphicsPipelines(mpDeviceContext->GetVkDevice(), VK_NULL_HANDLE, 1, graphicsPipelineCreateInfos, nullptr, &mColorPipeline));
    }
    
    void OneMillionCubesApplication::DrawFrame()
    {
    	mSwapchainContext.AcquireNextImage(mSemaphoreCollections[0].renderSignal);
    
    	VkSubmitInfo submitInfo = {};
    	submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
    	submitInfo.commandBufferCount = 1;
    	submitInfo.pCommandBuffers = &mCommandBuffers[0];
    	submitInfo.waitSemaphoreCount = 1;
    	submitInfo.pWaitSemaphores = &mSemaphoreCollections[0].renderSignal;
    	submitInfo.signalSemaphoreCount = 1;
    	submitInfo.pSignalSemaphores = &mSemaphoreCollections[0].presentSignal;
    
    	vkQueueSubmit(mGraphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
    
    	mSwapchainContext.Present(mSemaphoreCollections[0].presentSignal);
    
    	vkQueueWaitIdle(mGraphicsQueue);
    }
    
    void OneMillionCubesApplication::UpdateCamera()
    {
    	static bool firstTime = true;
    	static float lastMouseMotionX = 0.0f;
    	static float lastMouseMotionY = 0.0f;
    	static float yaw = 0.0f;
    	static float pitch = 0.0f;
    
    	Mouse::Pool();
    
    	if (firstTime)
    	{
    		lastMouseMotionX = Mouse::GetMouseMotionX();
    		lastMouseMotionY = Mouse::GetMouseMotionY();
    
    		firstTime = false;
    	}
    
    	yaw += Mouse::GetMouseMotionX() - lastMouseMotionX;
    	pitch += Mouse::GetMouseMotionY() - lastMouseMotionY;
    
    	lastMouseMotionX = Mouse::GetMouseMotionX();
    	lastMouseMotionY = Mouse::GetMouseMotionY();
    
    	if (pitch > 89.0f)
    		pitch = 89.0f;
    	else if (pitch < -89.0f)
    		pitch = -89.0f;
    
    	mFlyingCamera.Yaw(yaw);
    	mFlyingCamera.Pitch(pitch);
    }
    
    void OneMillionCubesApplication::MainLoop()
    {
    	VkClearValue clearValues[2] = {};
    	clearValues[0].color.float32[3] = 1.0f;
    	clearValues[1].depthStencil = { 1.0f, 0 };
    
    	VkCommandBufferBeginInfo commandBufferBeginInfo;
    	commandBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
    	commandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT;
    
    	VulkanRenderPassBeginInfo renderPassBeginInfo = {};
    	renderPassBeginInfo.clearValues = { clearValues[0],clearValues[0],clearValues[1] };
    	renderPassBeginInfo.renderArea = mSwapchainContext.GetVkExtent2D();
    	renderPassBeginInfo.subpassContents = VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS;
    
    	uint frameIndex = 0;
    
    	VkCommandBuffer commandBuffer = mCommandBuffers[0];
    
    	while (mRunning)
    	{
    		PeekWindowMessage();
    
    		vkBeginCommandBuffer(commandBuffer, &commandBufferBeginInfo);
    
    		std::cout << &mMSAAFramebuffers // HERE WHERE THE READ ACCESS VIOLATION HAPPENS;
    
    		renderPassBeginInfo.framebuffer = mMSAAFramebuffers[frameIndex];
    		mMSAAColorRenderPass.Begin(commandBuffer, renderPassBeginInfo);
    
    		VkViewport viewport = {};
    		viewport.width = (float)mSwapchainContext.GetVkExtent2D().width;
    		viewport.height = (float)mSwapchainContext.GetVkExtent2D().height;
    		viewport.maxDepth = 1.0f;
    
    		VkRect2D scissor = {};
    		scissor.extent = mSwapchainContext.GetVkExtent2D();
    
    		vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
    		vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
    
    		vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, mColorPipeline);
    
    		VkBuffer vertexBuffer = mCubeVertexBuffer.GetVkBuffer();
    		VkDeviceSize offsets = 0;
    
    		vkCmdBindVertexBuffers(commandBuffer, 0, 1, &vertexBuffer, &offsets);
    		vkCmdBindIndexBuffer(commandBuffer, mCubeIndexBuffer.GetVkBuffer(), 0, VK_INDEX_TYPE_UINT32);
    
    		vkCmdDrawIndexed(commandBuffer, 36, 1, 0, 0, 0);
    
    		vkCmdEndRenderPass(commandBuffer);
    		vkEndCommandBuffer(commandBuffer);
    
    		frameIndex = (frameIndex + 1) % mSwapchainContext.GetImageCount();
    
    		DrawFrame();
    	}
    }
    Thank you in advance
    Last edited by noobofcpp; October 24th, 2018 at 02:48 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: std::vector suddenly throw read access violation(Corrupted)

    Code:
    renderPassBeginInfo.framebuffer = mMSAAFramebuffers[frameIndex];
    Possibly because frameIndex is outside of the valid index range of mMSAAFramebuffers at the time of the issue.

    What is the value of frameIndex? How many elements are there in mMSAAFramebuffers? (mMSAAFramebuffers.size()) ?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Oct 2017
    Posts
    50

    Re: std::vector suddenly throw read access violation(Corrupted)

    The debugger show the frameIndex value is 0, while the mMSAAFramebuffers vector size is 2. I'm sure the problem is not that. It throws read access violation even i'm trying to print out the mMSAAFrambuffer address (std::cout << &mMSAAFramebuffers; )

  4. #4
    Join Date
    Feb 2017
    Posts
    677

    Re: std::vector suddenly throw read access violation(Corrupted)

    What if you initialize this structure to zero,

    VkCommandBufferBeginInfo commandBufferBeginInfo;

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: std::vector suddenly throw read access violation(Corrupted)

    It throws read access violation even i'm trying to print out the mMSAAFrambuffer address
    So it could be that the memory of the class instantiation is being overwritten - possibly set to 0 hence the read access violation.

    In the code posted, mMSAAFramebuffers occurs at

    Code:
    kFramebufferArray mMSAAFramebuffers // it's std::vector<VkFramebuffer>;
    ...
    mMSAAFramebuffers.clear();
    mMSAAFramebuffers.resize(mSwapchainContext.GetImageCount());
    ...
    for (uint i = 0; i < mMSAAFramebuffers.size(); ++i)
    {
        framebufferAttachment[1] = mSwapchainContext.GetVkImageView(i);
        CHECK_RESULT(mMSAAColorRenderPass.CreateFramebuffer(framebufferAttachment, mSwapchainContext.GetVkExtent2D(), 1, &mMSAAFramebuffers[i]));
    }
    ....
    mMSAAColorRenderPass.DestroyFramebuffers(mMSAAFramebuffers);
    ...
    std::cout << &mMSAAFramebuffers // HERE WHERE THE READ ACCESS VIOLATION HAPPENS;
    
    renderPassBeginInfo.framebuffer = mMSAAFramebuffers[frameIndex];
    I suggest you trace through from the class constructor onwards the value of the address of mMSAAFramebuffers and see that it is correct at the start and then you'll see when the value changes (which it shouldn't) so you'll then know what's causing it.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: std::vector suddenly throw read access violation(Corrupted)

    Pepper your code with static_assert() statements. Sometimes debugging code with a large number of items isn't always easy to find the issue. While debugging, let the code run and rely on multiple assert statements to trigger to let you know when something has gone wrong.

  7. #7
    Join Date
    Oct 2017
    Posts
    50

    Re: std::vector suddenly throw read access violation(Corrupted)

    Quote Originally Posted by wolle View Post
    What if you initialize this structure to zero,

    VkCommandBufferBeginInfo commandBufferBeginInfo;
    you're right mr wolle. That's actualy source of the problem. The vector object. distracted me from the the actual problem. Ahh how stupid am I. Thank you everyone for your time and effort to reply this thread. Now my project is complete.Name:  Untitled.jpg
Views: 1345
Size:  55.4 KB
    Last edited by noobofcpp; October 24th, 2018 at 02:38 AM.

  8. #8
    Join Date
    Feb 2017
    Posts
    677

    Re: std::vector suddenly throw read access violation(Corrupted)

    Quote Originally Posted by noobofcpp View Post
    Ahh how stupid am I.
    Well, no pain - no gain, as the saying goes. It means you are now a much better programmer!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured