Fatal signal 11 (SIGSEGV)

First of all thanks for all this effort, I've been using Dcraw for a while, and it's nice to have a simple threaded implementation.

I'm trying to call Libraw through JNI. I can pass hundreds of images and decode perfectly fine with this snippet:

	LibRaw RawProcessor;
	int result;
 
	jsize len = env->GetArrayLength(bufferBytes);
	jbyte* buffer = env->GetByteArrayElements(bufferBytes, 0);
 
	result = RawProcessor.open_buffer(buffer, len);
	if (LIBRAW_FATAL_ERROR(result))
		return NULL;
	result = RawProcessor.unpack_thumb();
	if (LIBRAW_FATAL_ERROR(result))
		return NULL;
	libraw_processed_image_t *image = RawProcessor.dcraw_make_mem_thumb(&result);
	if (LIBRAW_FATAL_ERROR(result))
		return NULL;
	jbyteArray thumb = env->NewByteArray(image->data_size);
	env->SetByteArrayRegion(thumb, 0, image->data_size, (jbyte *) image->data);
	env->ReleaseByteArrayElements(bufferBytes, buffer, 0);
	RawProcessor.dcraw_clear_mem(image);
	RawProcessor.recycle();
	return thumb;

However when I send a non-image it crashes out with Fatal Signal 11 (SIGSEGV). So it appears I have a memory issue, but ONLY when it's not an image file. I thought I followed the examples for when there is a failure, but I guess I'm missing something. How should I be handling memory when the open_buffer throws an error?

Forums: 

I've also created a test with

I've also created a test with various releases for the byte array data, but always the same error on non image files:

	LibRaw RawProcessor;
	jsize len = env->GetArrayLength(bufferBytes);
	//jbyte* buffer = env->GetByteArrayElements(bufferBytes, 0);
	jbyte* buffer;
	env->GetByteArrayRegion(bufferBytes, 0, len, buffer);
	int result = RawProcessor.open_buffer(buffer, len);
	//env->ReleaseByteArrayElements(bufferBytes, buffer, 0);
	if (result == 0)
		RawProcessor.recycle();

For non-image file LibRaw

For non-image file LibRaw will return LIBRAW_FILE_UNSUPPORTED return code.
This error is not 'fatal', so LIBRAW_FATAL_ERROR is 'false' for this return code.

It is better to compare return code with LIBRAW_SUCCESS.

dcraw_make_mem_thumb() will return NULL pointer to image variable, so image->data_size will result to memory access error.

-- Alex Tutubalin @LibRaw LLC

Thanks for the quick response

Thanks for the quick response. In the case of the second segment of code, I was trying to create a "canDecode" method to test with. I was still getting the memory error there. Is there anything I must do to clean up after an open_buffer call fails?

You may call LibRaw::recycle(

You may call LibRaw::recycle()
You don't *need* to do this because recycle() is called in the beginning of each open_buffer().

-- Alex Tutubalin @LibRaw LLC

Fixed everything and it was

Fixed everything and it was completely user error. The calls were threaded and hence the logging was misleading as to where the crashing occurred.

Taught myself coding mainly in Java/C#, so memory issues in c are over my head. Sorry to bother you with silly issues.

Now that it's working however I just wanted to say it's running great and extremely fast. Previously I was running threads off separate dcraw library builds (ghetto, but it let me thread). This is much much cleaner and faster to boot. Thanks!!!