djkoloski / libpd64

A port of LibPD with 64-bit compatibilty with Unity
MIT License
8 stars 1 forks source link

Standalone Windows Build crashes throwing Access Violation error #4

Open antpb opened 6 years ago

antpb commented 6 years ago

Hi!

Your documentation and this repo has been a lifesaver. I'm not sure if what I'm seeing is a bug so I thought I would open an issue to verify. I'm currently trying to build my project to be Windows standalone and for whatever reason I am getting Access Violations when starting up the game.

From the crash error log:

libpdcsharp.dll caused an Access Violation (0xc0000005)
  in module libpdcsharp.dll at 0033:004e14c4.

Everything works fine in the editor, but fails only on build. I've even taken a step back and attempted to build the sample scenes with the same result.

Happy to provide any additional info you need on this. Any thoughts would be super appreciated :)

djkoloski commented 6 years ago

Hi antpb,

I have reproduced the bug in question and can confirm that it happens with the given instructions. Since this was working as of roughly three months ago when @hamoid filed a bug, my guess is that something in the pure-data repo changed.

Compiling shows a few red flags:

pure-data/src/g_canvas.c:1391:5: warning: implicit declaration of function 'u8_utf8toucs2' [-Wimplicit-function-declaration]
     u8_utf8toucs2(ucs2path, MAXPDSTRING, pathbuf, MAXPDSTRING-1);
pure-data/src/g_canvas.c:1392:19: warning: implicit declaration of function '_waccess' [-Wimplicit-function-declaration]
     return (0 ==  _waccess(ucs2path, 0));
pure-data/src/s_main.c: In function 'sys_main':
pure-data/src/s_main.c:337:20: warning: '__p__fmode' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes]
         extern int _fmode;
pure-data/extra/sigmund~/sigmund~.c: In function 'sigmund_getpitch.isra.7':
pure-data/extra/sigmund~/sigmund~.c:385:14: warning: argument 1 value '18446744073709551424' exceeds maximum object size 9223372036854775807 [-Walloc-size-larger-than=]
     t_float *weights =  (t_float *)alloca(sizeof(t_float) * npit);

I'd try fixing those warnings first. The implicit function declarations are probably missing some #includes or had a function renamed. Those are definitely suspect to me.

Do you have the same warnings?

antpb commented 6 years ago

Is that in compiling the .dll or when building the standalone build from Unity?

Aside from what I'm seeing in the crash report, I'm not getting much more from the build. I don't think I can actually get to a point in the scene where I could log something useful. It seems to be on the scene initializing.

I think I'm going to follow your readme to build a fresh version of the .dll's maybe. It's been a while since I've refreshed things. If you have any advice there let me know.

antpb commented 6 years ago

update: rebuild didnt work aside from no longer working in editor haha.

I noticed something interesting in the libpd issues. Not sure if this helps pinpoint it but another user of libpd is having the exact same issue and provided a stacktrace on the function crashing the player. They also have a public repo for debugging this crash. https://github.com/Nagasaki45/unipd

========== OUTPUTING STACK TRACE ==================

