Click here to Skip to main content
11,932,103 members (59,948 online)
Rate this:
Please Sign up or sign in to vote.
See more: C threads
i have created a thread in which i am allocating memory to a pointer,freeing it inside the same. The function is being called in more than one thread.but some times it is getting crashed because of EXC_BAD_INSTRUCTION and at log it is giving incorrect checksum for frees object - object was probably modified after being freed.

this is the code
Posted 18-May-12 1:13am
Edited 18-May-12 11:09am
Richard MacCutchan 18-May-12 6:28am
Very interesting, but I do not think that anyone can help you without a lot more information.
subrata kumar Nayak 18-May-12 7:14am
sry....i don't know what i need to give as more information.......
subrata kumar Nayak 18-May-12 8:22am
above i have updated the question with code ........
Richard MacCutchan 18-May-12 12:44pm
Post the code here, not on some other site. Note, use the "Improve Question" link and put <pre> tags around it. Tell us exactly which line gets an error and what the error is.
CPallini 18-May-12 8:39am
Is the pointer variable shared among the threads?
subrata kumar Nayak 18-May-12 8:44am
no,the variable is not shared,as there are two threads only(main,thread created by me)and i am using the variable in thread created by me only...not in main thread.....
(u can check the code also,posted by me.)
CPallini 18-May-12 16:57pm
I see no code.
subrata kumar Nayak 19-May-12 0:09am
in my question only, there is a link "this is the code",click on code ,it will redirect to my code.....
CPallini 19-May-12 4:22am
OK, I missed it.
However, you are a liar :-) since p.Var IS a shared variable and it is the root of all your problems (if you are not married).
subrata kumar Nayak 19-May-12 6:35am
pVar is an global object is not being shared among threads......
CPallini 19-May-12 14:07pm
If it is global then it is shared.
Pablo Aliskevicius 18-May-12 12:49pm
I'd suggest checking the return values of the allocation functions (GlobalAlloc, GlobalRealloc, ...) for NULL. Lately, memory allocation functions are returning NULL more than they used to, particularly on virtual machines. Something to do with timeouts.
subrata kumar Nayak 19-May-12 0:12am
by checking null also it is not working.....
i am checking whether the return type of malloc is null or coming not null,still it is crashing.......
SAKryukov 18-May-12 13:18pm
What you really need it to make a code sample (better be complete but as short as possible) just to reproduce this exact problem. If you can do it, use "Improve question" above.
First of all, check up if you can reproduce the problem is a single-thread application.
subrata kumar Nayak 19-May-12 0:11am
problem is not there with single thread application.....when ever i am creating multiple thread then only it happens.....
SAKryukov 22-May-12 0:51am
OK, that's easier. Now, you need to create a code sample and provided it, with steps to reproduce. How about it?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

you need to balance the memory allocations per thread. So beware of accessing memory in different threads, ie per global objects.

Take care in exiting every thread that cleanup is fine.

Dont use functions or API-Calls in destructors!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 23 May 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100