Функция не воспринимает ссылку на изображение

#include <iostream>
#include "time.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <string>
#include <experimental/filesystem>
#include <fstream>
#define STB_IMAGE_IMPLEMENTATION
#include "C:\Users\tpog\Documents\liberylis\stb_image.h"



__global__ void zrenie(int* itog_voz,unsigned char* data_gpu)
{
    itog_voz[blockIdx.x] += data_gpu[blockIdx.x]/10;
}

namespace fs = std::experimental::filesystem;



std::string wstring_to_utf8(const std::wstring& str)
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;    
    std::string string = myconv.to_bytes(str);
    return string;
}


int main(void)
{
    cudaSetDevice(4);
    std::vector<std::wstring> fileList;
    for (const fs::path& it : fs::directory_iterator("C:\\Users\\tpog\\Documents\\images")) {
        if (fs::is_regular_file(it) && (it.extension() == ".png")) {
            fileList.emplace_back(it.c_str());
        }
    }
    std::cout << wstring_to_utf8(fileList[4]);


    const char* string = wstring_to_utf8(fileList[4]).c_str();
    int w = 1200, h = 800;
    unsigned char* data = stbi_load("C:\\Users\\tpog\\Documents\\images\\picture_(1).png", &w, &h, nullptr, 3); // если написать 4 - загрузит с прозрачностью
    
    unsigned char* data_gpu;
    cudaMalloc((void**)&data_gpu, 3 * h * w * sizeof(char));
    printf("%i", data[3 * (1) + 0]);
    printf("%i ", data[3 * (1) + 1]);
    printf("%i ", data[3 * (1) + 2]);
    std::cout << clock();
    cudaMemcpy(data_gpu, data, 3 * h * w * sizeof(char), cudaMemcpyHostToDevice);
    //zrenie <<<w * h * 3, 1 >>>(data_gpu);
    stbi_image_free(data);
    cudaFree(data_gpu);


}

функция stbi_load("C:\\Users\\tpog\\Documents\\images\\picture_(1).png", &w, &h, nullptr, 3); получает адрес изображения, а после его обрабатывает. Проблема в том что когда я прописываю ссылку явно (вместо переменной string), то есть ‪C:\Users\tpog\Documents\images\picture_(1).png то всё прекрасно работает, но как только я сохраняю ссылку в переменную и передаю через неё то вдруг всё перестаёт работать, причём ошибок не возникает, код успешно завершается но строки 49, 50, 51 которые должны печатать цвет пикселя ничего не печатают

вывод когда ссылка указана не явно введите сюда описание изображения

вывод когда ссылка указана явно

введите сюда описание изображения


Ответы (1 шт):

Автор решения: Никита Самоуков

const char* string = wstring_to_utf8(fileList[4]).c_str();

Не работает. Объект строки уничтожается по окончании строки и в переменной ссылка на неизвестно что.

→ Ссылка