C
C#7mo ago
sir loin

Weird Access violation (no exception) when using StreamReader

internal static IEnumerable<Object3d> CreateModel(string path, string spritePath, uint startTime)
{
using var archive = ZipFile.OpenRead(Paths.AssetArchive);
using StreamReader read = new(archive.GetEntry(path).Open());

List<Vector3> vertices = [];

string line;
while ((line = read.ReadLine()) is not null)
{
var values = line.Trim().Split(' ');
if (values[0].Equals("v")) vertices.Add(new(float.Parse(values[1]), -float.Parse(values[3]), float.Parse(values[2])));
else if (values[0].Equals("f"))
{
List<Vector3> faceVertices = [];
for (var i = 1; i < values.Length; ++i) faceVertices.Add(vertices[int.Parse(values[i].Split('/')[0]) - 1]);

for (var i = 1; i <= faceVertices.Count; ++i)
{
Line3d sprite = new()
{
SpritePath = spritePath,
UseDistanceFade = false
};

sprite.StartPosition.Add(startTime, faceVertices[i == faceVertices.Count ? 0 : i]);
sprite.EndPosition.Add(startTime, faceVertices[i - 1]);
sprite.Thickness.Add(startTime, 2);

yield return sprite;
}
}
}
}
internal static IEnumerable<Object3d> CreateModel(string path, string spritePath, uint startTime)
{
using var archive = ZipFile.OpenRead(Paths.AssetArchive);
using StreamReader read = new(archive.GetEntry(path).Open());

List<Vector3> vertices = [];

string line;
while ((line = read.ReadLine()) is not null)
{
var values = line.Trim().Split(' ');
if (values[0].Equals("v")) vertices.Add(new(float.Parse(values[1]), -float.Parse(values[3]), float.Parse(values[2])));
else if (values[0].Equals("f"))
{
List<Vector3> faceVertices = [];
for (var i = 1; i < values.Length; ++i) faceVertices.Add(vertices[int.Parse(values[i].Split('/')[0]) - 1]);

for (var i = 1; i <= faceVertices.Count; ++i)
{
Line3d sprite = new()
{
SpritePath = spritePath,
UseDistanceFade = false
};

sprite.StartPosition.Add(startTime, faceVertices[i == faceVertices.Count ? 0 : i]);
sprite.EndPosition.Add(startTime, faceVertices[i - 1]);
sprite.Thickness.Add(startTime, 2);

yield return sprite;
}
}
}
}
I have no idea why reading from this stream and then referencing from the vertices list is causing a memory violation, I need help
12 Replies
phaseshift
phaseshift7mo ago
Saying access violation then saying no exception doesn't make sense to me. Can you run in the debugger and paste the exact symptoms
sir loin
sir loin7mo ago
it literally does not do anything because it doesn't throw an access violdation exception the app just crashes in the debugger it says The program '[33224] StorybrewEditor.exe' has exited with code 3221225477 (0xc0000005) 'Access violation' no call stack or anything
phaseshift
phaseshift7mo ago
If you install windebug you'll most likely be able to get a reasonable stack, but that is odd
sir loin
sir loin7mo ago
it's weird that this code, which seems completely safe, is causing something like this
phaseshift
phaseshift7mo ago
Also if you bisect your code, or otherwise figure out which line is causing the issue that might help. Perhaps the issue is from another thread, tho
sir loin
sir loin7mo ago
actually I don't think it's the StreamReader it's the vertices list it's either indexing or adding to it causes an error but idk why
phaseshift
phaseshift7mo ago
Even if you access a list with an invalid index that does not cause a native memory fault Something else is going on
sir loin
sir loin7mo ago
yeah that's the part that stumped so removing the code that indexed that list fixed it so it's the indexing I literally do not see how it could cause an error, it should definitely be within bounds
JP
JP7mo ago
My brain goes... is this Object3d used by some comparatively unsafe code... and your particular Vector3 that gets added... results in an object state that causese that unsafe code to except
sir loin
sir loin7mo ago
the parent program is written in completely safe code, Object3d is using a 3D library which relies on System.Numerics and also the issue comes from indexing faceVertices list (I narrowed it down) but I still have no idea why
JP
JP7mo ago
To confirm, the error still occurs when you do all of the indexed-access, but don't actually use them with .Add? if so... that's weird to me
sir loin
sir loin7mo ago
really really weird\ sadly I haven't really fixed the issue, it seems like trying to do an alternative would be way too complicated I don't see how something this simple could cause a memory violation when there's no unsafe code