stbi_image_free throwing IllegalArgumentException during runtime randomly

Started by Animus_Surge, December 08, 2019, 05:06:45

Previous topic - Next topic

Animus_Surge

I don't know how to fix this issue, even after about an hour of extensive searching. My application works fine now, extended to about three minutes, then crashes with an actual error, along with a few random byte leaks which don't really affect runtime (except for one). The code is the following:

package com.programwings.wofrpg.display.drawable;

import java.awt.*;
import java.io.File;
import static org.lwjgl.opengl.GL11.*;

import com.programwings.wofrpg.display.Primary;
import com.programwings.wofrpg.util.Bounds;
import com.programwings.wofrpg.util.Coordinate;
import com.programwings.wofrpg.util.logcat.LogCat;
import org.lwjgl.BufferUtils;
import org.lwjgl.stb.STBImage;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.Properties;

public class Image {

	private int id, width, height;
	private Coordinate c1, c2, c3, c4;
	private String file;
	private File f;

	public Image(String filename) {
		file = filename;
		f = new File(filename);
		if(f.exists() && !f.equals(null)) {
			this.c1 = new Coordinate(1, 1);
			this.c2 = new Coordinate(-1, 1);
			this.c3 = new Coordinate(-1, -1);
			this.c4 = new Coordinate(1, -1);

			IntBuffer wid = BufferUtils.createIntBuffer(1);
			IntBuffer hei = BufferUtils.createIntBuffer(1);
			IntBuffer com = BufferUtils.createIntBuffer(1);

			ByteBuffer data = STBImage.stbi_load(filename, wid, hei, com, 4);

			id = glGenTextures();
			width = wid.get();
			height = hei.get();
			bind();

			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
			try {
				STBImage.stbi_image_free(data); //this line right here is the main problem
			} catch (NullPointerException e) {
				new LogCat("image").log("Image became null [[java.lang.NullPointerException:ln 50]]", LogCat.ERROR);
			}
		}
		else {
			new LogCat("images").log("File \"" + filename + "\" does not exist or cannot be read. [[java.io.IOException::ln 23]]", LogCat.ERROR);
		}
	}

	public Image(String filename, Coordinate c1, Coordinate c2, Coordinate c3, Coordinate c4) {
		file = filename;
		f = new File(file);
		if(f.exists() && !f.equals(null)) {
			file = filename;
			this.c1 = c1;
			this.c2 = c2;
			this.c3 = c3;
			this.c4 = c4;

			IntBuffer wid = BufferUtils.createIntBuffer(1);
			IntBuffer hei = BufferUtils.createIntBuffer(1);
			IntBuffer com = BufferUtils.createIntBuffer(1);

			ByteBuffer data = STBImage.stbi_load(filename, wid, hei, com, 4);

			id = glGenTextures();
			width = wid.get();
			height = hei.get();
			bind();

			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
			STBImage.stbi_image_free(data); //this line right here is the main problem
		}
	}

	public void bind() {
		if(new File(file).exists())
		glBindTexture(GL_TEXTURE_2D, id);
	}

	public void unbind() {
		if(new File(file).exists())
		glBindTexture(GL_TEXTURE_2D, 0);
	}

	@SuppressWarnings("unused")
	public void setFileName(String fileName) {
		this.file = fileName;
	}

	public void draw() {
		bind();
		glEnable(GL_TEXTURE_2D);
		glBegin(GL_QUADS);
			glTexCoord2d(1, 0);
			glVertex2d(c1.x, c1.y);
			glTexCoord2d(0, 0);
			glVertex2d(c2.x, c2.y);
			glTexCoord2d(0, 1);
			glVertex2d(c3.x, c3.y);
			glTexCoord2d(1, 1);
			glVertex2d(c4.x, c4.y);
		glEnd();
		unbind();
	}

	public boolean wasClicked(double px, double py) {

		//Bounds b = new Bounds(c1, c2, c3, c4);
		//System.out.println(b.getBounds().getW() + ", " + b.getBounds().getH());

		if((px >= c1.x && px <= c2.x) && (py >= c3.x && py <= c2.y)) {
			System.out.println("Button clicked!");
			return true;
		}
		return false;
	}

}


I marked the lines that are problematic. Is there any way to fix the following stacktrace error?

