Load shader file from assets on android using AssetManager?

hi, i try to load my shader file from assets using ndk, all work fine but final char array contain random sybols at end, and the compiler can’t copile it, there is my function :

const char* getShaderSource(const char* src)
	AAsset* shaderAsset = AAssetManager_open(mgr,src, AASSET_MODE_UNKNOWN);

	if (mgr == NULL) {
		LOGE("mgr is null");

	size_t length = AAsset_getLength(shaderAsset);

	LOGI("Shader source size: %d
", length);

	char* buffer = (char*) malloc(sizeof(char)*length);

	AAsset_read(shaderAsset, buffer, length);

	LOGI("buffer source : %s
", buffer);


	return (buffer);


can you help me please.

Since this isn’t an OpenGL question, you probably have a better chance of getting an answer on an ndk (android?) development forum.
Without knowing anything about these functions my guess would be the shader string is not ‘\0’ terminated. You should allocate the buffer one byte larger than length and write ‘\0’ into the last position to get a valid C string.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.