Monday, March 7, 2016

Access violation exception when using dereferencing a pointer in Dex

Problem
I have a pointer point to a temp table. When I try to dereference the pointer, Dynamics GP crashes with the error: "An unhandled exception of type 'System.AccessViolationException' occurred in Dynamics.exe"

Solution
Because I was initializing the pointer in a form procedure, it was pointing to the procedure's local table buffer. When the procedure was finished, the local table buffer's memory was deallocated (). So when I dereferenced the pointer, it was pointing at nothing, hence the memory error.

Leaky abstraction
This exposes a leaky abstraction. The table buffers and memory management are abstracted away. It's not explicit that the table buffer in a procedure is different than the containing form's table buffer. Furthermore, the memory management doesn't care that there's a pointer pointing to a location in memory, it deallocates anyway. That tells me the pointers aren't really managed.

Hidden form temp table reference pattern
Instead of running into this problem again in the future, i'm going to write down the pattern for making sure this works, so i don't have to spend hours upon hours troubleshooting a leaky abstraction problem.

"Class"-like form:
1. Has one internal window called State. This window has a hidden reference field on it.
2. Form pre: set State's ref field to the table buffer
3. Init: open form if it's not opened, use the dereferenced table buffer

No comments:

Post a Comment

There was an error in this gadget