2024-10-07 04:43 PM - edited 2024-10-07 05:49 PM
{
static void* data;
func ->
structure* s = new structure();
// do stuff
data = s;
return;
Callback ->
structure* s = (structure*) data;
// Error
}
Functions are more or less setup as above. This setup is used in many locations on the code, works just fine. But for some reason in one spot, the casting overwrites data in the structure that had memory allocated to it.
The assembly for each cast is generated as
Which, I dont understand why a cast needs to str data. By chance perhaps, it doesnt corrupt data anywhere else. I still have plenty of memory left as well, so its not like ive ran out.
2024-10-07 05:42 PM
{
static void* data;
func ->
structure* s = new structure();
// do stuff
return;
Callback ->
structure* s = (structure*) data;
// Error
}
The local "s" variable in the function has no relation to the local "s" variable in the callback. The global "data" variable isn't accessed anywhere in the function.
It's unclear what result you're looking for here, but the code doesn't do anything useful.
The GCC compiler is robust, it's unlikely the be the culprit. More likely a bug in the code.
2024-10-07 05:52 PM
Sorry you feel like it doesnt do anything. But like I said, it works fine for the rest of my code and is called dozens of times before it hits this error. The pointer and data is all stored correctly and is able to be accessed. The error only specifically occurs with casting to a pointer of the correct structure type.
2024-10-07 06:15 PM - edited 2024-10-07 06:16 PM
> Sorry you feel like it doesnt do anything. But like I said, it works fine for the rest of my code
You have silently edited your post, so yes it does something different than when you originally posted it and when I made my comment.
data should be declared volatile, since it is used in the main thread and a callback, but probably not the issue.
The takeaway is the same: it's a code bug, not a compiler bug.
If something is being modified and you don't know where or why, create a hardware watchpoint which will break when the memory is modified. This will let you see exactly where it gets modified. Could be a stack overflow, could be issues with memory allocation (two variables with the same location), could be out of bounds write.
2024-10-07 06:27 PM
Ive already done that. The casting line is what changed the value of my structure. Hence why I included the assembly portion that is changing my structure data.
>You have silently edited your post, so yes it does something different than when you originally posted it and when I made my comment.
What I edited had no relevance to my question. The code I wrote was only a general example of what's going on. I know specifically where the issue is, and its the 2nd line of assembly generated for each cast.