0x000000006629C0F3 (libpdcsharp) startpost
0x00000000662A83AA (libpdcsharp) x_gui_setup
0x000000006628AE62 (libpdcsharp) outlet_anything
0x0000000066284129 (libpdcsharp) binbuf_eval
0x000000006628AB9D (libpdcsharp) outlet_bang
0x0000000066288CE1 (libpdcsharp) pd_vmess
0x000000006623B7AF (libpdcsharp) canvas_loadbang
0x0000000066288CE1 (libpdcsharp) pd_vmess
0x000000006628BD93 (libpdcsharp) pd_doloadbang
0x0000000066286556 (libpdcsharp) glob_evalfile
0x000000003F1EAFCA (Mono JIT Code) (wrapper managed-to-native) LibPDBinding.LibPD:openfile (string,string)
0x000000003F1EAC18 (Mono JIT Code) [C:\Users\MAT-VR02\Desktop\tomg\libpd4unity\libpd4unity\Assets\LibPD\LibPDNativeMethods.cs:144] LibPDBinding.LibPD:OpenPatch (string) 
0x000000003F1EA988 (Mono JIT Code) (wrapper synchronized) LibPDBinding.LibPD:OpenPatch (string)
0x000000003F1E5483 (Mono JIT Code) [C:\Users\MAT-VR02\Desktop\tomg\libpd4unity\libpd4unity\Assets\LibPdFilterRead.cs:110] LibPdFilterRead:loadPatch (string) 
0x000000003F1DC8FA (Mono JIT Code) [C:\Users\MAT-VR02\Desktop\tomg\libpd4unity\libpd4unity\Assets\LibPdFilterRead.cs:35] LibPdFilterRead:Awake () 
0x0000000017C17402 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
0x00007FF959DF64CF (mono) [c:\buildslave\mono\build\mono\mini\mini.c:4937] mono_jit_runtime_invoke 
0x00007FF959D48A49 (mono) [c:\buildslave\mono\build\mono\metadata\object.c:2623] mono_runtime_invoke 
0x0000000140A2F43C (Unity) scripting_method_invoke
0x0000000140A22DAA (Unity) ScriptingInvocation::Invoke
0x0000000140A23E1E (Unity) ScriptingInvocation::InvokeChecked
0x00000001409E50C1 (Unity) MonoBehaviour::CallMethodInactive
0x00000001409E55F4 (Unity) MonoBehaviour::CallAwake
0x00000001409EFA49 (Unity) MonoBehaviour::AddToManager
0x00000001409E6122 (Unity) MonoBehaviour::AwakeFromLoad
0x0000000140A97308 (Unity) AwakeFromLoadQueue::InvokePersistentManagerAwake
0x0000000140A9B7E9 (Unity) AwakeFromLoadQueue::PersistentManagerAwakeFromLoad
0x00000001407FC8D7 (Unity) LoadSceneOperation::CompleteAwakeSequence
0x00000001407FCD8A (Unity) LoadSceneOperation::CompletePreloadManagerLoadSceneEditor
0x0000000140802103 (Unity) LoadSceneOperation::IntegrateMainThread
0x0000000140800E48 (Unity) PreloadManager::UpdatePreloadingSingleStep
0x0000000140800FDD (Unity) PreloadManager::WaitForAllAsyncOperationsToComplete
0x0000000140C8CBA7 (Unity) EditorSceneManager::RestoreSceneBackups
0x00000001411E8FBF (Unity) PlayerLoopController::EnterPlayMode
0x00000001411E98A6 (Unity) PlayerLoopController::SetIsPlaying
0x00000001411EA6F6 (Unity) Application::TickTimer
0x00000001414101DF (Unity) MainMessageLoop
0x0000000141411A9C (Unity) WinMain
0x0000000141E662B8 (Unity) __tmainCRTStartup
0x00007FF989A81FE4 (KERNEL32) BaseThreadInitThunk
0x00007FF98BE4EFC1 (ntdll) RtlUserThreadStart

========== END OF STACKTRACE ===========
First lines mark the exact functions and files, which cause the crash. In this case, it's libpdcsharp.dll file. 
antpb commented 6 years ago

I'm going to attempt to fix the warnings you sent @djkoloski , and recompile...if I can figure it out I'll make a PR here. Not to get your hopes up though, this is starting to stretch out of my capabilities. Learning is always fun though!

antpb commented 6 years ago

After a long day of digging I have some new info. Still crashing but I have new discoveries that may help narrow things down. (FYI I'm using Unity 2017.3.1f1)

pure-data/src/g_canvas.c:1391:5: warning: implicit declaration of function 'u8_utf8toucs2' [-Wimplicit-function-declaration]
     u8_utf8toucs2(ucs2path, MAXPDSTRING, pathbuf, MAXPDSTRING-1);

For the above, the fix is adding #include <s_utf8.h> after line 1380

pure-data/src/g_canvas.c:1392:19: warning: implicit declaration of function '_waccess' [-Wimplicit-function-declaration]
     return (0 ==  _waccess(ucs2path, 0));

For the above, the fix seems to be adding io.h to the src directory and adding #include <io.h> after line 1381 (after the s_utf8.h line) .

_waccess seems to require that file per these docs: https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-6.0/aa246685(v=vs.60)

Here's the source for the io.h file I used: https://sourceforge.net/p/mingw/mingw-org-wsl/ci/de3d0fc478b5795fd32e365cd30af1061ca691e6/tree/include/io.h

pure-data/src/s_main.c:337:20: warning: '__p__fmode' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes]
         extern int _fmode;

