C
C#Waffen

❔ Problem with Dictionary<ulong, Image> (Strange work of memory access)

Concept of my system: A system that draws a picture with stickers, each of the stickers can be moved, there can be an unlimited number of stickers. Code with explanations in comments:
private static Dictionary<ulong, Image> UserStickerProfiles = new();

public static async Task MoveSticker(Sticker sticker)
{
var stickerImage = await ItemManager.GetProfileItemImageAsync(itemId); // take image of sticker from file

UserStickerProfiles.TryGetValue(args.User.Id, out var image);
if (image == null)
{
image = await ProfileMethods.DrawProfileExceptStickerAsync(args.User, itemId); // draw picture without this sticker
UserStickerProfiles.Add(args.User.Id, image);
}

image.Mutate(x => x.DrawImage(stickerImage, new Point(sticker.PositionX, sticker.PositionY), 1f)); // draw this sticker with new coordinations

// saving and sending
var memoryStream = new MemoryStream();
await image.SaveAsPngAsync(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);

// send image to user with memortStream
}
private static Dictionary<ulong, Image> UserStickerProfiles = new();

public static async Task MoveSticker(Sticker sticker)
{
var stickerImage = await ItemManager.GetProfileItemImageAsync(itemId); // take image of sticker from file

UserStickerProfiles.TryGetValue(args.User.Id, out var image);
if (image == null)
{
image = await ProfileMethods.DrawProfileExceptStickerAsync(args.User, itemId); // draw picture without this sticker
UserStickerProfiles.Add(args.User.Id, image);
}

image.Mutate(x => x.DrawImage(stickerImage, new Point(sticker.PositionX, sticker.PositionY), 1f)); // draw this sticker with new coordinations

// saving and sending
var memoryStream = new MemoryStream();
await image.SaveAsPngAsync(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);

// send image to user with memortStream
}
Problem: Each time you use this function, a new picture should appear with ALL stickers and the sticker that we moved to a new location. However, it happens that the sticker is duplicated and there are several of them. The suggestion line image.Mutate(x => x.DrawImage(stickerImage, new Point(sticker.PositionX, sticker.PositionY), 1f)); updates the Dictionary directly, though it shouldn't be, presumably because C# has a memory bug here. I am using .NET 7.0
SL
Sherbert Lemon397d ago
where do itemId and args come from?
K
Kouhai /人◕ ‿‿ ◕人\397d ago
updates the Dictionary directly, though it shouldn't be Do you mean it shouldn't update the instance of image saved in the Dictionary? You're only doing this operation if image == null, so the dictionary isn't effected
W
Waffen397d ago
Take it like a constant Yes, i know that dictionary isn't effected and it's can't be updated but it is
S
Sossenbinder397d ago
That looks a lot like imagesharp, and as far as I can remember, imagesharp works in-place, so it will mutate the existing Image reference If you want to make a copy of what you have instead of mutating it in-place, you should be able to use the .Clone(...) method That will return a cloned image which won't affect the original reference
K
Kouhai /人◕ ‿‿ ◕人\397d ago
How does DrawProfileExceptStickerAsync work?
W
Waffen397d ago
@Sossenbinder Thank you very much! It works!
A
Accord396d ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ How do I refactor this?I am working with a grid of tiles, each tile has an int, which is made up of 4 bytes which represent❔ [AvaloniaUI] Visibility of item in ListView based on conditionAn Avalonia UI application is used to manage an evidence of items. It allows the user to add items. ✅ How to structure this login/signup page layoutSo I have this mockup of the layout I want for a login/signup page in my Blazor WASM app. Pretty sta❔ I am need of help before I give up and run my code on a server instead of a serverless solution.I have create an azure function locally and i've used the selenuim webdriver package for taking scre✅ .Net Core 6 Asymmetric Encryption with custom public and private keysHello all! How can i use the `Asymmetric Encryption` in .Net 6 but choosing/importing private and pu❔ No Design in C#Instances of this bug (1) 1. View Call Stack at System.Runtime.InteropServices.Marshal.ThrowExce❔ having issues on a reloading script on unity to reload weaponsthe code is used in a youtube video and i have pretty much copied to to get it to work but it doesnt❔ help with an exerciseI've encountered a weird problem that idk how to fix. Say we've got a string "31131123521" how do i ❔ Accessing HTTP Context at DbCommandInterceptor [.NET 7]Hi! I'm having some issues trying to access the HTTP Context at my DbCommand Interceptor class. What❔ XMLAttributeCollection -> Dictionary with LINQ?It's a confusing class. It only allows turning it into a Queryable, but I have no experience with th❔ MS SQL Reporting Server URL Being RedirectedWe have an application from a vendor that was written in C# and we recently upgraded the MS SQL data❔ dataset memory leakI found memory leak and I don't understand how to fix it Test case: ```cpp public class MemoryLeaksT❔ WebAPI .NET 7.0 - Encrypt connectionstring in appsettings.jsonguys, I'm writing some WebAPI using .NET 7.0, and I'd like to secure the connectionstring with encry❔ how to read strings correctly with System.Data.SQLitei m trying read turkish characters from database but i can't see correctly❔ Blazor server app, base url / base page to set environmentHi 🙂 I have a Blazor Server app, where I would like to use some dynamic baseurl. Say instead of my❔ C# Console Application -its only writing the same random string, how can i make it do different ones?❔ Rapid prototypingDo you feel there is such a thing as rapid prototyping or mvp development where you do things differ❔ HotChocolate with IQueryable, apply a required filter on the ef entityBasically, I'm trying to find a way to configure an `IObjectFieldDescriptor` by adding a required ar❔ Dev/prod databseHello, Me and a friend wants to create a webb-app project with a react frontend and C# backend. ❔ Book recommendation to learn c#/dotnetI've been programming in python for a little under two years. I am starting a new job in c#. Whats a