crashes - A heap has been corrupted error

Discussion related to the LuxCore functionality, implementations and API.
User avatar
TAO
Developer
Developer
Posts: 850
Joined: Sun Mar 24, 2019 4:49 pm
Location: France
Contact:

crashes - A heap has been corrupted error

Post by TAO »

I noticed if I cancel the render too quickly normally before 50 passes 3dsmax will crash and the compiler will show me the error about "a heap has been corrupted" with a pointer on Luxcore::Scene and critical error number C0000374. The problem will appear just if I cancel the render too quickly or hit the render too many times. By the way, after 100 passes or so, never happened.
Not sure if this is related or not but if I compile all the codes with the optimization /Ox switch the problem will be less likely to happen and most of the time I will get no error at all.
here is the actual error in the compiler that may be useful.
Screenshot 2021-11-30 013840.jpg
Screenshot 2021-11-30 013846.jpg
User avatar
Dade
Developer
Developer
Posts: 5672
Joined: Mon Dec 04, 2017 8:36 pm
Location: Italy

Re: crashes - A heap has been corrupted error

Post by Dade »

You are deleting objects in the wrong order. You are deleting objects while they are still referenced by other objects ("config" is referenced by "session", etc.). They crash can or can not happen, it depends if the broken pointers are referencing memory that has been reused (for your process) or not.

Also the correct order is:

Code: Select all

delete session;
delete config;
delete &scene; // NOTE: see below, there may be more
How do you create the scene object ? Are you using the RenderConfig::DeleteSceneOnExit() ?
Support LuxCoreRender project with salts and bounties
User avatar
TAO
Developer
Developer
Posts: 850
Joined: Sun Mar 24, 2019 4:49 pm
Location: France
Contact:

Re: crashes - A heap has been corrupted error

Post by TAO »

Dade wrote: Tue Nov 30, 2021 8:53 am You are deleting objects in the wrong order. You are deleting objects while they are still referenced by other objects ("config" is referenced by "session", etc.). They crash can or can not happen, it depends if the broken pointers are referencing memory that has been reused (for your process) or not.

Also the correct order is:

Code: Select all

delete session;
delete config;
delete &scene; // NOTE: see below, there may be more
How do you create the scene object ? Are you using the RenderConfig::DeleteSceneOnExit() ?
I did not use DeleteSceneOnExit() function and changed the order of the delete session as you mention.

Code: Select all

Delete &scene
is the last line and after that just resume all suspension in 3dsmax nothing more.
I need to add this, the error does not always mean crash, sometimes I will see the error but 3dsmax will not crash and function normally so if I'm not in debug mode I can not notice that all the time, just when the crash happens.
Screenshot 2021-11-30 113542.jpg
Screenshot 2021-11-30 113831.jpg
User avatar
Dade
Developer
Developer
Posts: 5672
Joined: Mon Dec 04, 2017 8:36 pm
Location: Italy

Re: crashes - A heap has been corrupted error

Post by Dade »

How do you create the scene object ?
Support LuxCoreRender project with salts and bounties
User avatar
TAO
Developer
Developer
Posts: 850
Joined: Sun Mar 24, 2019 4:49 pm
Location: France
Contact:

Re: crashes - A heap has been corrupted error

Post by TAO »

Dade wrote: Tue Nov 30, 2021 1:51 pm How do you create the scene object ?
That's how I create sessions, config, and scenes, in the middle pass the data to it, and then after stopping the session delete all unnecessary things and free up memory.

note: i did not use //scene.RemoveUnusedTextures(); because once a while it will remove the last texture that actually is in use. don't know why. a minor issue that I face.

Code: Select all

scene.RemoveUnusedImageMaps();
	scene.RemoveUnusedMaterials();
	scene.RemoveUnusedMeshes();
	//scene.RemoveUnusedTextures();
	scene.SetDeleteMeshData(true);
	RenderConfig *config = RenderConfig::Create(renderConfigProp, &scene);
	RenderSession *session = RenderSession::Create(config);

	//const float iteration_time_seconds = m_rendering_stopwatch.GetElapsedTime() / 1000.0;
	//TSTR msg;
	//msg.printf(MaxSDK::GetResourceStringAsMSTR(IDS_RENDER_TIMER), iteration_time_seconds);
	//m_rendering_logger.LogMessage(IRenderingLogger::MessageType::Info, msg);
	prog->SetSceneStats(0, 0, 0, maxscene->NumChildren(), 0);
	
	renderProgTitle = (L"Compile the kernels, can take up to 5 minutes, be patient");
	prog->SetTitle(renderProgTitle);

	// To avoid const_cast below, and possibility of notifiee from changing the value                
	BroadcastNotification(NOTIFY_PRE_RENDER, (void*)(RendParams*)&frp);
	m_rendering_stopwatch.Reset();
	m_rendering_stopwatch.Start();
	session->Start();
	DoRendering(session, prog, tobm);
	session->Stop();
	m_rendering_stopwatch.Stop();
	const float iteration_time_seconds = m_rendering_stopwatch.GetElapsedTime() / 1000.0;
	mprintf(L"\nLuxCore -> Render finished in %.2f seconds\n", iteration_time_seconds);
	mflush();

	BroadcastNotification(NOTIFY_POST_RENDER);

	delete session;
	delete config;
	delete &scene;

	GetCOREInterface()->EnableSceneRedraw();
	// Resume undo, macro recorder, animation, auto backup, and maintain
	SuspendAll Resume();