Exception in thread "main" java.lang.IllegalArgumentException: Argument for 1. parameter 'retval_from_stbi_load' must not be null
	at org.lwjglx.debug.RT.checkNotNull(RT.java:791)
	at org.lwjglx.debug.$Proxy$16.stbi_image_free28(Unknown Source)
	at com.programwings.wofrpg.display.drawable.Image.<init>(Image.java:50)
	at com.programwings.wofrpg.display.screens.MainMenu.displayMainMenu(MainMenu.java:18)
	at com.programwings.wofrpg.display.Primary.loop(Primary.java:117)
	at com.programwings.wofrpg.display.Primary.run(Primary.java:35)
	at com.programwings.wofrpg.Runnable.run(Runnable.java:14)
	at com.programwings.wofrpg.Runnable.main(Runnable.java:19)


Here's the full log for the test (used a debugger with it):

{[filesys|INFO   ]: Settings loaded.}
[LWJGL] Version: 3.2.3 build 13
[LWJGL] 	 OS: Windows 10 v10.0
[LWJGL] 	JRE: 13.0.1 amd64
[LWJGL] 	JVM: Java HotSpot(TM) 64-Bit Server VM v13.0.1+9 by Oracle Corporation
[LWJGL] Loading JNI library: lwjgl
[LWJGL] 	Module: org.lwjgl
[LWJGL] 	Using SharedLibraryLoader...
[LWJGL] 	Found at: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\lwjgl.dll
[LWJGL] 	Found at: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\lwjgl.dll
[LWJGL] 	Loaded from org.lwjgl.librarypath: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\lwjgl.dll
[LWJGL] Loading library: glfw
[LWJGL] 	Module: org.lwjgl.glfw
[LWJGL] 	Using SharedLibraryLoader...
[LWJGL] 	Found at: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\glfw.dll
[LWJGL] Java 9 stack walker enabled
[LWJGL] 	Loaded from org.lwjgl.librarypath: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\glfw.dll
[LWJGL] Loading library: jemalloc
[LWJGL] 	Module: org.lwjgl.jemalloc
[LWJGL] 	Using SharedLibraryLoader...
[LWJGL] 	Found at: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\jemalloc.dll
[LWJGL] 	Loaded from org.lwjgl.librarypath: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\jemalloc.dll
[LWJGL] MemoryUtil allocator: DebugAllocator
{[window|INFO   ]: GLFW initialized successfully}
{[window|INFO   ]: Window created. Initializing callbacks...}
{[window|DEBUG  ]: Callbacks initialized}
[LWJGL] Loading JNI library: lwjgl_opengl
[LWJGL] 	Module: org.lwjgl.opengl
[LWJGL] 	Using SharedLibraryLoader...
[LWJGL] 	Found at: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\lwjgl_opengl.dll
[LWJGL] 	Loaded from org.lwjgl.librarypath: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\lwjgl_opengl.dll
[LWJGL] Loading library: opengl32
[LWJGL] 	Module: org.lwjgl.opengl
[LWJGL] 	opengl32.dll not found in org.lwjgl.librarypath=C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13
[LWJGL] 	Loaded from system paths: C:\WINDOWS\SYSTEM32\opengl32.dll
[LWJGL] Java 10 multiplyHigh enabled
[LWJGL] Java 9 check intrinsics enabled
[LWJGL] Java 10 memcpy enabled
[LWJGL] [GL] Using OpenGL 4.3 for error logging.
[LWJGL] Java 9 text decoding enabled
[info ][1] Initialized OpenGL context for window[1]
  Effective OpenGL version: 4.5
  OpenGL version string   : 4.5.0 - Build 24.20.100.6292
  OpenGL vendor           : Intel
  OpenGL renderer         : Intel(R) HD Graphics 620
  GL_MAX_VERTEX_ATTRIBS   : 16
  Capabilities:
        1      GL_AMD_depth_clamp_separate GL_AMD_vertex_shader_layer 
        2      GL_AMD_vertex_shader_viewport_index GL_ARB_arrays_of_arrays 
        3      GL_ARB_base_instance GL_ARB_bindless_texture GL_ARB_blend_func_extended 
        4      GL_ARB_buffer_storage GL_ARB_cl_event GL_ARB_clear_buffer_object 
        5      GL_ARB_clear_texture GL_ARB_clip_control GL_ARB_color_buffer_float 
        6      GL_ARB_compatibility GL_ARB_compressed_texture_pixel_storage 
        7      GL_ARB_compute_shader GL_ARB_conditional_render_inverted 
        8      GL_ARB_conservative_depth GL_ARB_copy_buffer GL_ARB_copy_image 
        9      GL_ARB_cull_distance GL_ARB_debug_output GL_ARB_depth_buffer_float 
       10      GL_ARB_depth_clamp GL_ARB_depth_texture GL_ARB_derivative_control 
       11      GL_ARB_direct_state_access GL_ARB_draw_buffers GL_ARB_draw_buffers_blend 
       12      GL_ARB_draw_elements_base_vertex GL_ARB_draw_indirect GL_ARB_draw_instanced 
       13      GL_ARB_enhanced_layouts GL_ARB_ES2_compatibility GL_ARB_ES3_1_compatibility 
       14      GL_ARB_ES3_compatibility GL_ARB_explicit_attrib_location 
       15      GL_ARB_explicit_uniform_location GL_ARB_fragment_coord_conventions 
       16      GL_ARB_fragment_layer_viewport GL_ARB_fragment_program 
       17      GL_ARB_fragment_program_shadow GL_ARB_fragment_shader 
       18      GL_ARB_fragment_shader_interlock GL_ARB_framebuffer_no_attachments 
       19      GL_ARB_framebuffer_object GL_ARB_framebuffer_sRGB GL_ARB_geometry_shader4 
       20      GL_ARB_get_program_binary GL_ARB_get_texture_sub_image GL_ARB_gpu_shader5 
       21      GL_ARB_gpu_shader_fp64 GL_ARB_half_float_pixel GL_ARB_half_float_vertex 
       22      GL_ARB_indirect_parameters GL_ARB_instanced_arrays GL_ARB_internalformat_query 
       23      GL_ARB_internalformat_query2 GL_ARB_invalidate_subdata 
       24      GL_ARB_map_buffer_alignment GL_ARB_map_buffer_range GL_ARB_multi_bind 
       25      GL_ARB_multi_draw_indirect GL_ARB_multisample GL_ARB_multitexture 
       26      GL_ARB_occlusion_query GL_ARB_occlusion_query2 GL_ARB_pixel_buffer_object 
       27      GL_ARB_point_parameters GL_ARB_point_sprite GL_ARB_polygon_offset_clamp 
       28      GL_ARB_post_depth_coverage GL_ARB_program_interface_query 
       29      GL_ARB_provoking_vertex GL_ARB_query_buffer_object 
       30      GL_ARB_robust_buffer_access_behavior GL_ARB_robustness GL_ARB_sample_shading 
       31      GL_ARB_sampler_objects GL_ARB_seamless_cube_map 
       32      GL_ARB_seamless_cubemap_per_texture GL_ARB_separate_shader_objects 
       33      GL_ARB_shader_atomic_counter_ops GL_ARB_shader_atomic_counters 
       34      GL_ARB_shader_bit_encoding GL_ARB_shader_draw_parameters 
       35      GL_ARB_shader_group_vote GL_ARB_shader_image_load_store 
       36      GL_ARB_shader_image_size GL_ARB_shader_objects GL_ARB_shader_precision 
       37      GL_ARB_shader_stencil_export GL_ARB_shader_storage_buffer_object 
       38      GL_ARB_shader_subroutine GL_ARB_shader_texture_image_samples 
       39      GL_ARB_shading_language_100 GL_ARB_shading_language_420pack 
       40      GL_ARB_shading_language_packing GL_ARB_shadow GL_ARB_stencil_texturing 
       41      GL_ARB_sync GL_ARB_tessellation_shader GL_ARB_texture_barrier 
       42      GL_ARB_texture_border_clamp GL_ARB_texture_buffer_object 
       43      GL_ARB_texture_buffer_object_rgb32 GL_ARB_texture_buffer_range 
       44      GL_ARB_texture_compression GL_ARB_texture_compression_bptc 
       45      GL_ARB_texture_compression_rgtc GL_ARB_texture_cube_map 
       46      GL_ARB_texture_cube_map_array GL_ARB_texture_env_add GL_ARB_texture_env_combine 
       47      GL_ARB_texture_env_crossbar GL_ARB_texture_env_dot3 GL_ARB_texture_float 
       48      GL_ARB_texture_gather GL_ARB_texture_mirror_clamp_to_edge 
       49      GL_ARB_texture_mirrored_repeat GL_ARB_texture_multisample 
       50      GL_ARB_texture_non_power_of_two GL_ARB_texture_query_levels 
       51      GL_ARB_texture_query_lod GL_ARB_texture_rectangle GL_ARB_texture_rg 
       52      GL_ARB_texture_rgb10_a2ui GL_ARB_texture_stencil8 GL_ARB_texture_storage 
       53      GL_ARB_texture_storage_multisample GL_ARB_texture_swizzle GL_ARB_texture_view 
       54      GL_ARB_timer_query GL_ARB_transform_feedback2 GL_ARB_transform_feedback3 
       55      GL_ARB_transform_feedback_instanced GL_ARB_transpose_matrix 
       56      GL_ARB_uniform_buffer_object GL_ARB_vertex_array_bgra 
       57      GL_ARB_vertex_array_object GL_ARB_vertex_attrib_64bit 
       58      GL_ARB_vertex_attrib_binding GL_ARB_vertex_buffer_object GL_ARB_vertex_program 
       59      GL_ARB_vertex_shader GL_ARB_vertex_type_10f_11f_11f_rev 
       60      GL_ARB_vertex_type_2_10_10_10_rev GL_ARB_viewport_array GL_ARB_window_pos 
       61      GL_EXT_abgr GL_EXT_bgra GL_EXT_blend_color GL_EXT_blend_equation_separate 
       62      GL_EXT_blend_func_separate GL_EXT_blend_minmax GL_EXT_blend_subtract 
       63      GL_EXT_clip_volume_hint GL_EXT_compiled_vertex_array GL_EXT_direct_state_access 
       64      GL_EXT_draw_buffers2 GL_EXT_framebuffer_blit GL_EXT_framebuffer_multisample 
       65      GL_EXT_framebuffer_object GL_EXT_geometry_shader4 GL_EXT_gpu_program_parameters 
       66      GL_EXT_gpu_shader4 GL_EXT_packed_depth_stencil GL_EXT_packed_float 
       67      GL_EXT_polygon_offset_clamp GL_EXT_secondary_color 
       68      GL_EXT_shader_framebuffer_fetch GL_EXT_shader_integer_mix GL_EXT_shadow_funcs 
       69      GL_EXT_stencil_two_side GL_EXT_stencil_wrap GL_EXT_texture_array 
       70      GL_EXT_texture_compression_s3tc GL_EXT_texture_filter_anisotropic 
       71      GL_EXT_texture_integer GL_EXT_texture_shared_exponent GL_EXT_texture_snorm 
       72      GL_EXT_texture_sRGB GL_EXT_texture_sRGB_decode GL_EXT_texture_swizzle 
       73      GL_EXT_timer_query GL_EXT_transform_feedback 
       74      GL_INTEL_conservative_rasterization GL_INTEL_fragment_shader_ordering 
       75      GL_INTEL_framebuffer_CMAA GL_INTEL_map_texture GL_INTEL_performance_query 
       76      GL_KHR_blend_equation_advanced GL_KHR_blend_equation_advanced_coherent 
       77      GL_KHR_context_flush_control GL_KHR_debug GL_KHR_texture_compression_astc_hdr 
       78      GL_KHR_texture_compression_astc_ldr GL_NV_blend_square GL_NV_conditional_render 
       79      GL_NV_primitive_restart GL_NV_texgen_reflection 
