Using createMemoryReadFile() with deleteMemoryWhenDropped

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Using createMemoryReadFile() with deleteMemoryWhenDropped

Post by greenya »

Hello!

I'm trying to generate file content in the memory and use it in Irrlicht Engine without creating temporary files on the disk at runtime. So for testing i have created device and wrote next code:

Code: Select all

	char* m = new char[4];
	m[0] = 'A';
	m[1] = 'B';
	m[2] = 'C';
	m[3] = 'D';
	IReadFile* f = device->getFileSystem()->createMemoryReadFile(
		m, // buffer
		4, // length
		"myfile.tmp",
		true /* this should delete my m on drop */);

	// ... do some/nothing here ...

	f->drop(); // i do drop() here,
	// since i have created object using "create" method,
	// and also because the doc says "The returned pointer should be dropped when no longer needed."
This code crashes in line "f->drop()", with "Access violation reading", and Visual Studio brings me to IReferenceCounted::drop() -- so which means that i do bad reference counting. BUT when i set deleteMemoryWhenDropped argument (last argument in createMemoryReadFile() call) to "false", i do not get crash and all goes OK and then ends with device->drop() with no problems.

So what is wrong in my code, and how to use createMemoryReadFile() with deleteMemoryWhenDropped==true ?
hybrid
Admin
Posts: 14144
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Hmm, the code should be correct as is. I'll try this on my own.
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

I just made some testing without any success.

- tried to use "()" instead of "[]" in "new";
- tried to change implementation of CMemoryFile::~CMemoryFile(), from "delete []", to just "delete";

all the time it crashes when it comes to

Code: Select all

delete [] (c8*)Buffer;
saying:
Windows has triggered a breakpoint in 01.HelloWorld.exe.

This may be due to a corruption of the heap, which indicates a bug in 01.HelloWorld.exe or any of the DLLs it has loaded.
and it doesn't matter that Buffer actually points to correct place (accordingly to example above it points to "ABCD<and memory garbage goes here...>").

Does anybody have a clue what else can be wrong ? :?
pc0de
Posts: 300
Joined: Wed Dec 05, 2007 4:41 pm

Post by pc0de »

Just guessing but if you're dynamically linking Irrlicht then it could be that your application and Irrlicht are using different memory heaps.

What happens if you try using IrrAllocator:

Code: Select all

    irr::core::irrAllocator<char> a;
    char* m = a.allocate(4);
    //char* m = new char[4];
    m[0] = 'A';
    m[1] = 'B';
    m[2] = 'C';
    m[3] = 'D';
    IReadFile* f = fs->createMemoryReadFile(
        m, // buffer
        4, // length
        "myfile.tmp",
        true /* this should delete my m on drop */);

    // ... do some/nothing here ...

    f->drop(); // i do drop() here,
This ensures the allocation is done on Irrlichts heap. You can find more information about this here: Allocators and what they do.
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

pc0de,

allocation in the way below has the same behavior -- crash:

Code: Select all

char* m = core::irrAllocator<char>().allocate(4);
// crashes not the allocation call itself, but "f->drop()" call.

This is complete log what VisualStudio2008 writes to the Debug log on crash when i run from IDE:
HEAP[01.HelloWorld.exe]: Invalid address specified to RtlValidateHeap( 005F0000, 017F3E38 )
Windows has triggered a breakpoint in 01.HelloWorld.exe.

This may be due to a corruption of the heap, which indicates a bug in 01.HelloWorld.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while 01.HelloWorld.exe has focus.

The output window may have more diagnostic information.
The program '[3380] 01.HelloWorld.exe: Native' has exited with code 0 (0x0).
And this is the dialog which pops-up when i run directly EXE file:
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!

Program: ...jects\irrlicht-trunk\bin\Win32-VisualStudio\01.HelloWorld.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c
Line: 1317

Expression: _CrtIsValidHeapPointer(pUserData)

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------
pc0de
Posts: 300
Joined: Wed Dec 05, 2007 4:41 pm

Post by pc0de »

Okay, I added your code to 01.HelloWorld and traced down into the CRT malloc()/free() calls for "char* m". The malloc and free are definitely using different heaps (_crtheap base pointers are different) which is why the exception is thrown when CMemoryFile attempts to release it.

The default project files for "01.HelloWorld" and "Irrlicht.dll" link the CRT statically so that explains why the different heaps. What i didn't get is why irrAllocator didn't allocate off of Irrlicht.dll's heap. I guess that's because all of irrAllocator's code is defined inline in the header and it's actually being compiled into 01.HelloWorld... Someone smarter can give a better technical explanation :)

As I see it you have three options:

1. Manage the passed memory in your application - don't let createMemoryReadFile take ownership.

2. Link to the CRT dynamically in both Irrlicht.dll & 01.HelloWorld (your app). That means you have to change "Project | Options | C/C++ | Code Generation | Runtime Library" to "Multi-threaded DLL" (release) or "Multi-threaded Debug DLL" (debug). I tested this and it works fine.

3. Build and link to the static version of Irrlicht. Tested this as well.
hybrid
Admin
Posts: 14144
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

the allocator has to be kept until an allocator.delete can be called. Check irrString for the way to handle it this way. Since there's no way to pass along the allocator with the memory file, this is no solution here.
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

pc0de wrote:As I see it you have three options:

1. Manage the passed memory in your application - don't let createMemoryReadFile take ownership.

2. Link to the CRT dynamically in both Irrlicht.dll & 01.HelloWorld (your app). That means you have to change "Project | Options | C/C++ | Code Generation | Runtime Library" to "Multi-threaded DLL" (release) or "Multi-threaded Debug DLL" (debug). I tested this and it works fine.

3. Build and link to the static version of Irrlicht. Tested this as well.
Thank you very much for investigation.
First option is not possible for me, because i need this method to wrap in Irrlicht Lime (a .net wrapper for Irrlicht), and when user calls the method, i need to return wrapped IReadFile, i allocated memory inside and use deleteMemoryWhenDropped==true which saves me from adding another class which will handle this memory releasing for user. Adding new class is not an option, i will try to solve this using second or third provided options.

Here is how i use it now:

Code: Select all

ReadFile^ FileSystem::CreateMemoryReadFile(String^ filename, array<unsigned char>^ content)
{
	LIME_ASSERT(filename != nullptr);
	LIME_ASSERT(content != nullptr);

	int c = content->Length;
	unsigned char* m = new unsigned char[c];

	for (int i = 0; i < c; i++)
		m[i] = content[i];

	io::IReadFile* f = m_FileSystem->createMemoryReadFile(
		m,
		c,
		Lime::StringToPath(filename),
		true /* allocated m will be deleted automatically on drop() */);

	return ReadFile::Wrap(f);
}
Post Reply