User avatar
Dade
Developer
Developer
Posts: 5672
Joined: Mon Dec 04, 2017 8:36 pm
Location: Italy

Re: crashes - A heap has been corrupted error

Post by Dade »

TAO wrote: Tue Nov 30, 2021 3:07 pm
Dade wrote: Tue Nov 30, 2021 1:51 pm How do you create the scene object ?
That's how I create [...] scenes
"That's" ? Where ?
Support LuxCoreRender project with salts and bounties
User avatar
TAO
Developer
Developer
Posts: 850
Joined: Sun Mar 24, 2019 4:49 pm
Location: France
Contact:

Re: crashes - A heap has been corrupted error

Post by TAO »

Dade wrote: Tue Nov 30, 2021 4:48 pm
TAO wrote: Tue Nov 30, 2021 3:07 pm
Dade wrote: Tue Nov 30, 2021 1:51 pm How do you create the scene object ?
That's how I create [...] scenes
"That's" ? Where ?
Sorry I think I miss the first part of the code.

Code: Select all

int MaxToLux::Render(TimeValue t, Bitmap *tobm, FrameRendParams &frp, HWND hwnd, RendProgressCallback *prog, ViewParams *vp)
{
	if (renderingMaterialPreview)
	{
		renderPreview(tobm, prog);
		return true;
	}
	else
	{
		//Scene *scene = NULL;
		Scene *scene = Scene::Create();
		//In the camera 'export' function we check for supported camera, return false if camera not supported.
		if (!lxmCamera.exportCamera((float)atof(LensRadiusstr.ToCStr()), *scene, t))
		{
			return false;
		}

		renderFinal(*scene, t, tobm,frp, hwnd, prog, vp);
		return true;
	}
}
User avatar
Dade
Developer
Developer
Posts: 5672
Joined: Mon Dec 04, 2017 8:36 pm
Location: Italy

Re: crashes - A heap has been corrupted error

Post by Dade »

TAO wrote: Tue Nov 30, 2021 5:49 pm

Code: Select all

		renderFinal(*scene, t, tobm,frp, hwnd, prog, vp);
You may be passing scene by value (not by pointer), how is renderFinal() declared ?
Support LuxCoreRender project with salts and bounties
User avatar
TAO
Developer
Developer
Posts: 850
Joined: Sun Mar 24, 2019 4:49 pm
Location: France
Contact:

Re: crashes - A heap has been corrupted error

Post by TAO »

Dade wrote: Tue Nov 30, 2021 7:12 pm
TAO wrote: Tue Nov 30, 2021 5:49 pm

Code: Select all

		renderFinal(*scene, t, tobm,frp, hwnd, prog, vp);
You may be passing scene by value (not by pointer), how is renderFinal() declared ?
I'm not sure, Here is the renderfinal.

Code: Select all

void MaxToLux::renderFinal(luxcore::Scene &scene, TimeValue t, Bitmap *tobm, FrameRendParams &frp, HWND hwnd, RendProgressCallback *prog, ViewParams *vp)
User avatar
Dade
Developer
Developer
Posts: 5672
Joined: Mon Dec 04, 2017 8:36 pm
Location: Italy

Re: crashes - A heap has been corrupted error

Post by Dade »

TAO wrote: Tue Nov 30, 2021 8:25 pm I'm not sure, Here is the renderfinal.

Code: Select all

void MaxToLux::renderFinal(luxcore::Scene &scene, TimeValue t, Bitmap *tobm, FrameRendParams &frp, HWND hwnd, RendProgressCallback *prog, ViewParams *vp)
It should work, the argument is declared by reference.
Support LuxCoreRender project with salts and bounties
Post Reply