[LWJGL] Loading JNI library: lwjgl_stb
[LWJGL] 	Module: org.lwjgl.stb
[LWJGL] 	Using SharedLibraryLoader...
[LWJGL] 	Found at: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\lwjgl_stb.dll
[LWJGL] 	Loaded from org.lwjgl.librarypath: C:\Users\1645522\AppData\Local\Temp\lwjgl1645522\3.2.3-build-13\lwjgl_stb.dll
[Coordinate:-0.5490483162518301, 0.8567708333333334]
Button clicked!
true
[Coordinate:-0.17423133235724741, 0.38541666666666663]
false
[Coordinate:-0.6866764275256223, 0.8177083333333334]
Button clicked!
true
[Coordinate:-0.6866764275256223, 0.8177083333333334]
Button clicked!
true
[Coordinate:-0.3806734992679356, 0.5286458333333333]
false
[Coordinate:-0.3806734992679356, 0.5286458333333333]
false
[Coordinate:-0.588579795021962, 0.796875]
Button clicked!
true
[Coordinate:-0.588579795021962, 0.796875]
Button clicked!
true
Exception in thread "main" java.lang.IllegalArgumentException: Argument for 1. parameter 'retval_from_stbi_load' must not be null
	at org.lwjglx.debug.RT.checkNotNull(RT.java:791)
	at org.lwjglx.debug.$Proxy$16.stbi_image_free28(Unknown Source)
	at com.programwings.wofrpg.display.drawable.Image.<init>(Image.java:50)
	at com.programwings.wofrpg.display.screens.MainMenu.displayMainMenu(MainMenu.java:18)
	at com.programwings.wofrpg.display.Primary.loop(Primary.java:117)
	at com.programwings.wofrpg.display.Primary.run(Primary.java:35)
	at com.programwings.wofrpg.Runnable.run(Runnable.java:14)
	at com.programwings.wofrpg.Runnable.main(Runnable.java:19)
