Jump to content
  • Advertisement
Sign in to follow this  
popsoftheyear

Realloc on separate threads

This topic is 4074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all. Ok so I allocate some data on the main thread that the application is running on. This main thread never ever accesses or uses the allocated data again. I run another thread that accesses this data, and as soon as I do anything (like move the window or whatever) *BOOM* The data is no longer valid as read from this new thread, but was fine until I interacted with the software. However if I copy the same code to the function that runs as a new thread, and realloc the same exact data there instead...it works fine no matter what I do. I searched around on the net without a whole lot of luck...perhaps I'm using the wrong keywords, but can someone help me figure out the cause of this? I've only recently messed with multithreading, so excuse me if it is some obvious common knowledge. Also if there is more info needed or whatever just let me know. Thanks -Scott

Share this post


Link to post
Share on other sites
Advertisement
Show the code.

How are you allocating the data? On stack or on heap? When do you delete it?

You're most likely de-allocating the data before second thread has change to access it.

Share this post


Link to post
Share on other sites
Aye it was a retarded problem.
I was allocating memory in a dynamic array, and had a pointer to one of the objects in this array in another dynamic array. Problem was when I copied the data over to the object being used by the other thread, my copy-on-write scheme copied the dynamic array as the reference count went > 1, which is correct, but the original array was of course destroyed in its destructor when its scope ran out. So I had all the correct data, but there was this pointer pointing to deallocated data as you said Antheus.

Anyway, that looks confusing as I read it, but it would have been a lot of posted code to find the problem. To solve it I just replaced the pointer with an index. Voila. Actually I had went through this earlier when I was converting this API to run on multiple threads, and I just missed that one pointer. I'm relatively new to multithreading concepts though so I'm still learning which crashes or warning signs usually indicate which bugs or problems.

Thanks y'all...turns out taking a break and having some birthday cake is all I needed to fix the bug.

-Scott

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!