This one went away as I went a different approach. I've gone down the path of using libpd branch 0.10.0 paired with pd v0.47-1. Everything works fine in editor, it's only on build that I get a crash. Here's what I have left:

gcc -DPD -DHAVE_UNISTD_H -DUSEAPI_DUMMY -I./pure-data/src -I./libpd_wrapper -I./libpd_wrapper/util -DWINVER=0x502 -DWIN32 -D_WIN32 -DPD_INTERNAL -I"/include" -I"/include/win32" -O3     -c -o pure-data/src/g_editor.o pure-data/src/g_editor.c
pure-data/src/g_editor.c: In function 'canvas_vis':
pure-data/src/g_editor.c:973:17: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
                 (unsigned long)c);
                 ^
pure-data/src/g_editor.c:979:36: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
                          " .x%lx", (unsigned long)c);
                                    ^
gcc -DPD -DHAVE_UNISTD_H -DUSEAPI_DUMMY -I./pure-data/src -I./libpd_wrapper -I./libpd_wrapper/util -DWINVER=0x502 -DWIN32 -D_WIN32 -DPD_INTERNAL -I"/include" -I"/include/win32" -O3     -c -o pure-data/src/g_graph.o pure-data/src/g_graph.c
pure-data/src/g_graph.c: In function 'graph_vis':
pure-data/src/g_graph.c:750:14: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
              (long)glist_getcanvas(x),  x1, i, arrayname->s_name, sys_font,
              ^
gcc -DPD -DHAVE_UNISTD_H -DUSEAPI_DUMMY -I./pure-data/src -I./libpd_wrapper -I./libpd_wrapper/util -DWINVER=0x502 -DWIN32 -D_WIN32 -DPD_INTERNAL -I"/include" -I"/include/win32" -O3     -c -o pure-data/src/s_inter.o pure-data/src/s_inter.c
pure-data/src/s_inter.c:35:0: warning: "EADDRINUSE" redefined
 #define EADDRINUSE WSAEADDRINUSE

In file included from C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/6.2.0/include/mm_malloc.h:28:0,
                 from C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/6.2.0/include/xmmintrin.h:34,
                 from C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/6.2.0/include/x86intrin.h:33,
                 from C:/msys64/mingw64/x86_64-w64-mingw32/include/winnt.h:1519,
                 from C:/msys64/mingw64/x86_64-w64-mingw32/include/minwindef.h:163,
                 from C:/msys64/mingw64/x86_64-w64-mingw32/include/windef.h:8,
                 from C:/msys64/mingw64/x86_64-w64-mingw32/include/windows.h:69,
                 from C:/msys64/mingw64/x86_64-w64-mingw32/include/winsock.h:10,
                 from pure-data/src/s_inter.c:32:
C:/msys64/mingw64/x86_64-w64-mingw32/include/errno.h:86:0: note: this is the location of the previous definition
 #define EADDRINUSE 100