[LWJGL] 1872 bytes leaked, thread 1 (main), address: 0x24905AD0000
	at org.lwjgl.system.ThreadLocalUtil.setEnv(ThreadLocalUtil.java:164)
	at org.lwjgl.opengl.GL.setCapabilities(GL.java:243)
	at org.lwjgl.opengl.GL.createCapabilities(GL.java:468)
	at org.lwjgl.opengl.GL.createCapabilities(GL.java:322)
	at org.lwjglx.debug.opengl.GL.createCapabilities(GL.java:33)
	at org.lwjglx.debug.$Proxy$14.createCapabilities16(Unknown Source)
	at com.programwings.wofrpg.display.Primary.loop(Primary.java:99)
	at com.programwings.wofrpg.display.Primary.run(Primary.java:35)
	at com.programwings.wofrpg.Runnable.run(Runnable.java:14)
	at com.programwings.wofrpg.Runnable.main(Runnable.java:19)
[LWJGL] 16 bytes leaked, thread 1 (main), address: 0x2495AAF0000
	at org.lwjgl.system.Callback.create(Callback.java:136)
	at org.lwjgl.system.Callback.<init>(Callback.java:86)
	at org.lwjgl.glfw.GLFWErrorCallback.<init>(GLFWErrorCallback.java:60)
	at org.lwjglx.debug.glfw.GLFW$1.<init>(GLFW.java:50)
	at org.lwjglx.debug.glfw.GLFW.ensureErrorCallback(GLFW.java:50)
	at org.lwjglx.debug.glfw.GLFW.glfwInit(GLFW.java:115)
	at org.lwjglx.debug.$Proxy$14.glfwInit5(Unknown Source)
	at com.programwings.wofrpg.display.Primary.init(Primary.java:49)
	at com.programwings.wofrpg.display.Primary.run(Primary.java:34)
	at com.programwings.wofrpg.Runnable.run(Runnable.java:14)
	at com.programwings.wofrpg.Runnable.main(Runnable.java:19)
[LWJGL] 16 bytes leaked, thread 1 (main), address: 0x24905B20000
	at org.lwjgl.system.Callback.create(Callback.java:136)
	at org.lwjgl.system.CallbackI.address(CallbackI.java:34)
	at org.lwjgl.opengl.GLDebugMessageCallback.create(GLDebugMessageCallback.java:54)
	at org.lwjglx.debug.opengl.GLUtil.setupDebugMessageCallback(GLUtil.java:70)
	at org.lwjglx.debug.opengl.GLUtil.setupDebugMessageCallback(GLUtil.java:37)
	at org.lwjglx.debug.opengl.GL.createCapabilities(GL.java:36)
	at org.lwjglx.debug.$Proxy$14.createCapabilities16(Unknown Source)
	at com.programwings.wofrpg.display.Primary.loop(Primary.java:99)
	at com.programwings.wofrpg.display.Primary.run(Primary.java:35)
	at com.programwings.wofrpg.Runnable.run(Runnable.java:14)
	at com.programwings.wofrpg.Runnable.main(Runnable.java:19)

Process finished with exit code 1


As for byte leakages, the main one is the GL.createCapabilities() method in my Primary.java file, which has a GL.setCapabilities(null) when it ends, so I don't know why that one does that. The other one is with the glfwInit method. Once again, no idea why it does it. Can someone please help? I'm not good with this sort of thing yet.