gcc -DPD -DHAVE_UNISTD_H -DUSEAPI_DUMMY -I./pure-data/src -I./libpd_wrapper -I./libpd_wrapper/util -DWINVER=0x502 -DWIN32 -D_WIN32 -DPD_INTERNAL -I"/include" -I"/include/win32" -O3     -c -o pure-data/src/s_loader.o pure-data/src/s_loader.c
gcc -DPD -DHAVE_UNISTD_H -DUSEAPI_DUMMY -I./pure-data/src -I./libpd_wrapper -I./libpd_wrapper/util -DWINVER=0x502 -DWIN32 -D_WIN32 -DPD_INTERNAL -I"/include" -I"/include/win32" -O3     -c -o pure-data/src/s_main.o pure-data/src/s_main.c
gcc -DPD -DHAVE_UNISTD_H -DUSEAPI_DUMMY -I./pure-data/src -I./libpd_wrapper -I./libpd_wrapper/util -DWINVER=0x502 -DWIN32 -D_WIN32 -DPD_INTERNAL -I"/include" -I"/include/win32" -O3     -c -o pure-data/src/s_path.o pure-data/src/s_path.c
gcc -DPD -DHAVE_UNISTD_H -DUSEAPI_DUMMY -I./pure-data/src -I./libpd_wrapper -I./libpd_wrapper/util -DWINVER=0x502 -DWIN32 -D_WIN32 -DPD_INTERNAL -I"/include" -I"/include/win32" -O3     -c -o pure-data/src/s_print.o pure-data/src/s_print.c
pure-data/src/s_print.c: In function 'strnpointerid':
pure-data/src/s_print.c:46:38: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
         snprintf(dest, len, ".x%lx", (unsigned long)pointer);
                                      ^
pure-data/src/s_print.c: In function 'glob_findinstance':
pure-data/src/s_print.c:278:30: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
             canvas_finderror((void *)obj);
                              ^
gcc -DPD -DHAVE_UNISTD_H -DUSEAPI_DUMMY -I./pure-data/src -I./libpd_wrapper -I./libpd_wrapper/util -DWINVER=0x502 -DWIN32 -D_WIN32 -DPD_INTERNAL -I"/include" -I"/include/win32" -O3     -c -o pure-data/src/x_text.o pure-data/src/x_text.c
pure-data/src/x_text.c: In function 'textbuf_open':
pure-data/src/x_text.c:95:31: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
         sprintf(buf, ".x%lx", (unsigned long)x);
                               ^

Going to keep digging, but if you have any thoughts feel free to add :) thanks!

antpb commented 6 years ago

Most interesting development. Building from Unity 5.6.5 works just fine! Not too sure what this means...really not excited to bring my code down to 5.6.5 standards after spending so long getting things to the latest 2017 lol

Any idea why this might be? I used the dlls that contained the above errors on compile. So weird.

Update: Have tested up to Unity 2017.1.0 and have gotten successful builds with no crash. Patch loads fine. Going to keep crawling up versions til I find where things changed.

djkoloski commented 6 years ago

Hmm, that's definitely an interesting development. My guess is that 2017.2 will be the culprit. The changelog for it mentions several changes to DLL loading behavior: https://unity3d.com/unity/whats-new/unity-2017.2.0

This could also be a latent issue that has been aggravated by those changes, so it's worth keeping anything on the table.

anthonyburchell commented 6 years ago

Was just chiming in to say I crawled up and the break is from .1 updating to .2.0. Makes total sense. I'll keep digging and share what I find as it comes. :) Thanks for the link! That is handy.

DanteMarshal commented 4 years ago

Is this bug resolved ? Cuz I'm having the same issue trying to use libSkiaSharp on Unity 2019.2.17f1. The crash happens in Editor (Didn't try building the whole thing cuz it's pretty big now !)

Here's Unity's Crash Report, including a stack trace.

djkoloski commented 4 years ago

Your crash looks unrelated. This issue is related to a change in DLL loading behavior between Unity 2017.1 and 2017.2. These are old versions now. Your crash is a generic segmentation fault that may likely be a logical error. See the relevant part of the stack trace:

0x00007FFA7A3C6D1D (libSkiaSharp.win.x64) sk_vertices_make_copy
0x000001FA359E9A24 (SkiaSharp) SkiaSharp.SkiaApi.sk_bitmap_get_pixel_color()
0x000001FA359E984B (SkiaSharp) SkiaSharp.SKBitmap.GetPixel()
DanteMarshal commented 4 years ago

@djkoloski Yessss, That was right. Thank you !

I've been using the wrong Height and Width to calculate pixel coordinates before calling GetPixel

I think if SkiaSharp C# wrapper throws an IndexOutOfRange exception in such cases everything would be smoother.